Wednesday, October 28, 2009

Data types of Java

Java has 8 Primitive data types

A. Integer Types (All signed)

1. byte 8 bit
2. short 16 bit
3. int 32 bit
4. long 64 bit

B. Floating Point Types (All signed)

5. float 32 bit
6. double 64 bit

C. Boolean Type

7. boolean true / false

D. Char Type (unsigned)

8. char 16bit









Literal

Literal is a primitive typed values that appears directly inside a program.

int i = 20;
long l = 1000000;
double d = 5.0;

Floating point literals (float & double) are written with a decimal point. By default, a floating-point literal is treated as a double type.

class test
{
public static void main (String args[])
{
float i = 9.8;

}
}

Result of compilation:

G:\work>javac test.java
test.java:5: possible loss of precision
found : double
required: float
float i = 9.8;

class test
{
public static void main (String args[])
{
float i = 9.8F; // F or f

}
}


OK to compile

class test
{
public static void main (String args[])
{
int i = 2200;
int hex = 0x2200; // Starts with 0x
int oct = 02200; // Starts with 0, Binary literal not
// possible
long l = 1000000L;
boolean done = true;
char cr = 'A';
String str = "Abc";

if ( hex > 0x2200 ) { // Do something }

if (done) { // Do something }

if ( cr == 'B') { // Do something }
}
}