Primitive variables local variables will store in stack.
Datatypes defines how to store variables value in stack memory.
DataType Explanation byte The memory size allocated to byte is 8 bits. range from -128 to 127 (total of 256 numbers) useful when working with a stream of data from a network or file. Ex. uploading large file on server byte by byte. declaration - byte b, c; int Range from -2,147,483,648 to 2,147,483,647 Represents a total of 4,294,967,296 numbers. A 32-bit (4-byte) integer value short Memory size allocated to short is 16 bits. default value for short is zero -'0 Range -32768 to -32767 long A 64-bit (8-byte) integer value float A 32-bit (4-byte) floating-point value declaration - float a = 2.45f; double A 64-bit (8-byte) floating-point value char Memory size allocated to char is 16 bits. default value for char is blank space ('\u0000'). declaration - char b ='z'; boolean A true or false value Default value for boolean is 'false'. declaration - boolean a =true;
public class Datatype_Default_Value { static boolean bool; static byte by; static char ch; static double d; static float f; static int i; static long l; static short sh; static String str; public static void main(String[] args) { System.out.println("bool = " + bool); System.out.println("by = " + by); System.out.println("ch = " + ch); System.out.println("d = " + d); System.out.println("f = " + f); System.out.println("i = " + i); System.out.println("l = " + l); System.out.println("sh = " + sh); System.out.println("str = " + str); } }
bool = false by = 0 ch = d = 0.0 f = 0.0 i = 0 l = 0 sh = 0 str = null
With the use of Wrapper class in java we can convert primitive datatypes into object and object into primitive datatypes.
Java is an object-oriented language and take everything as an object. The primitive data types are not objects they do not belong to any class.
Whenever, the data type is required as an object then we will use wrapper classes to convert primitive datatypes into object and object into primitive datatypes.
public class Wrapper_Classes { public static void main(String[] argv) { Boolean refBoolean = new Boolean(true); // return primitive boolean value of Boolean object boolean bool = refBoolean.booleanValue(); System.out.println("bool = " + bool); Byte refByte = new Byte((byte) 123); // return primitive byte value of Byte object byte b = refByte.byteValue(); System.out.println("b = " + b); Character refChar = new Character('x'); // return value of Character object char c = refChar.charValue(); System.out.println("c = " + c); Short refShort = new Short((short) 123); // return primitive sort value of Short object short s = refShort.shortValue(); System.out.println("s = " + s); Integer refInt = new Integer(123); // return primitive int value of Integer object int i = refInt.intValue(); System.out.println("i = " + i); Long refLong = new Long(123L); // return primitive long value of Long object long l = refLong.longValue(); System.out.println("l = " + l); Float refFloat = new Float(12.3F); // return primitive float value of Float object float f = refFloat.floatValue(); System.out.println("f = " + f); Double refDouble = new Double(12.3D); // return primitive double value of Double object double d = refDouble.doubleValue(); System.out.println("d = " + d); } }
bool = true b = 123 c = x s = 123 i = 123 l = 123 f = 12.3 d = 12.3
Showing java primitive datatypes minimum and maximum values.
public class Min_Max_Value_Datatypes { public static void main(String[] args) { System.out.println("Byte.MIN = " + Byte.MIN_VALUE); System.out.println("Byte.MAX = " + Byte.MAX_VALUE); System.out.println("Short.MIN = " + Short.MIN_VALUE); System.out.println("Short.MAX = " + Short.MAX_VALUE); System.out.println("Integer.MIN = " + Integer.MIN_VALUE); System.out.println("Integer.MAX = " + Integer.MAX_VALUE); System.out.println("Long.MIN = " + Long.MIN_VALUE); System.out.println("Long.MAX = " + Long.MAX_VALUE); System.out.println("Float.MIN = " + Float.MIN_VALUE); System.out.println("Float.MAX = " + Float.MAX_VALUE); System.out.println("Double.MIN = " + Double.MIN_VALUE); System.out.println("Double.MAX = " + Double.MAX_VALUE); } }
Byte.MIN = -128 Byte.MAX = 127 Short.MIN = -32768 Short.MAX = 32767 Integer.MIN = -2147483648 Integer.MAX = 2147483647 Long.MIN = -9223372036854775808 Long.MAX = 9223372036854775807 Float.MIN = 1.4E-45 Float.MAX = 3.4028235E38 Double.MIN = 4.9E-324 Double.MAX = 1.7976931348623157E308