Data Types
In Java, a Keyword that is used for creating variables and objects for storing single or
multiple values is called data types.
A variable is a named memory location which is used for storing one value or one object
reference. An object is also a memory location but it is used for storing multiple
values/objects.
Data types are used to store data temporarily in the computer through a program. In
the real world, we have different types of data like integer, floating-point, character,
boolean, array, string e.t.c. To store all these types of data in the program we must
use data types after that, we are able to perform business required calculations and
validations.
Based on storing one or multiple values, Java supports two types of data types.
• Primitive Data type
• Referenced Data type
Primitive data types are used for creating a variable that will store only one value at a time.
Referenced data types are used for creating an object for storing multiple values of the same
type or different type.
Need for data types in Java
Creating variable and object type memory for storing single and multiple values in a program is part
of performing one operation. In the program, to perform any operation first we must store values,
and to store values, we must allocate memory. We need to provide information about the number
of bytes (size) and type of memory that must be allocated to store a value in the program.
To specify the number of bytes and memory type to the compiler and JVM we must use some set of
keywords. These sets of keywords are collectively called data types. Hence we can say data type is
used for allocating memory for storing the value in a program by specifying the required numbers of
bytes and memory type.
Java is a strongly typed programming language because it is very strong in type checking. In
Java, every variable and every expression must have some type. Each and every variables
data type should be defined clearly. Every assignment will be checked by the compiler for
type compatibility.
I. Fundamental data type : Ex Int a=10; not int a=10,20,30; (primitive data types)
II. Derived data types (non primitive data type)
III. User defined data type
Byte data type in Java
Size :: 1 byte
Range :: -27 to 27-1 Or, -128 to 127
Default value :: 0
Corresponding Wrapper class :: Byte
If we try to assign a value which is not in the range of byte data type
then we will get compile-time error, error: incompatible types
byte b1 = -128; // valid
byte b2 = 10; // valid
byte b3 = 127; // valid
byte b4 = 128; // error
byte b5 = 'a'; // valid
byte b6 = 10.25; // error
Short data type in Java
It is the most rarely used data types in the Java language. The short data type is
best suitable for 16-bit microprocessors like 8085 but currently, they are outdated
and we have better alternatives. Since 16-bit microprocessors not used therefore
short data type is also outdated.
Size :: 2 bytes
Range :: -215 to 215-1
Or,
-32,768 to 32,767
Default value :: 0
Corresponding Wrapper class :: Short
short s1 = 32767; // valid
short s2 = 32768; // error
short s3 = 'a'; // valid
short s4 = 900.5; // error
short s4 = false; // error
// boolean cannot be converted to short
short n2 = 9; // variable assignment
Or,
(short)9; // casting
// it will convert 9 as int to short
Int data type in Java
It is the most used primitive data type in Java. It is used to store integer value in the Java
program.
Size :: 4 bytes
Range :: -231 to 231-1
Or,
-2147483648 to 2147483647
Default value :: 0
Corresponding Wrapper class :: Integer
int a1 = -2147483648; // valid
int a2 = 2147483647; // valid
int a3 = 2147483648; // error
int a4 = 2147483648L; // error
int a5 = 9.5; // error
int a6 = 'a'; // valid
Long data type in Java
Sometimes int data type may not enough to hold big values like 20!
value or phone numbers. Then we should go for a long data type. It is used to store
large integer numbers. Other examples are:- for storing debit and credit card
number, account number, and e.t.c.
Size :: 8 bytes
Range :: -263 to 263-1
Or,
-9223372036854775808
to 9223372036854775807
Default value :: 0
Corresponding Wrapper class :: Long
long n3 = 9; // variable assignment
Or,
(long)9; // casting
// it will convert 9 as int to long
Or,
9L or 9l // using suffix L or l
Double data type in Java
Size :: 8 bytes
Range :: -3.4e38 to 3.4e38
Or,
4.94065645841246544e-324
to 1.79769313486231570e+308
Default value :: 0.0
Corresponding Wrapper class :: Double
double d1 = 10;
double d2 = 97.5;
double d3 = 95.5D;
double d4 = 100L;
Float data type in Java
Size :: 4 bytes
Range :: -1.7e38 to 1.7e38
Or,
1.40129846432481707e-45
to 3.40282346638528860e+38
Default value :: 0.0F
Corresponding Wrapper class :: Float
//[Link]
class Test{
public static void main(String[] args) {
int n = 9;
//int
m1(n);
//representing as byte
byte n1 = 9;
m1(n1);
m1( (byte)9 );
//representing as short
short n2 = 9;
m1(n2);
m1( (short)9 );
//representing as long
long n3 = 9;
m1(n3);
m1( (long)9 );
m1(9L);
}
static void m1(byte b){
[Link]("byte parameter.");
}
static void m1(short s){
[Link]("short parameter.");
}
static void m1(int i){
[Link]("int parameter.");
}
static void m1(long l){
[Link]("long parameter.");
}
}
int parameter.
byte parameter.
byte parameter.
short parameter.
short parameter.
long parameter.
long parameter.
long parameter.
Char Data Type in Java
Size :: 2 bytes
Range :: 0 to 65535
Default value :: ‘\u0000’
Corresponding Wrapper class :: Character
Any single letter or digit or special character placed inside the single quote is called character.
Example:- ‘A’, ‘a’, ‘\0’, ‘@’, e.t.c. Some invalid characters are:- A, a, \0, @ (every character must be
inside a single quote). In this post, we will discuss the char data type in Java.
Every character by default is treated as a char data type. To store a single character we need to
create a char variable. Example:-
char ch1 = ‘A’;
char ch2 = ‘9’;
Allowed characters
To store a character in char data type, inside single quote:-
• Only one character is allowed.
• Only one space is allowed.
• Empty (without any character) is not allowed.
char ch3 = 'Ab'; // invalid
char ch4 = ''; // invalid, empty
char ch5 = ' '; // valid, one space is allowed
char ch6 = ' '; // invalid, more than one space
The byte, short and int data type can be assigned to char data type but it must be positive and within
the range of char data type.
char ch1 = -97; // error
char ch2 = 97; // 'a'
char ch3 = 65; // 'A'
char ch4 = 48; // '0'
Char literal can be represented in 4 ways,
1) Using Character (Ex:- ‘a’, ‘b’, and e.t.c.)
2) Integral literal (Ex:- 97, 98, and e.t.c.)
3) Unicode character set (Ex:- ‘\u0061’, ‘\u0062’ and e.t.c.)
4) Escape character (Example:- ‘\n’, ‘\t’ and e.t.c.)
Boolean Data Type in Java
The boolean data type in Java is used for representing conditional/logical
values true and false.
The boolean data type in Java is incompatible with all other primitive data types, so
we can’t store those values in the boolean data type.
boolean bol1 = true; // valid
// error: incompatible types:
boolean bol2 = 9; // error
// int cannot be converted to boolean
boolean bol3 = 9.99; // error
// double cannot be converted to boolean
boolean bol4 = 'a'; // error
// char cannot be converted to boolean
boolean bol5 = False; // error
boolean bol6 = True; // error
boolean bol7 = "true"; // error
// String cannot be converted to Boolean
public class B {
public static void main(String[] args) {
boolean bol1 = true;
if(bol1==true)
[Link]("It is True");
else
[Link]("It is False");
}
}
Output:
It is True
class PrimitiveDataTypes {
public static void main(String[] args) {
byte b = 9;
short s = 900;
int i = 90000;
long l = 90000000;
char ch = 'a';
float f = 10.5F;
double d = 155.89;
boolean bol = true;
[Link]("byte = " + b);
[Link]("short = " + s);
[Link]("int = "+ i);
[Link]("long = " + l);
[Link]("char = " + ch);
[Link]("float = " + f);
[Link]("double = " + d);
[Link]("boolean = " + bol);
}
}
Output: