Java – Datatype Explanation

Home/Java - Tutorial/Java - Datatypes/Java – Datatype Explanation
Java – Datatype Explanation 2017-08-09T09:14:41+00:00

Data type in java:

We all know the data and its type. For storing different type of data we have data types.

Java defines eight types of data: byte, short, int, long, float, double, char and boolean.

These can be put in four groups.

Integers:This group includes byte, short, int, and long, which are for signed and unsigned number including zero.

Floating-point numbers:This group includes float and double, which can store numbers with fractional precision.

Characters:This group includes char, which represents symbols in a characterset, like letters and numbers.

Boolean:This group includes boolean, which is a special type for representing true/false values.

Now we elaborate each data type in detail.

NOTE: The data types mentioned above are primitive data types. Java also support reference data types like Array, Class and Interface. We will study those things in further chapter when needed.

1. Integers:

Java defines four integer types: byte, short, int, and long. All of these can have signed unsigned or zero value. Java does not support unsigned, positive-only integers.

Name    Width (in bits)        Range

long          64                           –9,223,372,036,854,775,808  to 9,223,372,036,854,775,807

int             32                            –2,147,483,648 to 2,147,483,647

short         16                            –32,768 to 32,767

byte          08                            –128 to 127

Note: Divide width with 8 to get size in Bytes;

the table mentioned above is providing width and range of the integer data type. Width is the size the data type will occupy in the program if you write int a,b; than a and b both will occupy 32 bits (4 bytes) in memory.

1.1 byte

The smallest integer type is byte. This is a signed 8-bit type that has a range from –128 to 127

Ex. byte b1;

Variables of type byte are especially useful when you’re working with a stream

of data from a network or file.

1.2 short

Short is a signed 16-bit type. It has a range from –32,768 to 32,767

so if you are sure that your value will not more than the range of Short data type than and only we recommended to use this.

Ex short s1,s2;

1.3 int

The most commonly used integer type is int. It is a signed 32-bit type that has a range

from –2,147,483,648 to 2,147,483,647.

Ex.  int  a, b;

1.4 long

long is a signed 64-bit type and is useful for those occasions where an int type is not

large enough to hold the desired value. The range of a long is quite large. This makes

it useful when big, whole numbers are needed.

Ex  long  l , g;

//Demonstrate the integer data type
The value of different variables of type integers is.........
byte integers : b1 = 50 and b2 = 0
short integers : s1 = 10 and s2 = 20
int integers : i1 = -555 and i2 = 123
long integers : l1 = 100000 and l2 = 2033

2. Floating-Point Types:

Floating-point numbers, also known as real numbers, are used when evaluating

expressions that require fractional precision. For example, calculations such as square

root, or transcendental such as sine and cosine, result in a value whose precision

requires a floating-point type.

Name    Width in Bits          Approximate Range

double        64                           4.9e–324 to 1.8e+308

float            32                           1.4e-045 to 3.4e+038

2.1 float

The type float specifies a single-precision value that uses 32 bits of storage. Single

precision is faster on some processors and takes half as much space as double

precision, but will become imprecise when the values are either very large or very small.

When you require to store floating point value use float and double.

Ex. float  r, pi;

2.2 double

Double precision, as denoted by the double keyword, uses 64 bits to store a value. Double

precision is actually faster than single precision on some modern processors that have

been optimized for high-speed mathematical calculations.

Ex. double  d , k;

Output :
The value of different variables of type floating point is........
float value : f1 = 1.2 and f2 = 123.2
double value : d1 = 1.234 and d2 = 23.4512

3. Characters:

In Java, the data type used to store characters is char.

NOTE: However, C/C++ programmers beware: char in Java is not the same as char in C or

C++. In C/C++, char is an integer type that is 8 bits wide. This is not the case in Java.

Instead, Java uses Unicode to represent characters.

Unicode defines a fully international character set that can represent all of the characters

found in all human languages.

In Java char require 16-bit. The range of a char is 0 to 65,536. There are no negative chars.

The standard set of characters known as ASCII still ranges from 0 to 127. The extended 8-

bit character set, ISO-Latin-1, ranges from 0 to 255. Since Java is designed to allow

applets to be written for worldwide use, it makes sense that it would use Unicode to

represent characters.

Ex. char ch;

 //Demonstrate char data type.
Output :
The value of type character variable is........
char data type : ch1 = X and ch2 =  Y

4. Boolean:

Java has a simple type, called boolean, for logical values. It can have only one of two

possible values, true or false. This is the type returned by all relational operators, such as

a < b.

Ex. boolean b;

So here the value of b is either true or false.

Output :
The value of type boolean variable is........
boolean data type :  b is false
boolean data type : b is true
This is executed.
100 > 9 is true

 

Prev Next