Java Series Part 3 – Variable/Data Types

Primitive Data Types

Data types that can store integer values:

int i;
byte b;
short s;
long l;

Variable types:

byte b = 5; //8bits – 128 to 127
short s = 128 //16bits – 32,768 to 32,767
int i = 40000; //32 bits – 2,147,483,648 to 2,147,483,647
long l = 222222222; //64bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float f = 4.0f; //32 bits NOT very precise, don’t use for financials
double d = 67.0 //64bits NOT very precise, don’t use for financials
char c = ‘A’; //16bits ‘\u0000’ to ‘\uffff’
boolean isPrime = false; //true or false

For example:

Example 1:
To store number of goals in a football match we can use either byte or short data type and declare it as below:
short numberOfGoals;

Example 2: To store population of world, use long data type
long population;

Example 3: To store average of two number, use double for all decimal operation
double average;

Example 4: To store Grades of students, use char data type
char grade;
grade = 'A'

Example 5: To check whether a number is even or odd, use boolean type
boolean isEven;
isEven = false;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s