0% found this document useful (0 votes)
13 views

Data types in java

Uploaded by

Thomachan Antony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Data types in java

Uploaded by

Thomachan Antony
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Variables

A variable is a container which holds the value while the Java program is executed. A variable is
assigned with a data type.

Variable is name of reserved area allocated in memory. In other words, it is a name of memory
location. It is a combination of "vary + able" that means its value can be changed.

1. int data=50; //Here data is a variable

Data Types in Java


Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:

1. Primitive data types: The primitive data types are also known as Basic data types/ Built-
in datatypes /Intrinsic data types / Pre-defined data types.
2. Non-primitive data types: The non-primitive data types are know as ‘user defined data
types’.

Primitive Data Types


Java is a Strictly typed programming language. It means, all variables must be declared before its use. That is
why we need to declare variable's type and name.

Primitive data types can be devided into Numerical and Non Numerical data
types.

Numerical data types are related with numerical data (digits)


1.byte

Size= 8 bits (1 byte) Range = -27 to +27 -1


Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum
value is 127. Its default value is 0.

The byte data type is used to save memory in large arrays where the memory savings is most
required. It saves space because a byte is 4 times smaller than an integer. It can also be used in
place of "int" data type.

Example: byte a = 10, byte b = -20

2.short
Size= 16 bits (2 bytes) Range = -215 to +215 -1

Its minimum value is -32,768 and maximum value is 32,767. Its default value is 0.

The short data type can also be used to save memory just like byte data type. A short data type is
2 times smaller than an integer.

Example: short s = 10000, short r = -5000

3.int
Size= 32 bits (4 bytes) Range = -231 to +231-1

Its default value is 0.

The int data type is generally used as a default data type for integral values unless if there is no
problem about memory.

Example: int a = 100000, int b = -200000

4.long
Size= 64 bits (8 bytes) Range = -263 to +263 -1

Its default value is 0. The long data type is used when you need a range of values more than those
provided by int.

Example: long a = 100000L, long b = -200000L

The above data types are Known as Integer data ypes


Floating Point Data Types

1.float
Size=32 bits (4bytes) Range = 1.4e–045 to 3.4e+038
The float data type is a single-precision 32-bit floating point. The float data type should never be
used for precise values, such as currency. Its default value is 0.0F.

Example: float f1 = 234.5f

2.double
Size=64 bits (8 bytes) Range = 4.9e–324 to 1.8e+308

The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is
unlimited. The double data type is generally used for decimal values just like float. The double
data type also should never be used for precise values, such as currency. Its default value is 0.0d.

Example: double d1 = 12.3

Non Numerical data types


1.char

Size=16 bits (2 bytes) Range = 0 - 65,536

In Java, the data type used to store characters is char. char in Java is not the same as char in C or
C++. In C/C++, char 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. For this purpose, it requires 16 bits. Thus, in Java
char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars.

2.boolean
Size= 1 bit Range = either 0 or 1 (0/1)
Java has a primitive 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, as in the case of a < b.
boolean is also the type required by the conditional expressions that govern the control statements
such as if and for. Its default value = false

You might also like