Primitive Data Types:
There are total eight primitive data types in java.
1. boolean
2. byte
3. char
4. short
5. int
6. long
7. float
8. double
All the 8 data types can be considered as building blocks of data manipulation.
Java Package: - A package is a way to organize your java projects.
for all primitive data type we have corresponding class is known as Wrapper class.
Integer is a Wrapper class .
Overflow and Underflow in Java:
If you try to put a value larger than the maximum value in java or, a value smaller than the minimum value in java , than you will get Overflow in the case of maximum vale or Underflow in the case of minimum.
What Happens in background during Overflow or, Underflow in Java:
The computer just skips back to the minimum number or the maximum number , which is not usually not what you want . it's an important concept to be aware of.
Program:
public class PrimitiveDataType {
public static void main(String[] args) {
int myValue = 100;
int minIntValue = Integer.MIN_VALUE;
int maxIntValue = Integer.MAX_VALUE;
System.out.println("Ingeger min value = " + minIntValue);
System.out.println("Integer max value = " + maxIntValue);
//add +1 to max value and -1 to min value
System.out.println("Add 1 to max value = "+maxIntValue+1); //overflow will happen
System.out.println("subs 1 to min value = "+minIntValue-1);// unnderflow will happen
int maxintvalue=2147483648;// will get compile time error saying that The literal 2147483648 of type int is out of range
int maxvalue=2_147_483_647;// will not give any error
}
}
byte Data type:-
program:
public class byteDataType {
public static void main(String[] args) {
byte minByteval = Byte.MIN_VALUE;
byte maxByteVal = Byte.MAX_VALUE;
System.out.println("Byte min value = " + minByteval);
System.out.println("Byte max value = " + maxByteVal);
}
}
long Data type:
There are total eight primitive data types in java.
1. boolean
2. byte
3. char
4. short
5. int
6. long
7. float
8. double
All the 8 data types can be considered as building blocks of data manipulation.
Java Package: - A package is a way to organize your java projects.
for all primitive data type we have corresponding class is known as Wrapper class.
Integer is a Wrapper class .
Overflow and Underflow in Java:
If you try to put a value larger than the maximum value in java or, a value smaller than the minimum value in java , than you will get Overflow in the case of maximum vale or Underflow in the case of minimum.
What Happens in background during Overflow or, Underflow in Java:
The computer just skips back to the minimum number or the maximum number , which is not usually not what you want . it's an important concept to be aware of.
Program:
public class PrimitiveDataType {
public static void main(String[] args) {
int myValue = 100;
int minIntValue = Integer.MIN_VALUE;
int maxIntValue = Integer.MAX_VALUE;
System.out.println("Ingeger min value = " + minIntValue);
System.out.println("Integer max value = " + maxIntValue);
//add +1 to max value and -1 to min value
System.out.println("Add 1 to max value = "+maxIntValue+1); //overflow will happen
System.out.println("subs 1 to min value = "+minIntValue-1);// unnderflow will happen
int maxintvalue=2147483648;// will get compile time error saying that The literal 2147483648 of type int is out of range
int maxvalue=2_147_483_647;// will not give any error
}
}
byte Data type:-
program:
public class byteDataType {
public static void main(String[] args) {
byte minByteval = Byte.MIN_VALUE;
byte maxByteVal = Byte.MAX_VALUE;
System.out.println("Byte min value = " + minByteval);
System.out.println("Byte max value = " + maxByteVal);
}
}
short data type:
program:
public class shortDatType {
public static void main(String[] args) {
short minShortval = Short.MIN_VALUE;
short maxShortval = Short.MAX_VALUE;
System.out.println("Min short value =" + minShortval);
System.out.println("Max short value =" + maxShortval);
}
}
Size of primitive Types and width:
A Byte occupies 8 bits. A bit is not directly represented in a primitive type - we have a boolean which is not really the same thing .
Hence a Byte occupies 8 bit . we can say that a byte has a width of 8.
A short can store a large range of number and occupies 16 bits, and has width of 16.
An int has a much larger range as we know, and occupies 32 bits and has a width of 32.
NOTE: Each primitive type occupies different amount of memory - we can see that an int needs four times the amount of space, than a byte .
long Data type:
No comments:
Post a Comment