Primitive Data Types
Data Type | Size | Default Value (for fields / instance variable) |
---|---|---|
byte | 1 byte | false |
short | 2 byte | 0 |
int | 4 byte | 0 |
long | 8 byte | 0L |
float | 4 byte | 0.0f |
double | 8 byte | 0.0d |
boolean | 1 bit | false |
char | 2 byte | '\u0000' |
Java programming language is a strictly typed and the variable should be declared with an appropriate data type before the usage.
Java provides Primitive data types like byte, short,
int, long, float,
double, boolean, char.
String is a special data type to hold a sequence of char and
the value should be enclosed within double quotes (i.e. "Hello World") instead of single quotes.
Java also provides the non-primitive data types like class, interface,
array, etc.
Data Type | Size | Default Value (for fields / instance variable) |
---|---|---|
byte | 1 byte | false |
short | 2 byte | 0 |
int | 4 byte | 0 |
long | 8 byte | 0L |
float | 4 byte | 0.0f |
double | 8 byte | 0.0d |
boolean | 1 bit | false |
char | 2 byte | '\u0000' |
Note: The following list contains the commonly used predefined data types. The data types like class, interface, enum, etc., can be explained in details in the later session.
Data Type | Size | Default Value (for fields / instance variaable) |
---|---|---|
String | ~ | null |
array | ~ | null |
The data type byte is an 8-bit signed 2's complement integer. Its value ranges from -128 to 127 and its default value is 0. Byte accepts underscore _ as a separator, but it should be used in-between the digits.
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);
}
}
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 data type short is an 16-bit signed 2's complement integer. Its value ranges from -32,768 to 32,767 and its default value is 0. short excepts underscore _ as a separator, but it should be used in-between the digits.
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);
}
}
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 data type int is an 32-bit signed 2's complement integer. Its value ranges from -231 to 231-1 (i.e. -2,147,483,648 to 2,147,483,647) and its default value is 0. It accepts underscore _ as a separator but it should be used in-between the digits.
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);
}
}
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 data type long is an 64-bit signed 2's complement integer. Its value ranges from -263 to 263-1 (i.e. -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) and its default value is 0. It accepts underscore _ as a separator but it should be used in-between the digits.
Note: It requires the letter L or l (i.e. 9223372036854775807L) at the end of the digits to denote the value as long otherwise it will throw a compile time error The literal 9223372036854775807 of type int is out of range.
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);
}
}
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 data type double is a single-precision 64-bit IEEE 754 floating point. It accepts underscore _ as a separator but it should be used in-between the digits.
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);
}
}
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 data type float is a single-precision 32-bit IEEE 754 floating point. It accepts underscore _ as a separator but it should be used in-between the digits.
Note: It requires the letter F or f (i.e. 6.123456F) at the end of the digit to denote the value as float otherwise needs to parse the value explicitly (i.e. (float) 6.123456). So the assignment should be either of the ways otherwise it will throw a compile time error Type mismatch: cannot convert from double to float.
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);
}
}
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 data type boolean has only two possible values true and false (i.e. 1 and 0). So it uses only one bit to store the value in memory. Use this data type to assign simple flag value which holds true / false.
Note: The Default value of boolean variable is 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);
}
}
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 data type char is an 16-bit Unicode character. Its value ranges from '\u0000' to '\uffff' (i.e. it contains totally 65,535 chars) and its default value is '\u0000'.
Note: char variable should be enclosed within a single quotes (i.e. 'B' or '\u0042')
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);
}
}
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);
}
}
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.println("The value of letter is " + obj.letter);
}
}
Java also provides the support for character strings via the package class java.lang.String and enclosing your character string within double quotes will automatically create a new String object. String objects are immutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type.
Note: The String should be enclosed within a double quotes (i.e. "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);
}
}
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);
}
}
Java array is an object which stores multiple values of a same data type in a single variable. Array elements can be accessed through an numerical index and its starting value is zero. Java also supports the multi-dimensional array.
Note: The length of the array should be mentioned when you create the object of an array using new keyword.
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]);
}
}
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);
}
}
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]);
}
}