Data Types in Java
In Java, every variable and every expression has some type. Data type defines the values that a variable can take.
Java is not considered as pure Object Oriented Programming language because several OOPs features are not satisfied by Java like (Operator overloading, Multiple Inheritance etc..).
Moreover, we are depending on primitive data types which are non-objects.
1) Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.
double num1 = 22.4;
float num2 = 22.4f;
Note: Always suffix float value with the “f” else compiler will consider it as double.
Characters:
Char (2)
char ch = 'A';
char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports internationalization. UNICODE is a character set which covers all known scripts and language in the world.
Logical:
boolean (1 byte) (true/false)
boolean data type represents only one bit of information either true or false . Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.
// A Java program to demonstrate boolean data type
class AutoTechX
{
public static void main(String args[])
{
boolean b = true;
if (b == true)
System.out.println("Automation Technology Exploration");
}
}
Output: Automation Technology Exploration
byte
The byte data type is an 8-bit signed two’s complement integer.The byte data type is useful for saving memory in large arrays.
Size: 8 bit
Value: -128 to 127
// Java program to demonstrate byte data type in Java
class AutoTechX
{
public static void main(String args[])
{
byte a = 126;
// byte is 8 bit value
System.out.println(a);
a++;
System.out.println(a);
// It overflows here because
// byte can hold values from -128 to 127
a++;
System.out.println(a);
// Looping back within the range
a++;
System.out.println(a);
}
}
Output:
126
127
-128
-127
// Java program to demonstrate primitive data types in Java
class AutoTechX
{
public static void main(String args[])
{
// declaring character
char a = 'A';
// Integer data type is generally
// used for numeric values
int i=11;
// use byte and short if memory is a constraint
byte b = 7;
// this will give error as number is
// larger than byte range
// byte b1 = 7888888955;
short s = 77;
// this will give error as number is
// larger than short range
// short s1 = 87878787878;
// by default fraction value is double in java
double d = 4.355453532;
// for float use 'f' as suffix
float f = 4.7333434f;
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}
Output:
char: A
integer: 11
byte: 7
short: 77
float: 4.7333436
double: 4.355453532
Happy Learning !
- Data types represent the different values to be stored in the variable.
- Each and every data type is clearly defined.
- Every assignment should be checked by compiler for type compatibility.
Java is not considered as pure Object Oriented Programming language because several OOPs features are not satisfied by Java like (Operator overloading, Multiple Inheritance etc..).
Moreover, we are depending on primitive data types which are non-objects.
Data Type | Default Value | Default size |
---|---|---|
byte | 0 | 1 byte |
short | 0 | 2 bytes |
int | 0 | 4 bytes |
long | 0L | 8 bytes |
float | 0.0f | 4 bytes |
double | 0.0d | 8 bytes |
boolean | false | 1 bit |
char | '\u0000' | 2 bytes |
1) Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.
Except Boolean and Char remaining data types are considered as signed data types because we can represent both positive and negative numbers.
There are 8 primitive types: byte, short, int, long, char, float, double, and boolean.
Whole Numbers:
byte (1 byte)
short (2 bytes)
int (4 bytes)
long (8 bytes)
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Fractional Numbers:
float (4 bytes)
double (8 bytes)
double num1 = 22.4;
float num2 = 22.4f;
Note: Always suffix float value with the “f” else compiler will consider it as double.
Characters:
Char (2)
char ch = 'A';
char data type in Java is 2 bytes because it uses UNICODE character set. By virtue of it, Java supports internationalization. UNICODE is a character set which covers all known scripts and language in the world.
Logical:
boolean (1 byte) (true/false)
boolean data type represents only one bit of information either true or false . Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.
// A Java program to demonstrate boolean data type
class AutoTechX
{
public static void main(String args[])
{
boolean b = true;
if (b == true)
System.out.println("Automation Technology Exploration");
}
}
Output: Automation Technology Exploration
byte
The byte data type is an 8-bit signed two’s complement integer.The byte data type is useful for saving memory in large arrays.
Size: 8 bit
Value: -128 to 127
// Java program to demonstrate byte data type in Java
class AutoTechX
{
public static void main(String args[])
{
byte a = 126;
// byte is 8 bit value
System.out.println(a);
a++;
System.out.println(a);
// It overflows here because
// byte can hold values from -128 to 127
a++;
System.out.println(a);
// Looping back within the range
a++;
System.out.println(a);
}
}
Output:
126
127
-128
-127
// Java program to demonstrate primitive data types in Java
class AutoTechX
{
public static void main(String args[])
{
// declaring character
char a = 'A';
// Integer data type is generally
// used for numeric values
int i=11;
// use byte and short if memory is a constraint
byte b = 7;
// this will give error as number is
// larger than byte range
// byte b1 = 7888888955;
short s = 77;
// this will give error as number is
// larger than short range
// short s1 = 87878787878;
// by default fraction value is double in java
double d = 4.355453532;
// for float use 'f' as suffix
float f = 4.7333434f;
System.out.println("char: " + a);
System.out.println("integer: " + i);
System.out.println("byte: " + b);
System.out.println("short: " + s);
System.out.println("float: " + f);
System.out.println("double: " + d);
}
}
Output:
char: A
integer: 11
byte: 7
short: 77
float: 4.7333436
double: 4.355453532
Happy Learning !
Thanks for sharing a good article. This is very interesting and I like this type of article only. I have always read important article like this.
ReplyDeleteC C++ Training in Chennai
C Training in Chennai
core java training in chennai
javascript training in chennai
javascript course in chennai
core java training in chennai
core java training