Which are classified below as
1)Primitive Data types: Primitive data types are used for computation, storing a single without any additional features
8 primitive data types are available in java , given below
1)boolean
2)byte
3)short
4)int
5)long
6)float
7)double
8)char
2) Non primitive Data Types:
Non primitive Data Types are referred to objects , called as reference types will be defined by programmer
1)boolean:
Boolean data type resembles truth values of boolean algebra involving logical expression, allows only two values can be either true or false
Syntax:
boolean var_name;
Example:
class BooleanDatatype{
public static void main(String[] args){
boolean a = true , b = false;
if(a&&b){
System.out.println("boolean data type")
}
else{
System.out.println("code Executed")
}
}
}
output:
code Executed
Values Allowed: true, false
Default value: false
type conversion: Allowed
Size: Machine Dependent
2)byte
If the Programmer is using values declared in between range -128 to 127, then byte data type comes in to picture
Byte data types are 8-bit Signed Integer values, works on Two's complement integer
Syntax:
byte var_name;
Example:
class ByteDataType{
public static void main(String[] args){
byte x = 126;
System.out.println("x value:"+x);
}
}
output:
x value:126
If the literal assigned to byte data type identifier is not within range (-128 to 127) then an error is raised
Consider the below Code:
class ByteDataType{
public static void main(String[] args){
byte x = 128;
System.out.println("x value:"+x);
}
}
output:
Exception in thread "main" java. lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to byte
at Comment. main(Comment.java:3)
Values Allowed: Only within range(-128 to 127)
Default Value:0
Type conversion: Allowed
Size: 1 byte(8 bits)
3)short
If the Programmer is using values declared within the range (-32,768 to 32,767) inclusively then short comes in to the picture
short data type saves memory in a large collection of arrays, where memory especially matters
Syntax:
short var_name;
Example:
class ShortDataType{
public static void main(String[] args){
short x = 4678;
System.out.println("x value declared:"+x);
}
}
output:
x value declared:4678
if the literal assigned to the short identifier is not within range then errors are raised
class ShortDataType{
public static void main(String[] args){
short x = 32768;
System.out.println("x value declared:"+x);
}
}
output:
Exception in thread "main" java. lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to short
at Comment.main(Comment.java:3)
Social Plugin