Data Types in Java
Data types in Java are classified into two types :
1) Primitive data types
2) Reference data types
There are 8 primitive data types in java as :
1) boolean
2) char
3) int
4) float
5) long
6) double
7) byte
8) short
1) boolean
it is either true or false.
declaration : private boolean flag = true;
default value : false
2) byte
it contains 8-bit signed two's complement integer.
declaration : private byte b = 0;
default value : 0
3) short
it is 16-bits] signed two's complement integer.
declaration : private short s = 0;
default value : 0
4) int
it is 32-bit signed two's complement integer.
declaration : private int i = 0;
default value : 0
5) long
it is 64-bit signed two's complement integer.
declaration : private long l = 0;
default value : 0
6) char
it is single 16 bit unicode character.
declaration : private char c = 'a';
default value : '\u0000'
7) float
it is single precision 32 bit IEEE 754 floating point.
declaration : private float f = 1.8;
default value : 0.0f
8) double
it is double precision 64 bit IEEE 754 floating point.
declaration : private double d = 2.5;
default value : 0.0d
Table showing a brief of all primitive data types :
Data Type | Default value | Declaration Example |
boolean | false | private Boolean b = false; |
byte | 0 | private byte b = 0; |
short | 0 | private short s = 2; |
int | 0 | private int 1 = 5; |
long | 0 | private long l = 6l; |
char | \u0000 | private char c = ‘a’; |
float | 0.0f | private float f = 3.8; |
double | 0.0d | private double d = 3.6; |
Here is a simple program for printing values of all primitive data types :
package Hello;
public class TestPrimitiveDataTypes {
private static boolean b = false;
private static byte b1 = 1;
private static short s = 2;
private static int i = 3;
private static long l = 2l;
private static float f = 2.2f;
private static double d = 5.5d;
private static char c = 'a';
public static void main(String[] args) {
System.out.println("boolean data type : "+b);
System.out.println("byte data type : "+b1);
System.out.println("short data type : "+s);
System.out.println("int data type : "+i);
System.out.println("long data type : "+l);
System.out.println("float data type : "+f);
System.out.println("double data type : "+d);
System.out.println("char data type : "+c);
}
}
Output :
boolean data type : false
byte data type : 1
short data type : 2
int data type : 3
long data type : 2
float data type : 2.2
double data type : 5.5
char data type : a
Note: static is a keyword and we will discuss it in our later tutorials.
Reference Data Types
Reference data types refer to the memory address of the actual object in memory.
Reference data types can be categorized into two types:
1) Objects
2) Arrays
Object refers to a memory address. We will discuss objects in the oops concepts tutorial.
Suppose, there is a class TestRefDataTypes and an object and an instance are defined in the class.
Here object 'reftype' is a reference data type that refers to the address of instance 'a' created by class TestRefDataTypes .
package Hello;
public class TestRefDataTypes {
static TestRefDataTypes reftype;
static TestRefDataTypes a = new TestRefDataTypes();
public static void main(String[] args) {
reftype = a;
System.out.println(a);
System.out.println(reftype);
}
}
Output :
Hello.TestRefDataTypes@659e0bfd
Hello.TestRefDataTypes@659e0bfd
or if try to print hashcode values than output will be same.for printing hashcode just call hashcode method like :
System.out.println(a.hashCode());
System.out.println(reftype.hashCode());
both the hashcode will be the same
1704856573
1704856573
thus, we can see from the generated output, reftype points to the memory address of a.
if we remove the line
reftype = a;
from our code then the output will be :
Hello.TestRefDataTypes@659e0bfd
null
means, here reftype is null and not point to any memory address.
No comments:
Post a Comment