Learn DataTypes in Java Quick and Easy Way

DataTypes in Java:

Data is the information in such a form that a computer can consume. The data type is the specification of the data that guides us how the computer will represent data and what are the operations that can be done on the data(applied).

To create an object with new for a small simple variable is not efficient. As new operator places objects on the heap. For these types of java, instead of creating the variable using new, an automatic variable is created which is not a handle. The variable holds the value itself. Variables are created on the stack instead of the heap. Datatypes in Java is divided into two major parts. Namely Primitive/intrinsic datatype and Non-primitive/derived/reference type.

A primitive data type is divided into Numeric, non-numerical datatype. The numerical data type is divided into Integral and floating-point. Integral is further divided to Byte, Short, int, long. On the other hand, floating-point is divided into float and double. Non-numerical type is divided into characters and boolean.

A primitive data type takes a fixed amount of space in the memory. Whereas a reference or nonprimitive datatype takes a variable amount of space in memory.

Non Primitive or derived or reference type is divided into classes, arrays, String and interfaces. Every data type has its default value. Default values are what java guarantees when variables are used as a member of a class and initialized. This defaulting behaviour of variable does not apply to local variables. So if in a function we just provide:
int x;

This above statement will not be initialized to zero. And if we try to use it, the compiler throws a compile-time error stating that variable may not have initialized.

Java determines the size of each primitive type. These sizes don’t change from one machine architecture to another(like other languages). This size invariance is one reason java codes are portable. Or in another word, java is very strict and particular about the size. There is another aspect of java data types. All data types are guaranteed default values (instead of garbage values).

All numeric types are signed so we do not need to check for unsigned types. Most primitive types also have wrapper classes to make a class on the heap.

All primitive datatypes have MAX_VALUE and MIN_VALUE constants.

DataTypes in Java:

DataTypes in Java
DataTypes in Java

Integral types:

byte, short, int, char and long are Integral type. All integral except char are signed. (Java does not support unsigned type.)All integral types throw ArithmeticException if we perform divide by zero or modulo by zero operation.

See also  Class DatagramPacket in Java

Byte data:

byte data type represents 8 bits of data ranging from -128 to 127. The default value is zero.

Short Type:

short data type represents 16 bits of data ranging from  -32768 to 32767. The default value is zero.

Integer Type:

Integer can hold whole numbers(exp-14,1234) etc.The size of the value depends on the type of integer we choose. Storage also gets allocated accordingly. Wider data types require more time for manipulation hence, we should use smaller data types whenever possible. This will improve the speed of execution(Example-instead of using it to store 10, we can use byte or Short).
we can also make integers long by appending the letter L or l at the end of the number. (Example- 124L or 567l).Java int types may not be used as boolean types and always signed. Int represents 32 bits of data ranging from -2147483648 to 2147483647. the integer data type can be further subdivided to

  1. Short int – 8 bits representation ranging from -128 to 127
  2. Signed int- 16 bits of representation ranging from  -32768 to 32767
  3. Unsigned int-16 bits of representation ranging from  0 to 65535

Character type:

To store character constant java supports character data type called char. It is 8 bits representation and mostly by default ASCII format. The default value is ‘U000 that is null. The char data type is further divided into two parts:

  1. Signed char- 2 bytes or 16 bits of data ranging from -128 to 127.
  2. Unsigned char- 1byte or 8 bits of data ranging from 0 to 255.

Character literals should be enclosed between single quotes.
like char a=’X’;
All Unicode escape characters like

  • char newline=’n’
  • apostrophe='”
  • delete=’377′
  • aleph=’u05D0′

are supported by java. char does not have a sign. Casting char to byte or short results a negative value.

Long Data Type:

Long represents 64 bits of data ranging from -9223372036854775508 to 9223372036854775507

Floating-point Type:

The floating-point can hold containing fractional parts of a number.(Example-12.34,56.789 etc). Floating-point types are divided into two parts:

  1. float-float type values are single-precision numbers, represents 32 bits of data.Default value 0.L.It ranges from IEEE754 to IEEE754
  2. double-double type values are double-precision numbers, represents 64 bits of data.

default Floating point numbers are double-precision numbers. We need to force them to single precision numbers by appending an f or F to the number. like-
1.23f or 456.789e2F

Double type:

Double precision types are used when we need greater precision in the storage of floating-point numbers. All mathematical functions-Sin,cos,sqrt,etc return double type values.double value number needs to be represented by d or D.

Floating-point data types(Float and Double) support few special values known as Not a Number(NaN), POSITIVE_INFINITY and NEGATIVE_INFINITY. NaN represents the result of operation such as diving by zero, where an actual number is not produced. NaN is unordered, hence comparing it with any number including a NaN results false.
Floating-point arithmetic operations thus do not produce any Exceptions.

Boolean Data Type:

Boolean data type represents only two possible values true or false. It can not hold any other data types. Other data types cannot be treated or converted to boolean.

See also  Class Exception in Java

Primitive Datatypes in Java:


DataTypeLanguage ElementsWrapper ClassSizeRangeComments
LogicalbooleanBoolean1 bitvalue true or false.default is false.
TextCharCharacter16 bitsMin Unicode-0
Max Unicode-2 to the power 16-1
ASCII Characters use 16 bits. default u0000

Literal value enclosed between ”
like ‘a’

IntegralbyteByte1 byte
8 bits
Min value -128
Max value 127
To declare variables to store the binary form of data

default 0

