Wednesday, August 25, 2010

Session 4: More on data types - primitives

Java is a statically typed language, which means you have to declare the variable before defining/using it. There is a difference between declaration and definition of a variable.

Take the following case:
//declaration
Object object;
//definition
object = new Object();

Unless you define a variable using it may result in a null pointer exception.

There are broadly 2 types of variables.
Primitives and Object variables.

Primitives are a group of commonly used datatypes provided in java.
8 types of primitives(size defined in bits) are listed.
byte(8), short(16), int(32), long(64), float(32), double(64), boolean(1) and char(16).

int i = 10;

There are object wrappers in Java for these primitives .

Integer integer = new Integer(10);
int i = integer.intValue();

Since numerical variables are used very often, using them as primitives provides greater performance benefit compared to object wrappers.

0 Comments:

Post a Comment

<< Home