Primitive Data Types
| Data Type | Size | Default value for fields |
|---|---|---|
| byte | 1 byte | 0 |
| short | 2 bytes | 0 |
| int | 4 bytes | 0 |
| long | 8 bytes | 0L |
| float | 4 bytes | 0.0f |
| double | 8 bytes | 0.0d |
| boolean | Implementation-dependent | false |
| char | 2 bytes | '\u0000' |
Java is statically typed. Its eight primitive types store numeric, boolean, or character values. Reference types include classes, interfaces, and arrays.
Tell a number from text
class Main {
public static void main(String[] args) throws Exception {
System.out.println(2 + 2);
System.out.println("2" + "2");
}
}
4 22
A school shop sells three packs of paper at 125 cents each. Its scanner must print the code 007 exactly as it appears on the shelf. Keep the code as text and multiply the numeric quantity and price.
| Data Type | Size | Default value for fields |
|---|---|---|
| byte | 1 byte | 0 |
| short | 2 bytes | 0 |
| int | 4 bytes | 0 |
| long | 8 bytes | 0L |
| float | 4 bytes | 0.0f |
| double | 8 bytes | 0.0d |
| boolean | Implementation-dependent | false |
| char | 2 bytes | '\u0000' |
Fields and array elements receive default values. A local variable must be definitely assigned before it is read.
| Data Type | Size | Default value for fields |
|---|---|---|
| String | ~ | null |
| array | ~ | null |
A byte is a signed 8-bit integer with values from −128 to 127.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type byte
// Minimum value is -128 and Maximum value is 127
byte x = 127;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 127
class DataTypes {
// Instance variable declaration
private byte num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0
A short is a signed 16-bit integer with values from −32,768 to 32,767.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type short
// Minimum value is -32,768 and Maximum value is 32,767
// Use the underscore as a separator if required
short x = 32_767;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 32767
class DataTypes {
// Instance variable declaration
private short num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0
An int is a signed 32-bit integer with values from −2³¹ to 2³¹ − 1.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type int
// Minimum value is -2,147,483,648 and Maximum value is 2,147,483,647
// Use the underscore as a separator if required
int x = 2_147_483_647;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 2147483647
class DataTypes {
// Instance variable declaration
private int num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0
A long is a signed 64-bit integer with values from −2⁶³ to 2⁶³ − 1.
Append L to a decimal integer literal that needs the long type, as in 9223372036854775807L.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type long
// Minimum value is -9,223,372,036,854,775,808 and Maximum value is 9,223,372,036,854,775,807
// Use the underscore as a separator if required
long x = 9_223_372_036_854_775_807L;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 9223372036854775807
class DataTypes {
// Instance variable declaration
private long num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0
A double is a 64-bit IEEE 754 double-precision floating-point type. Decimal floating-point literals have this type by default.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type double
// Use the underscore as a separator if required
double x = 6.1234567890_12345;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 6.123456789012345
class DataTypes {
// Instance variable declaration
private double num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0.0
A float is a 32-bit IEEE 754 single-precision floating-point type. Its precision is lower than that of a double.
Append F to write a float literal, such as 6.123456F. An explicit cast from double can lose precision.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type float
// Use the underscore as a separator if required
float x = 6.123_456f;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is 6.123456
class DataTypes {
// Instance variable declaration
private float num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of num is " + obj.num);
}
}
The value of num is 0.0
A boolean holds true or false. Java specifies these two values and leaves their storage size to the implementation.
Boolean fields and boolean array elements default to false.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type boolean
boolean x = true;
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is true
class DataTypes {
// Instance variable declaration
private boolean flag;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value 0
System.out.println("The value of flag is " + obj.flag);
}
}
The value of flag is false
A char holds one unsigned 16-bit UTF-16 code unit, with 65,536 possible values. Some Unicode characters require two code units.
Enclose a char literal in single quotes, as in 'B'.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type char
char x = 'B';
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is B
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of data type char
// Minimum value is '\u0000' and Maximum value is '\uffff'
// Unicode assignment '\u0042'
char x = '\u0042';
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is B
class DataTypes {
// Instance variable declaration
private char letter;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value '\u0000'
System.out.printf("The code unit of letter is U+%04X%n", (int) obj.letter);
}
}
The code unit of letter is U+0000
String is a reference type. A String object holds an immutable sequence of UTF-16 code units.
Enclose a string literal in double quotes, as in "Hello World".
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of object type String
String x = "Hello World";
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x is " + x);
}
}
The value of x is Hello World
class DataTypes {
// Instance variable declaration
private String firstName;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value null
System.out.println("The value of firstName is " + obj.firstName);
}
}
The value of firstName is null
An array is a fixed-length object whose elements share a component type. Indices start at zero.
Give the length when allocating an array by size, or use an initializer to supply its elements.
class DataTypes {
public static void main(String[] args) {
// Declare and Initialize the local variable x of object type String
int[] x = { 1, 2, 3 };
// The local variable x should be initialized before usage
// otherwise it will throw an compile time error
System.out.println("The value of x[0] is " + x[0]);
System.out.println("The value of x[1] is " + x[1]);
System.out.println("The value of x[2] is " + x[2]);
}
}
The value of x[0] is 1 The value of x[1] is 2 The value of x[2] is 3
class DataTypes {
// Instance variable declaration
private int[] num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// By default the instance variable will take a default value null
System.out.println("The value of num is " + obj.num);
}
}
The value of num is null
class DataTypes {
// Instance variable declaration
private int[] num;
public static void main(String[] args) {
// Declaration of a local variable 'obj' of type 'DataTypes'
DataTypes obj = new DataTypes();
// Initialize an array size by 3
// i.e. it will hold only 3 values
obj.num = new int[3];
// Assign the values
obj.num[0] = 1;
obj.num[1] = 2;
obj.num[2] = 3;
// By default the instance variable will take a default value null
System.out.println("The value of num[0] is " + obj.num[0]);
System.out.println("The value of num[1] is " + obj.num[1]);
System.out.println("The value of num[2] is " + obj.num[2]);
}
}
The value of num[0] is 1 The value of num[1] is 2 The value of num[2] is 3
Reference: Java language reference