Monday, 19 March 2012

Basic Data Types


Basic data types:

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java:
1.    Primitive Data Types
2.    Reference/Object Data Types

Primitive Data Types:

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the eight primitive data types.
·        Byte
·        Short
·        Int
·        Long
·        float
·        double
·        Boolean
·        Char

Reference Data Types:

·         Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.
·         Class objects, and various type of array variables come under reference data type.
·         Default value of any reference variable is null.
·         A reference variable can be used to refer to any object of the declared type or any compatible type.
·         Example : Animal animal = new Animal("giraffe");

Java Literals:

A literal is a source code representation of a fixed value. They are represented directly in the code without any computation.
Literals can be assigned to any primitive type variable.
Example:
byte a=65;
char a=’A’;

No comments:

Post a Comment