0% found this document useful (0 votes)
45 views8 pages

What Are Data Types in Java ?

The document explains Java's primitive data types, which are essential for storing single values without special capabilities. There are eight primitive data types: boolean, byte, char, short, int, long, float, and double, each with specific sizes, value ranges, and default values. It also highlights the importance of type compatibility in Java, emphasizing its classification as a strongly typed programming language.

Uploaded by

anjanabahzala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

What Are Data Types in Java ?

The document explains Java's primitive data types, which are essential for storing single values without special capabilities. There are eight primitive data types: boolean, byte, char, short, int, long, float, and double, each with specific sizes, value ranges, and default values. It also highlights the importance of type compatibility in Java, emphasizing its classification as a strongly typed programming language.

Uploaded by

anjanabahzala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JAVA Primitive Data Types

What are Data Types in Java ?


Java language is consider as a stongly typed programming language becaused type is important.

In java , every variable and expression has some datatype.

Each and every datatype is clearly defined.

Every Assigment should be checked by complier type compatibility.


Because of above reason we can conclude java is consider as a stongly typed programming language.

Primitive data types are the types in java that can store a single value and do not provide any special capability.
Data types in Java are of different sizes and values that can be stored in the variable that is made as per convenience
and circumstances to cover up all test cases. Java has two categories in which data types are segregated

1. Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double

2. Non-Primitive Data Type or Object Data type: such as Class ,String, Array, etc.

JAVA Primitive Data Types 1


Primitive Data Types in Java :-
Primitive data are only single values and have no special capabilities. There are 8 primitive data types. They are depicted
below in tabular format below as follows:

Type Description Default Size Example Literals Range of values

boolean true or false false 1 bit true, false true, false

twos-complement
byte 0 8 bits (none) -128 to 127
integer

‘a’, ‘\u0041’, characters representation of


char Unicode character \u0000 16 bits ‘\101’, ‘\\’, ‘\’, ASCII values
‘\n’, ‘β’ 0 to 255

twos-complement
short 0 16 bits (none) -32,768 to 32,767
integer

-2,147,483,648
twos-complement
int 0 32 bits -2,-1,0,1,2 to
intger
2,147,483,647

-9,223,372,036,854,775,808
twos-complement
long 0 64 bits -2L,-1L,0L,1L,2L to
integer
9,223,372,036,854,775,807

1.23e100f ,
IEEE 754 floating
float 0.0 32 bits -1.23e-100f , .3f upto 7 decimal digits
point
,3.14F

1.23456e300d ,
IEEE 754 floating
double 0.0 64 bits -123456e-300d , upto 16 decimal digits
point
1e1d

Let us discuss and implement each one of the following data types that are as follows:

JAVA Primitive Data Types 2


1. Boolean Data Type
Boolean data type represents only one bit of information either true or false which is intended to represent the two truth
values of logic and Boolean algebra, but the size of the boolean data type is virtual machine-dependent.
Size: Virtual machine dependent
Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can
easily write conversion code.
The Boolean data type specifies one bit of information, but its "size" can't be defined precisely.
Example: boolean input = false
Syntax:

boolean booleanVar;

2. Byte Data Type


The byte data type is an 8-bit signed two’s complement integer.
Size: 1 byte (8 bits)

The byte data type is useful for saving memory in large arrays.
Its value-range lies between -128 to 127 (inclusive).
Its minimum value is -128 and maximum value is 127.
Its default value is 0.
Syntax:

byte byteVar;

3. Short Data Type


The short data type is a 16-bit signed two’s complement integer.
Size: 2 bytes (16 bits)

Similar to byte, use a short to save memory in large arrays, in situations where the memory savings actually matters.
Its value-range lies between -32,768 to 32,767 (inclusive).
Its minimum value is -32,768 and maximum value is 32,767.
Its default value is 0.
Syntax:

short shortVar;

JAVA Primitive Data Types 3


4. Integer Data Type
It is a 32-bit signed two’s complement integer.

Size: 4 bytes ( 32 bits )


Its value-range lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).

Its minimum value is - 2,147,483,648and maximum value is 2,147,483,647.


Its default value is 0.

Syntax:

int intVar;

Remember: In Java SE 8 and later, we can use the int data type to represent an unsigned 32-
bit integer, which has a value in the range [0, 232-1]. Use the Integer class to use the int data
type as an unsigned integer.

5. Long Data Type


The range of a long is quite large. The long data type is a 64-bit two’s complement integer and is useful for those
occasions where an int type is not large enough to hold the desired value. The size of the Long Datatype is 8 bytes (64
bits).

Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).

Its minimum value is - 9,223,372,036,854,775,808and maximum value is 9,223,372,036,854,775,807. Its default value is
0.

