Command Palette

Search for a command to run...

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

Every numeric type's size and range

3.4028235E38 is scientific notation: 3.4028235 × 10³⁸.

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?

Every numeric type's size and range
Sign in to run this example in your browser.

Expected output

byte   8 bits  -128 to 127
short  16 bits  -32768 to 32767
int    32 bits  -2147483648 to 2147483647
long   64 bits  -9223372036854775808 to 9223372036854775807
char   16 bits  0 to 65535
float  32 bits  max 3.4028235E38
double 64 bits  max 1.7976931348623157E308