Java Data Types
Java programming language has a rich set of data types. The data type is a category of data stored in variables. In java, data types are classified into two types and they are as follows.
- Primitive Data Types
- Non-primitive Data Types
Primitive Data Types
The primitive data types are built-in data types and they specify the type of value stored in a variable and the memory size. The primitive data types do not have any additional methods.
In java, primitive data types includes byte, short, int, long, float, double, char, and boolean.
The following table provides more description of each primitive data type.
Data type | Meaning | Memory size | Range | Default Value |
---|---|---|---|---|
byte | Whole numbers | 1 byte | -128 to +127 | 0 |
short | Whole numbers | 2 bytes | -32768 to +32767 | 0 |
int | Whole numbers | 4 bytes | -2,147,483,648 to +2,147,483,647 | 0 |
long | Whole numbers | 8 bytes | -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 | 0L |
float | Fractional numbers | 4 bytes | - | 0.0f |
double | Fractional numbers | 8 bytes | - | 0.0d |
char | Single character | 2 bytes | 0 to 65535 | \u0000 |
boolean | unsigned char | 1 bit | 0 or 1 | 0 (false) |
Let's look at the following example java program to illustrate primitive data types in java and their default values.
public class PrimitiveDataTypes {
byte i;
short j;
int k;
long l;
float m;
double n;
char ch;
boolean p;
public static void main(String[] args) {
PrimitiveDataTypes obj = new PrimitiveDataTypes();
System.out.println("i = " + obj.i + ", j = " + obj.j + ", k = " + obj.k + ", l = " + obj.l);
System.out.println("m = " + obj.m + ", n = " + obj.n);
System.out.println("ch = " + obj.ch);
System.out.println("p = " + obj.p);
}
}
When we run the above example code, it produces the following output.
Non-primitive Data Types
In java, non-primitive data types are the reference data types or user-created data types. All non-primitive data types are implemented using object concepts. Every variable of the non-primitive data type is an object. The non-primitive data types may use additional methods to perform certain operations. The default value of non-primitive data type variable is null.
In java, examples of non-primitive data types are String, Array, List, Queue, Stack, Class, Interface, etc.
public class NonPrimitiveDataTypes {
String str;
public static void main(String[] args) {
String name = "BTech Smart Class!";
String wish = "Hello, ";
NonPrimitiveDataTypes obj = new NonPrimitiveDataTypes();
System.out.println("str = " + obj.str);
//using addition method
System.out.println(wish.concat(name));
}
}
When we run the above example code, it produces the following output.
Primitive Vs Non-primitive Data Types
Primitive Data Type | Non-primitive Data Type |
---|---|
These are built-in data types | These are created by the users |
Does not support additional methods | Support additional methods |
Always has a value | It can be null |
Starts with lower-case letter | Starts with upper-case letter |
Size depends on the data type | Same size for all |