Showing posts with label Java data types. Show all posts
Showing posts with label Java data types. Show all posts

Saturday, 3 January 2015

Java Character Data Type

Java uses data type char to store individual characters. Java characters are encoded using 16-bit Unicode character set. The char data type is unsigned and the range of values that can be stored in a char vary from 0 to 65535.

The first 128 characters of the Unicode set are the same as the 128 characters of 7-bit ASCII character set and the first 256 characters of the Unicode correspond to the 256 characters of the extended ASCII (8-bit ISO Latin-1) character set.

Java characters can also be used in integer expressions. The Unicode value of the character is used when it is part of an integer expression.

Java Boolean Data Type

Java has a data type boolean. It is used to store boolean values true and false. You can not use zero to represent false or a non-zero value to represent true like C/C++. Boolean values in Java can not be treated like integers and vice-versa.

The size of the boolean data type is undefined but irrespective of the internal storage boolean data type is used to hold just true and false. Boolean values are produced by all relational, conditional and boolean logical operators and are primarily used to govern flow of control during program execution.

Java Floating data types

Floating point data types are similar to C/C++. They are used to store the real numbers. There are two floating-point data types in Java.


Data type Size Range (Absolute value)
float 4 bytes / 32 bits 1.401298464324817E-45f to 3.4028234663852886E38f
long 8 bytes / 64 bits 4.9E-324d to 1.7976931348623157E308d

Java integer data types

Integer data types in Java are quite similar to C/C++. There are four integer types in Java as mentioned above. Java has one additional integer data type "byte".

All the integers are signed values in Java i.e. they can hold positive as well as negative values.

We choose the data type depending on the range of values to be stored.

The range of values for different integer data types is as follows:


Integer Data type Size Range
byte 1 byte / 8 bits -128 to -127
short 2 bytes / 16 bits -32768 to 32767
int 4 bytes / 32 bits -2147483648 to 2147483647
long 8 bytes / 64 bits -9223372036854775808 to 9223372036854775807

Java Data Types

Data types in Java can be broadly classified in two categories

1. Primitive data types / Simple data types
2. Non-primitive data types / Derived data types or Referenced data types

Primitive Data Types

Although java is an object oriented language, but the primitive data types are not objects. They are kept in java for performance reason. They form the basis for all other types of data that you define in your java programs.

The primitive data types may be further classified as:

1. Numeric data types

-- Integer data types - byte, short, int, long
-- Floating data types - float, double

2. Boolean data type - boolean

3. Character data type - char

Reference Data Types

Reference are also called derived data types as they are derived from the primitive data types. Reference data types can be further classified as:

1. Classes
   Built-in / Library classes
   User-Defined classes

2. Interfaces
   Built-in / Library classes
   User-Defined classes

3. Arrays

Array are treated as objects in Java, which is different from C++.