Data Type

In previous tutorial we read about variables. We also know that variables have size and type. Now, what’s that? Why bother about type and size?

Java cares about type. It won’t allow you to do something bizarre and dangerous, like stuff a Chocolate reference into a Cold-drink Variable, or stuff an aircraft reference into a car garage.

Data type

For all these safety type to work, you must declare the type of your variable.

Variables must have a type and a size as we discussed in variables section.

There are 2 type of data types:

  1. Primitive Data type(8)
  2. Non-Primitive Data typeData type java

Table below shows the different primitive data type with it’s size and range.

Size, default value

In the above table in example column a,s are the variables and float, int, Boolean etc are the data types which has different sizes.

So, now you understood why you cannot put a large value into a small box due its type and size.

byte, short, int, long

Declaration & Initialization:

Every variable needs to be declared and initialized before using in the program. Declaration can be done only once( int a, int Age) while initialization can be done many times( int a = 25, int a = 4, int Age = 52, int Age = 39).

There are certain rules which must be followed.

  • Variable name must follow datatype.

Ex:    int a = 23;

  • Nothing except 0-9, A-Z, a-z, _, $ can be used in variable

Ex:    int student1_Age = 23, int $_age = 32;

  • No spaces or special characters are allowed.

Ex:    int #_a = 20,  int dog Age = 23. All these are invalid.

  • Variable name must not start with numbers and special character.

Ex:   int 9Age = 50;  This is invalid.

  • Cannot use a java keyword (reserved word) for a variable name. Below is some java reserved keyword. No need to learn just remember them.

java keyword, reserved keyword


Non-primitive data types (Reference):

  • Class objects and various type of array variables come under reference datatype.
  • They are defined by user using constructors of the classes.
  • They are used to access objects (We will learn about this in further tutorials).
  • Default value of reference variable is NULL.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s