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
| Type | Size | Default Value | Range | Description |
|---|---|---|---|---|
byte | 1 byte | 0 | -128 to 127 | Saves memory in large arrays |
short | 2 bytes | 0 | -32,768 to 32,767 | Also saves memory |
int | 4 bytes | 0 | -2^31 to 2^31-1 | Default for whole numbers |
long | 8 bytes | 0L | -2^63 to 2^63-1 | For very large numbers |
float | 4 bytes | 0.0f | Unlimited | Single precision floating point |
double | 8 bytes | 0.0d | Unlimited | Double precision floating point |
boolean | 1 bit | false | true or false | For flags and logic |
char | 2 bytes | '\u0000' | 0 to 65,535 | Single 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 false2. 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); // CKey Takeaways
- 8 Primitives:
boolean,byte,short,int,long,float,double,char. - Default Types:
intis the default for whole numbers,doublefor decimals. - Suffixes: Use
Lforlong(e.g.,100L) andFforfloat(e.g.,3.14F).
Common Pitfalls
[!WARNING] Integer Division: Dividing two integers results in an integer (e.g.,
5 / 2 = 2). Use5.0 / 2to get2.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.Java Variables and Literals
Learn Java variables and literals with examples. Understand variable declaration, initialization, naming rules, and all types of literals in Java programming.
Java Type Casting
Learn about Type Casting in Java with examples. Understand Widening Casting (Implicit) and Narrowing Casting (Explicit).
