JAVA

Datatypes in Java

Variables in Java

A variable is the name given to a memory location. It is the basic unit of storage in a program.

The value stored in a variable can be changed during program execution.

A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

In Java, all the variables must be declared before they can be used.

How to declare variables?
We can declare variables in java as follows:

datatype: Type of data that can be stored in this variable.
variable_name: Name given to the variable.
value: It is the initial value stored in the variable

Datatype in Java

Datatype is a spacial keyword used to allocate sufficient memory space for the data, in other words Data type is used for representing the data in main memory (RAM) of the computer.

In general every programming language is containing three categories of data types. They are

*    Fundamental or primitive data types

*     Derived data types

*     User defined data types.

Primitive data types

Primitive data types are those whose variables allows us to store only one value but they never allows us to store multiple values of same type. This is a data type whose variable can hold maximum one value at a time.

In java we have eight data type which are organized in four groups. They are

·        Integer category data types

·        Character category data types

·        Float category data types

·        Boolean category data types

 

Integer category data types

These category data types are used for storing integer data in the main memory of computer by allocating sufficient amount of memory space.

Integer category data types are divided into four types which are given in following table

Data Type

Size

Range

Byte

1

+ 127 to -128

Short

2

+ 32767 to -32768

Int

4

+ x to - (x+1)

Long

8

+ y to - (y+1)

 

Character category data types

A character is an identifier which is enclosed within single quotes. In java to represent character data, we use a data type called char. This data type takes two byte since it follows Unicode character set.

Data Type

Size(Byte)

Range

Char

2

232767 to -32768

 

Float category data types

Float category data type are used for representing float values. This category contains two data types, they are in the given table

Data Type

Size

Range

Number of decimal places

Float

4

+2147483647 to -2147483648

8

Double

8

+ 9.223*1018

16

 

Boolean category data types

Boolean category data type is used for representing or storing logical values is true or false. In java programming to represent Boolean values or logical values, we use a data type called Boolean.

Data Type

Default Value

Default size

boolean

false

1 bit

char

'\u0000'

2 byte

byte

0

1 byte

short

0

2 byte

int

0

4 byte

long

0L

8 byte

float

0.0f

4 byte

double

0.0d

8 byte

 

Derived data types

Derived data types are those whose variables allow us to store multiple values of same type. But they never allows to store multiple values of different types. These are the data type whose variable can hold more than one value of similar type. In general derived data type can be achieve using array.

 

User defined data types

User defined data types are those which are developed by programmers by making use of appropriate features of the language.

User defined data types related variables allows us to store multiple values either of same type or different type or both. This is a data type whose variable can hold more than one value of dissimilar type, in java it is achieved using class concept.

Note: In java both derived and user defined data type combined name as reference data type.

 

public class DataTypesExample {
     public static void main(String args[]) {
//integers
     byte largestByte = Byte.MAX_VALUE;
     short largestShort = Short.MAX_VALUE;
     int largestInteger = Integer.MAX_VALUE;
     long largestLong = Long.MAX_VALUE;
//real numbers
    float largestFloat = Float.MAX_VALUE;
    double largestDouble = Double.MAX_VALUE;
//other primitive types
    char aChar = 'S';
    boolean aBoolean = true;
 //Display them all.
    System.out.println("largest byte value is " + largestByte + ".");
    System.out.println("largest short value is " + largestShort + ".");
    System.out.println("largest integer value is " + largestInteger + ".");
    System.out.println("largest long value is " + largestLong + ".");
    System.out.println("largest float value is " + largestFloat + ".");
    System.out.println("largest double value is " + largestDouble + ".");
     }
    
}


     OUTPUT:

        largest byte value is 127.
        largest short value is 32767.
        largest integer value is 2147483647.
        largest long value is 9223372036854775807.
        largest float value is 3.4028235E38.
        largest double value is 1.7976931348623157E308.