Back to: Java Tutorials
Datatype refers to the kind of data. For example, a decimal number, a number without a decimal number, just a latter (Character) or a full word (String) or a whole writing. Datatype is mainly divided into two kinds in Java:
1) primitive data types
2) non primitive data types
Primitive Data Types:
This data type different other types as follows:
1) Boolean (true / false):
Default (meaning if no value is assigned) value: false
Default size: 1 bit
Example: boolean flag = true;
2) Character data type:
minimum / default value: ‘u0000’ (or 0).
maximum value: ‘ff uffff’ (or 65,535 inclusive).
size: 2 bytes
example: char a = ‘a’;
3) Byte:
minimum value: -128 (or -2 ^ 7)
maximum value: 127 (or 2 ^ 7 -1)
default value: 0
size: 1byte
example: byte b = 100;
4) Short:
minimum value: -32,768 (or -2 ^ 15)
maximum value: 32,767 (inclusive) (or 2 ^ 15 -1)
default value: 0.
size: 2 bytes
example: short s = 20000, short t = -20000
5) Integer:
minimum value: – 2,147,483,648. (or – 2 ^ 31)
maximum value: 2,147,483,647 (or 2 ^ 31 -1)
default value: 0.
size: 4 bytes
example: int num1 = 20000; int num2 = -20000;
6) Long:
minimum value: -9,223,372,036,854,775,808. (or – 2 ^ 63)
maximum value: 9,223,372,036,854,775,807 (or 2 ^ 63 -1)
default value: 0L.
size: 8 bytes
example: long num1 = 200000L (or long num = 20000;); long num2 = -200000L;
B: If we have to use any big data of integer type, then we can use “long” datatype.
7) Float (decimal number):
Size: 4 bytes
Default value: 0.0f.
Example: float num = 10.21f; (Or just float num = 10.21;)
8) Double (decimal number):
Size: 8 bytes
Default value is 0.0d.
Example: double num = 10000000001.10000;
Non-primitive / Reference data types:
1) Class objects,
2) String and
3) Array variables
These three types are called reference data type or non-primitive datatype.
Example of Reference datatype:
public class MyClass {
String str = "This is a string"; // String type
Int [] numbers = 1, 2, 3, 3; // array type
public static void main (String arg []) {
MyClass obj = new MyClass (); // obj is the object of MyClass. So obj is a reference variable
//printing the string
System.out.println("Our string value is: " + str);
}
}
We will see these reference datatypes in their individual chapters.
Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.
Follow us on social media
Follow Author