Ad Code

Data Types in Java Part - 1


 

Data types in java 

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)



4)int

It is 32 bit signed two's complement integer

Syntax:
int a;

Example:

public class IntDatatype{
public static void main(String[] args){
int a = 20;
System.out.println("a value declared:"+a);
}
}

output:

a value declared:20
Values Allowed:-2, 147, 483, 648 to 2, 147, 483, 647
Default Value: 0
Type conversion: Allowed
Size: 4 bytes(32 bits)

5)long

long data type is a 64 bit works on two's complement integer

Syntax:

long var_name;
Example:
public class LongDataType{
public static void main(String[] args){
long a = 754574;
System.out.println("a value declared:"+a);
}
}

output:

a value declared:754574

Values Allowed: -9, 223, 372, 036, 854, 775, 808 to 9, 223, 372, 036, 854, 775, 807
Default Value:0
Type Conversion: Allowed
Size:8 Byte(64 bits)

6)Float

Float Data type in java is a 32-bit IEEE works on 754 floating point , used to store all floating numbers

Syntax:

float var_name;

Example:

public class FloatDataType{
public static void main(String[] args){
float f = 34.5f;
System.out.println("f value declared:"+f);
}
}

output:
x value declared:34.5

values Allowed: up to 7 digits
Default value: 0.0
Type conversion: Allowed
Size:4 byte(32 bits)

7)Double
double data type in java takes 64 bit IEEE 754 floating point value.

Syntax:

double var_name;

Example:

public class DoubleDataType{
public static void main(String[] args){
double d = 56.78965;
System.out.println("d value declared:"+d);
}
}

output:
d value declared: 56.78965
values Allowed: up to 16 decimal digits
Default value:0.0
Type Conversion: Allowed
Size:8 byte(64 bits)

8)char 
character data type takes a 16-bit Unicode Character  

Syntax:

char var_name;

Example:

public class CharacterDataType{
public static void main(String[] args){
char c = 'a';
System.out.println("Taken Character ":+c);
}
}

output:

Taken Character :a
Values Allowed:‘\u0000’ (0) to ‘\uffff’ (65535)
Default value:\u0000
Type Conversion:Allowed
Size: 2 byte(16 bits)

 

Ad Code

Responsive Advertisement