IntegralshortShort2 bytes
16 bits
Max 32767
default 0
IntegralintInteger4 bytes
32 bits
Max 2147483647
default 0
IntegrallongLong8 bytes
64 bits
Min -9223372036854775808
Max 9223372036854775807
default 0
RealdoubleDouble8 bytes
64 bits
Min IEEE754
Max IEEE1985
default 0.0

4 bytes
32 bits
IEEE754 to IEEE1985
default 0.0

Non Primitive Data types:


DataTypeLanguage ElementsWrapper ClassSizeRangeComments
Non PrimitiveStringStringThe string is a Java class that is loaded with the class coder. Typically 16-bit characters
Non PrimitiveVoid
Non PrimitiveReferencestores the handle default value is null


The string is an instance of String Class. The string appears in double-quotes.
like String name=”xyz”;
The compiler automatically creates a String object when it encounters a String provided by double-quotes.

Conversion between Primitive Datatypes:

Conversion between data types is called casting. So casting means explicitly telling java to force a conversion that the compiler would otherwise not carry out explicitly. Narrowing conversion is often referred to as a casting. However, widening casting is also a type of casting. This is implicit and the compiler will do that automatically. For narrowing conversion or downcasting we need to provide the desired type is placed inside a bracket “()”.
Case-1: When java run time explicitly changes the type without users interaction
Case-2: All conversion of primitive data types is checked at compile time to establish whether or not the conversion is permissible.

int i=10;
double d=i;

In this case, an int type is converted to double type. It is an automatic conversion.

double d=10.0;//compiler will throw an error.
double d=(int)10.0;//conversion is done with loss of information
int i=d;

It is not directly permissible. The compiler will throw an error that there could be a possible loss of precision. To forcefully do the conversion we need to cast it to int. The cast (int) forces d to be an int.

public void someMethod(double myValue)

In this method is expecting a value of type double to be passed to it, when it is invoked. If we pass a float value or other data type(Integral or real), as double being top of the primitive data type hierarchy, the value will be converted to double.
Boolean values cannot be converted to any other type.
A non-boolean type can be converted to another non-boolean type provided that the conversion is widening conversion.
A non-boolean type cannot be converted to another non-boolean type if the conversion is narrowing conversion.

High Precision Numbers:

Java 1.1 added two classes for performing high precision arithmetic. They are as follows:

  1. BigInteger
  2. BigDecimal

These are two wrapper classes but they do not have any primitive analogue. We need to use method calls instead of the operation. Operations on these will be slower but the results will be accurate.

BigInteger supports arbitrary precision integers. This means that we can accurately represent integral values of any size without losing any information during operation.

BigDecimal supports arbitrary precision floating-point numbers. We can use them for accurate mostly monetary calculations.

What is AutoBoxing and AutoUnboxing in Java
What is AutoBoxing and AutoUnboxing in Java

What is AutoBoxing and AutoUnboxing in Java?

Java has introduced automatic conversion between primitive types and the corresponding wrapper class. So if we use a value of type int in a context that requires an object of type Integer, the int will automatically be wrapped in an Integer object.

int a=22; is equivalent to
Integer a new Integer(22);

This feature is called autoboxing. Similarly, as variable, a refers to an object of type Integer we can use ‘a’ in the numerical expression like 3*a.
The integer value inside ‘a’ automatically unboxes to int and complete the operation. This feature is called auto unboxing.

Autoboxing and Autounboxing can be applied to Byte, Short, Long, Double, Float data types and method calls. So the autoboxing and unboxing feature facilitate the process of handling primitive datatypes in Collections. We can use this feature to convert primitive types to corresponding wrapper classes automatically. The compiler generates a code implicitly to convert primitive type to the wrapper class. and vice versa.

Double d_object=98.42;
Double d_primitive=d_object.doubleValue();
//same can be written as
Double d_object=98.42;
Double d_primitive=d_object;

java puts restrictions to perform the following conversions:

  1. Convert from null type to any primitive type.
  2. Convert to the null type other than the identified conversion.
  3. Convert from any class type c to any array type if c does not object.

General code for Collection:

import java.util.*;
public class autoUnboxingTest{
public static void main(String []args){
Stack myStack=new Stack();
//Container stack can take only Object hence converting
myStack.push(new Integer(10));
myStack.push(new Integer(20));
Integer num1=(Integer)myStack.pop();
Integer num2=(Integer)myStack.pop();
int sum=num1.intValue()+num2.intValue();

the output of the code:
$javac autoUnboxingTest.java
Note: autoUnboxingTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
$java -Xmx128M -Xms16M autoUnboxingTest
Same can be written as:

import java.util.*;
public class autoUnboxingTest{
public static void main(String []args){
Stack<Integer> myStack=new Stack<Integer>();
int sum=myStack.pop()+myStack.pop();

$javac autoUnboxingTest.java
$java -Xmx128M -Xms16M autoUnboxingTest


Java’s automatic type conversion applies if

  • The two types are compatible.
  • The destination type is larger than the source type.

Share and Enjoy !

5 thoughts on “Learn DataTypes in Java Quick and Easy Way

  1. Pingback: Google
  2. Hola! I’ve been reading your blog for some time now and finally got the courage to go ahead and give you
    a shout out from Atascocita Tx! Just wanted to
    say keep up the fantastic job!

  3. I haven’t a clue how to use WordPress and Comicpress to create sites and publish comic strips! Please help. Do you know of a good walkthrough? Thanks much!.

  4. Hi,. can enybody please tell me other platforms that blogger, wordpress, posterous, dupral where I can make blog posts by email?. I need a list of free blog services that allow you to make your post by email..

Leave a Reply

Your email address will not be published. Required fields are marked *