Default values of fields
Java has exactly eight built-in primitive types: four for whole numbers (byte, short, int, long), two for decimals (float, double), one for characters (char) and one for true/false (boolean). Each has a fixed size and range on every computer, which is a big part of why Java programs behave the same everywhere.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Declare well-chosen variables for: a person's age (14), the number of stars in the Milky Way (100 billion), a temperature (36.6), a flag saying whether a door is locked, and a grade letter. Print them on one line.
Print how many bytes one million values of byte, int, long and double would need in an array, using the BYTES constants.
Print one third as a float and as a double and count how many correct digits each shows.
Explain it without notes
Name the eight primitive types with their sizes, and say which is the default for whole numbers and which for decimals.
Why does long x = 3000000000; fail to compile even though 3 billion fits in a long?
Why does Java have primitives at all, if everything else is an object?
Which values do fields and array elements get by default, and why don't local variables get them?
Expected output
byte=0 short=0 int=0 long=0
float=0.0 double=0.0
char code=0 boolean=false
String=null