Command Palette

Search for a command to run...

Back to the lesson: Topic 1.2 — The Eight Primitive Types
Core Java · Example 2 of 3

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.

01

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.

02

Print how many bytes one million values of byte, int, long and double would need in an array, using the BYTES constants.

03

Print one third as a float and as a double and count how many correct digits each shows.

Explain it without notes

01

Name the eight primitive types with their sizes, and say which is the default for whole numbers and which for decimals.

02

Why does long x = 3000000000; fail to compile even though 3 billion fits in a long?

03

Why does Java have primitives at all, if everything else is an object?

04

Which values do fields and array elements get by default, and why don't local variables get them?

Default values of fields
Sign in to run this example in your browser.

Expected output

byte=0 short=0 int=0 long=0
float=0.0 double=0.0
char code=0 boolean=false
String=null