There are two data types in JAVA. Lets take a graphical representation to understand it.
Lets take graphical representation to understand the primitive data type memory required , value holding capacity range and their default value.
Lets take a example to understand the primitive data type:
1) boolean primitive data type example:
package primitiveDataType;
/**
*
* boolean is a primitive data type and it takes 1 bits of memory
* Its default value is false
*/
public class BooleanPrimitiveDataType {
public static void main(String[] args) {
boolean booleanVariable=true;
System.out.println("Boolean value is : "+booleanVariable);
}
}
2) byte primitive data type example:
package primitiveDataType;
/**
*
* byte is a primitive data type and takes 8 bits(1 byte) of memory
* Its value-range lies between -128 to 127 (inclusive)
* Its minimum value is -128 and maximum value is 127
* Its default value is 0
*
*/
public class BytePrimitiveDataType {
public static void main(String[] args) {
byte byteVariable=127;
System.out.println(byteVariable);
byteVariable='p';
System.out.println((char)byteVariable);
}
}
3) char primitive data type example:
package primitiveDataType;
/**
*
* short is a primitive data type and takes 16 bits(2 byte) of memory
* Its value range lies between
* '\u0000' (or 0) to '\uffff' (or 65,535 inclusive)
* Its default value is '\u0000'
*
*/
public class CharPrimitiveDataType {
public static void main(String[] args) {
char name='p';
System.out.println(name);
}
}
4) short primitive data type example:
package primitiveDataType;
/**
*
* short is a primitive data type and takes 16 bits(2 byte) of memory
* Its value-range lies between -32,768 to 32,767 (inclusive).
* Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.
*
*/
public class ShortPrimitiveDataType {
public static void main(String[] args) {
//It will accept only max value 32767
short shortVariable=32767;
System.out.println(shortVariable);
shortVariable=-32768;
System.out.println(shortVariable);
}
}
5) int primitive data type example:
package primitiveDataType;
/**
*
* short is a primitive data type and takes 32 bits(4 byte) of memory
* Its value range lies between - 2,147,483,648
* to 2,147,483,647 (inclusive)
* Its minimum value is -32,768 and maximum value is 32,767
* Its default value is 0
*
*/
public class IntPrimitiveDataType {
public static void main(String[] args) {
/**
* It will accept only max value 2,147,483,647,
* if you will try to put above the mentioned range
* it will throw error
*/
int intVariable=2147483647;
System.out.println("Max acceptable value is "+intVariable);
intVariable=-2147483648;
System.out.println("Min acceptable value is :"+intVariable);
}
}
6) long primitive data type example:
package primitiveDataType;
/**
*
* short is a primitive data type and takes 64 bits(8 byte) of memory
* Its value range lies between
* -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807(inclusive)
* Its minimum value is -9,223,372,036,854,
* 775,808 and maximum value is 9,223,372,036,854,775,807
* Its default value is 0L
*
*/
public class LongPrimitiveDataType {
public static void main(String[] args) {
/**
* It will accept only max value 9,223,372,036,854,775,807,
* if you will try to put above the mentioned
* range it will throw error
* All literal numbers in java are by default ints,
* which has range -2147483648 to 2147483647 inclusive.
* Your literals are outside this range,
* so to make this compile you need to indicate they're
* long literals (ie suffix with L):
* You must use L to say to the compiler that it is a long literal.
*/
long longVariable=9223372036854775807L;
System.out.println("Max acceptable value is "+longVariable);
longVariable=-9223372036854775808L;
System.out.println("Min acceptable value is :"+longVariable);
}
}
7) float primitive data type example:
package primitiveDataType;
/**
*
* float is a primitive data type and takes 32 bits(4 byte) of memory
* The smallest decimal is 1.40239846 x 10^-45,
* and the largest value is 3.40282347 x 10^38.
* Float range is much difference than the other primitive data type.
* Its default value is 0.0f
*/
public class FloatPrimitiveDataType {
public static void main(String[] args) {
float floatVariable=9223.5f;
System.out.println("Float value is : "+floatVariable);
}
}
8) double primitive data type example:
package primitiveDataType;
/**
*
* double is a primitive data type and takes 64 bits(8 byte) of memory
* The range is 4.9406564584124654 x 10^-324
* to 1.7976931348623157 x 10^308. That range can
* also be positive or negative.
* Its default value is 0.0d
*/
public class DoublePrimitiveDataType {
public static void main(String[] args) {
double doubleVariable=9223.50006d;
System.out.println("Double value is : "+doubleVariable);
}
}
Note : You can download full source code from git repository https://github.com/firststepitsolution/java-core