Java Data Types

Complete guide to Java primitive data types with examples. Learn about int, char, boolean, double, float, byte, short, and long data types.

As the name suggests, data types specify the type of data that can be stored inside variables in Java.

Java is a statically-typed language. This means that all variables must be declared before they can be used.

int speed;

Here, speed is a variable, and the data type of the variable is int. The int data type determines that the speed variable can only contain integers.

There are 8 data types predefined in the Java programming language, known as primitive data types.


Summary of Primitive Data Types

TypeSizeDefault ValueRangeDescription
byte1 byte0-128 to 127Saves memory in large arrays
short2 bytes0-32,768 to 32,767Also saves memory
int4 bytes0-2^31 to 2^31-1Default for whole numbers
long8 bytes0L-2^63 to 2^63-1For very large numbers
float4 bytes0.0fUnlimitedSingle precision floating point
double8 bytes0.0dUnlimitedDouble precision floating point
boolean1 bitfalsetrue or falseFor flags and logic
char2 bytes'\u0000'0 to 65,535Single Unicode character

1. Boolean Type

The boolean data type has two possible values: true or false. They are usually used for true/false conditions.

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun); // Outputs true
System.out.println(isFishTasty); // Outputs false

2. Byte Type

The byte data type can have values from -128 to 127 (8-bit signed two's complement integer). It is used to save memory in large arrays, where the memory savings actually matters.

byte myByte = 100;
System.out.println(myByte);

3. Short Type

The short data type can have values from -32768 to 32767 (16-bit signed two's complement integer).

short myShort = 5000;
System.out.println(myShort);

4. Int Type

The int data type is the default data type for integral values unless there is a problem with memory.

int myInt = 100000;
System.out.println(myInt);

What is the default value of an int variable in Java?


5. Long Type

The long data type is used when you need a range of values wider than those provided by int.

Note: You must end the value with an "L".

long myLong = 15000000000L;
System.out.println(myLong);

6. Float Type

The float data type is a single-precision 32-bit floating-point. It should never be used for precise values such as currency.

Note: You must end the value with an "f".

float myFloat = 5.75f;
System.out.println(myFloat);

7. Double Type

The double data type is a double-precision 64-bit floating-point. It is the default type for decimal numbers.

double myDouble = 19.99d;
System.out.println(myDouble);

8. Char Type

The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c'.

char myGrade = 'B';
System.out.println(myGrade);

Alternatively, you can use ASCII values to display certain characters:

char myVar1 = 65, myVar2 = 66, myVar3 = 67;
System.out.println(myVar1); // A
System.out.println(myVar2); // B
System.out.println(myVar3); // C

Key Takeaways

  • 8 Primitives: boolean, byte, short, int, long, float, double, char.
  • Default Types: int is the default for whole numbers, double for decimals.
  • Suffixes: Use L for long (e.g., 100L) and F for float (e.g., 3.14F).

Common Pitfalls

[!WARNING] Integer Division: Dividing two integers results in an integer (e.g., 5 / 2 = 2). Use 5.0 / 2 to get 2.5.

[!WARNING] Overflow: Storing a value larger than a type can handle (like putting 130 in a byte) causes overflow and unexpected negative numbers.

Challenge

Challenge

Task:

Declare a double variable named 'price' with value 99.99 and print it.