Java Data Types

 

In the previous tutorial we discussed variables and one of the 3 things a variable must have is a type or a data type which we will discuss here. Quick recap on variables:

public static void main(String[] args) {	
	String firstName = "Folau";
	System.out.println(firstName);// Folau	
}

String – data type
firstName – variable name
“Folau” = value

There are two types of data types:
1. Primitive data types – byte, short, int, long, float, double, boolean, and char
2. Non-Primitive or Object data types – String, Arrays, List, Classes(you will learn about this later)

Primitive Data Types
Primitive data types have size. Here is the 8 Java primitive data types.

Type Size Description
byte 1 byte Stores whole numbers from -128 to 127
short 2 bytes Stores whole numbers from -32,768 to 32,767
int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
float 4 bytes Stores fractional numbers from 3.4e−038 to 3.4e+038. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers from 1.7e−308 to 1.7e+038. Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 2 bytes Stores a single character/letter

String Data Type
String is almost a primitive data type because it is used everywhere in java. But it is not a primitive data type. String variables are references and have methods which are things primitive data types don’t have.

Non Primitive Data Types
Non primitive data types are references to objects in memory and they have methods or actions to trigger.

Difference between Primitive and Non-Primitive Data Types
1. Primitive data types are defined by Java like int or double. Non Primitives are created by developers
2. Primitive data types start with lowercase like int, long, double, etc. Non Primitive data types start with uppercase like String, User, etc.
3. Primitive data types always have values. Non Primitive data types can be null.
4. Primitive data types cannot be invoked to perform actions but primitive can. i.e user.sayHello();
4. Primitive data types cannot be invoked to perform actions but primitive can. i.e user.sayHello();

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.