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 typelong population;
Example 3: To store average of two number, use double for all decimal operationdouble average;
Example 4: To store Grades of students, use char data typechar grade;
grade = 'A'
Example 5: To check whether a number is even or odd, use boolean typeboolean isEven;
isEven = false;