The long data type is used when you need a range of values more than those provided by int.
Syntax:

long longVar;

Remember: In Java SE 8 and later, you can use the long data type to represent an unsigned
64-bit long, which has a minimum value of 0 and a maximum value of 264-1. The Long class
also contains methods like comparing Unsigned, divide Unsigned, etc to support arithmetic
operations for unsigned long.

6. Float Data Type


The float data type is a single-precision 32-bit IEEE 754 floating-point. Use a float (instead of double) if you need to
save memory in large arrays of floating-point numbers. The size of the float data type is 4 bytes (32 bits).

The float data type occupies 4 bytes of memory.


Its value range is unlimited.

JAVA Primitive Data Types 4


Its default value is 0.0F.
Example:
float f1 = 234.5f

Syntax:

float floatVar;

7. Double Data Type


The double data type is a double-precision 64-bit IEEE 754 floating-point. For decimal values, this data type is generally
the default choice. The size of the double data type is 8 bytes or 64 bits.
The double data type occupies 8 bytes of memory.
Its value range is unlimited.
Its default value is 0.0d.
Example:
double d1 = 12.3
Syntax:

double doubleVar;

Note: Both float and double data types were designed especially for scientific calculations,
where approximation errors are acceptable. If accuracy is the most prior concern then, it is
recommended not to use these data types and use BigDecimal class instead.

It is recommended to go through rounding off errors in java.

8. Char Data Type


The char data type is a single 16-bit Unicode character with the size of 2 bytes (16 bits).
Its value-range lies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store characters.

Syntax:

char charVar;

Why is the Size of char 2 bytes in Java?


So, other languages like C/C++ use only ASCII characters, and to represent all ASCII characters 8 bits is enough. But
java uses the Unicode system not the ASCII code system and to represent the Unicode system 8 bits is not enough to
represent all characters so java uses 2 bytes for characters. Unicode defines a fully international character set that can
represent most of the world’s written languages. It is a unification of dozens of character sets, such as Latin, Greek,
Cyrillic, Katakana, Arabic, and many more.

Properties Primitive data types Objects

Origin Pre-defined data types User-defined data types

Reference variable is stored in stack


Stored structure Stored in a stack
and the original object is stored in heap

When copied Two different variables is created along with different Two reference variable is created but
assignment(only values are same) both are pointing to the same object on

JAVA Primitive Data Types 5


the heap

When changes are made in the


Change does not reflect in the original ones. Changes reflected in the original ones.
copied variable

The default value for the reference


Default value Primitive datatypes do not have null as default value
variable is null

Example byte, short, int, long, float, double, char, boolean array, string class, interface etc.

DURGA SIR :-

for itegral datatypes [byte,short,int,long] we can specifies literal value in the follwing base.

1) Decimal Literal :- Base (10)


eg : int x = 10;

2) Octa Literal :- Base (8) 0-7


Literal value should be prefix 0.

int x = 010;

3) Hexa Literal :- Base (16) 0-9 , A-F


Literal value should be prefix 0x.

for extra digit [ a to f ] [ A to F] we can use both uppercase and lowercase. characters. this is one place where java is not
casesensitive.
int x = 0x10;

JAVA Primitive Data Types 6


int x=10; …………….yes
int x= 0786;……..no

int x= 0xFace;……..yes
int x= 0xBeef;……..yes
int x= 0xBeer;……..no

int x =10;…………...yes
long l =10L;…………..yes
int x = 10L;………………no
long l=10;…………...yes

float f =123.456;………no
float f =123.456F;………yes
double d = 123.456;………yes
double d = 123.456D;………..yes

float f =123.456d;……………no
double d = 123.456;…………….yes
double d = 0123.456;…………yes

double d = 0x123.456;…………no
double d =0786;…………..no
doible d=0xfaee;…………….yes
double d =0786.0;…………..yes

double d =0xface.0;…………no
double d=10;…………..yes
double d=0777;……………..yes

int x=10.0;……………..no
float f = 1.2e3F;….yes
float f = 1.2e3;…………no
boolean b =true;….yes

boolean b = 0;…..no
boolean b =True;……..no
boolean b =”true”;….yes

char ch=’a’;…………yes
char ch=a;………………..no
char ch= “a”;………..no
char ch= ‘ab’;……..no

char ch =0777;…..yes
char ch= oxFace;……….yes

JAVA Primitive Data Types 7


char ch = 65535;…………yes
char ch= 65536…….no
char ch=’\n’;……..yes

char ch=’\t’;……..yes
char ch=’\m’;……..no
string r =”durga”;

int x = 10;…………10
int y = 010;………….8
int z=0x10;………….16

10L…...
010L……

0x10L……Long

JAVA Primitive Data Types 8

You might also like