Data types in java
Data types:
Data type specify the size and type of values that can be stored in a variable. Java is rich in data types. The data type can be classified into two types. They are:
Primitive data types:
There are eight primitive data types in java. The data type and it's default value, size and range are shown below.
Boolean:
It can store only two possible values either True or False. These values cannot be converted implicitly or explicitly through cast to any other type.
It specify 1 bit of information and it size can't be defined precisely.
Example:
boolean flag = true;
boolean stop = false:
Byte:
It is an 8 bit signed two's complement representation of integer. It range lies between -128 to 127. The default value of byte data type is 0. The byte data types is useful for saving memory in large array. It saves 4 times smaller than interger.
Example:
class Example
{
public static void main(String[] args)
{
byte a = 126;
System.out.println (a);
a++;
System.out.println (a);
a++;
// relooping because the byte data hold value between -128 to 127
System.out.println (a);
a++;
System.out.println (a);
}
}
Output:
126
127
-128
-127
Short:
It is an 16 bit signed two's complement data types. It range lies between -32768 to 32767.
The default values of short data types is 0. It is similar to byte, use a short to save memory in large array. It is 2 times smaller than integer data type
Example:
// similar to byte
short a = 32766;
Int:
It is an 32 bit signed two's complement data type. It's range lies between -2^31 to (2^31) -1 . The default value of integer data type is 0.
Note:
The int data type can used to represent the unsigned 32 bit int where values range lies between 0 to (2^32)-1.
Example:
int i = 100;
Long:
It is an 64 bit two's complement representation of integers. It range lies between -2^63 to (2^63)-1. It used when you need a range of value more than int data types.
Note:
The long data type can used to represent the unsigned 64 bit long where values range lies between 0 to (2^64)-1.
It contains methods like compareUnsigned, dividedUnsigned to support arithmetic operation.
Example:
long num = 12345678L:
Float:
It is a single precision 32 bit IEEE 754 floating point. Single precision is much more faster and takes half of the space a double precision.The default value is 0.0f. Use float to save memory in large array of floating point number. This data type is useful if the large precision is not required.
Example:
float f = 234.5f
Double:
It is a double precision 64 bit IEEE 754 floating point. The default value is 0.0d. For decimal value, it is the default choice.
Example:
double d = 234.56d;
Note:
The float and double data types are used only where approximation error can be acceptable. If the value accuracy are more concern then it is not recommended to use these data types.
Char:
It is a single 16 bit Unicode character. It is used to store a single character. The value range lies between '\u0000' to '\uffff'.
Example:
char ch = 'a';
Non-primitive data types:
It is also known as reference data type which store set of values in a specified reference. The non-primitive data type can be class, interface, array, enum etc
Class:
Class is a template for creating a different objects which defines its properties and behaviours.
A class can contain a fields and methods of behavior of an object.
Interface:
It is the blueprint of class that can be implemented in class. It provides the functionality of multiple inheritance. The interface provides complete abstraction which means the methods in interface with empty body and all fields are public, static and final by default. It used to achieve for loose coupling
Example:
interface Addition
{
int i =5;
int addition ();
int substraction ();
}
"After compilation it will change internally to"
interface Addition
{
public static final int i =5;
public abstract int addition();
public abstract int subtraction();
}
Array:
It is the group of value that store in same data type under single object. A specified element in an array are accessed by it index. The index range lies between 0 to (n-1).
Example:
class Example
{
public static void main (String[] args)
{
int [] a = new int [5];
a [0] = 1;
a [1] = 2;
a [2] = 3;
a [3] = 4;
a [4] = 5;
for (int i =0; i< a.length; i++)
{
System.out.println ("Element at the index " + i + ": " a [i]);
}
}
}
Output:
Element at the index 0: 1
Element at the index 1: 2
Element at the index 2: 3
Element at the index 3: 4
Element at the index 4: 5
Enum:
The enumerated data type is used to describe set of value for a variable. They are static, final and type safe because we cannot assign unfit value. It acts like a class and has own constructor.
Example:
enum day
{
Sunday, Monday,Tuesday,Wednesday, Thursday,Friday,Saturday
}
String:
It stores the sequence of char into single object.
The java string is immutable which means it cannot change. In other word, whenever we change the string, a new instance is created. To create mutable string use either stringBuffer or stringBuilder.
The java.lang.String class are used yo create a string object. We can create a String object using
Data type specify the size and type of values that can be stored in a variable. Java is rich in data types. The data type can be classified into two types. They are:
- Primitive data type - includes integers, characters, boolean and floating point
- Non primitive data type - includes class, interfaces,Arrays, etc
Primitive data types:
There are eight primitive data types in java. The data type and it's default value, size and range are shown below.
![]() |
Primitive data types |
Boolean:
It specify 1 bit of information and it size can't be defined precisely.
Example:
boolean flag = true;
boolean stop = false:
Byte:
It is an 8 bit signed two's complement representation of integer. It range lies between -128 to 127. The default value of byte data type is 0. The byte data types is useful for saving memory in large array. It saves 4 times smaller than interger.
Example:
class Example
{
public static void main(String[] args)
{
byte a = 126;
System.out.println (a);
a++;
System.out.println (a);
a++;
// relooping because the byte data hold value between -128 to 127
System.out.println (a);
a++;
System.out.println (a);
}
}
Output:
126
127
-128
-127
Short:
It is an 16 bit signed two's complement data types. It range lies between -32768 to 32767.
The default values of short data types is 0. It is similar to byte, use a short to save memory in large array. It is 2 times smaller than integer data type
Example:
// similar to byte
short a = 32766;
Int:
It is an 32 bit signed two's complement data type. It's range lies between -2^31 to (2^31) -1 . The default value of integer data type is 0.
Note:
The int data type can used to represent the unsigned 32 bit int where values range lies between 0 to (2^32)-1.
Example:
int i = 100;
Long:
It is an 64 bit two's complement representation of integers. It range lies between -2^63 to (2^63)-1. It used when you need a range of value more than int data types.
Note:
The long data type can used to represent the unsigned 64 bit long where values range lies between 0 to (2^64)-1.
It contains methods like compareUnsigned, dividedUnsigned to support arithmetic operation.
Example:
long num = 12345678L:
Float:
It is a single precision 32 bit IEEE 754 floating point. Single precision is much more faster and takes half of the space a double precision.The default value is 0.0f. Use float to save memory in large array of floating point number. This data type is useful if the large precision is not required.
Example:
float f = 234.5f
Double:
It is a double precision 64 bit IEEE 754 floating point. The default value is 0.0d. For decimal value, it is the default choice.
Example:
double d = 234.56d;
Note:
The float and double data types are used only where approximation error can be acceptable. If the value accuracy are more concern then it is not recommended to use these data types.
Char:
It is a single 16 bit Unicode character. It is used to store a single character. The value range lies between '\u0000' to '\uffff'.
Example:
char ch = 'a';
Non-primitive data types:
It is also known as reference data type which store set of values in a specified reference. The non-primitive data type can be class, interface, array, enum etc
Class:
Class is a template for creating a different objects which defines its properties and behaviours.
A class can contain a fields and methods of behavior of an object.
Interface:
It is the blueprint of class that can be implemented in class. It provides the functionality of multiple inheritance. The interface provides complete abstraction which means the methods in interface with empty body and all fields are public, static and final by default. It used to achieve for loose coupling
Example:
interface Addition
{
int i =5;
int addition ();
int substraction ();
}
"After compilation it will change internally to"
interface Addition
{
public static final int i =5;
public abstract int addition();
public abstract int subtraction();
}
Array:
It is the group of value that store in same data type under single object. A specified element in an array are accessed by it index. The index range lies between 0 to (n-1).
Example:
class Example
{
public static void main (String[] args)
{
int [] a = new int [5];
a [0] = 1;
a [1] = 2;
a [2] = 3;
a [3] = 4;
a [4] = 5;
for (int i =0; i< a.length; i++)
{
System.out.println ("Element at the index " + i + ": " a [i]);
}
}
}
Output:
Element at the index 0: 1
Element at the index 1: 2
Element at the index 2: 3
Element at the index 3: 4
Element at the index 4: 5
Enum:
The enumerated data type is used to describe set of value for a variable. They are static, final and type safe because we cannot assign unfit value. It acts like a class and has own constructor.
Example:
enum day
{
Sunday, Monday,Tuesday,Wednesday, Thursday,Friday,Saturday
}
String:
It stores the sequence of char into single object.
The java string is immutable which means it cannot change. In other word, whenever we change the string, a new instance is created. To create mutable string use either stringBuffer or stringBuilder.
The java.lang.String class are used yo create a string object. We can create a String object using
- String literal
- By new keyword
String literal:
It create by using the double quotes.
Example:
String s ="Hello World";
By new keyword:
The new keyword is used to create an object.
Example:
String s =new String ("Hello world");
In above example JVM create a string object in heap memory and store the literal inside that object.
Program example:
class Example
{
public static void main (String[] args)
{
String s = "Hello world";
String s1 = new String("java");
System.out.println(s);
System.out.println(s1);
}
}
Output:
Hello world
java
Program example:
class Example
{
public static void main (String[] args)
{
String s = "Hello world";
String s1 = new String("java");
System.out.println(s);
System.out.println(s1);
}
}
Output:
Hello world
java
This comment has been removed by a blog administrator.
ReplyDeleteThanks for the valuable comment. Please share this blog with your friends and follow us on Google community: https://plus.google.com/communities/108383297026980973370?sqinv=dnpOeG9jd0YxR0lHTHEyNGpXWFd1X3FEYzFyQlBR Facebook: https://www.facebook.com/easiercoding/
Delete