Java Data Types

Java Data Types
Java Data Types

Data types in Java:

Java defines eight simple (or elemental) types of data: byte, short, int, long, char, float,
double, and boolean. These can be put in four groups:
Integers This group includes byte, short, int, and long, which are for wholevalued
signed numbers.
byte
Size(in byte) 1 byte
Range –128 to 127
The smallest integer type is byte.
Byte variables are declared by use of the byte keyword. For example, the following
declares two byte variables called b and c:
byte b, c;

short
Size(in byte) 2 bytes
Range –32,768 to 32,767
Here are some examples of short variable declarations:
short s;
short t;

int
Size(in byte) 4 bytes
Range –2,147,483,648 to 2,147,483,647
The most commonly used integer type is int.
int n;

long
Size(in byte) 8 bytes
Range –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The range of a long is quite large. This makes it useful when big, whole numbers are needed.
long l;

Floating-point numbers This group includes float and double, which represent
numbers with fractional precision.
double
Size(in byte) 8 bytes
Range 4.9e–324 to 1.8e+308
double pi;

float
Size(in byte) 4 bytes
Range 1.4e−045 to 3.4e+038
Here are some example float variable declarations:
float f;

Characters This group includes char, which represents symbols in a character
set, like letters and numbers.
In Java, the data type used to store characters is char.
Size(in byte) 2 bytes
Range 0 to 65,536.
char ch;

Boolean This group includes boolean, which is a special type for representing
true/false values.
It can have only one of two possible values, true or false.
boolean b;

1- Primitive data types: The primitive data types include boolean, char, byte, short, int, long, float and double.
2- Non-primitive data types: The non-primitive data types include Classes, Interfaces, and Arrays.

Data TypeDefault ValueDefault size
booleanfalse1 bit
char‘\u0000’2 byte
byte01 byte
short02 byte
int04 byte
long0L8 byte
float0.0f4 byte
double0.0d8 byte

Example Programs:

Variables Initialization in JAVA

Character Escape Sequences

EscapeSequence Description
\dddOctal character (ddd)
\uxxxxHexadecimal UNICODE character (xxxx)
\’Single quote
\”Double quote
\\Backslash
\rCarriage return
\nNew line (also known as line feed)
\fForm feed
\tTab
\bBackspace
java java tutorials learn java study java