DataTypes Concept Simplified in Java

Spread the love

Data is the information in such a form that a computer can consume. The datatype 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 datatype 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 datatype 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 own default value. Default values are what java guarantees when variables are used as a member of a class and initialized. This defaulting behavior 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 datatypes. All datatypes 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 has 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.

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 a 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.

Primitive Datatypes in Java:


DataType Language Elements Wrapper Class Size Range Comments
Logical boolean Boolean 1 bit value true or false.default is false.
Text Char Character 16 bits Min Unicode-0
Max Unicode-2 to the power 16-1
ASCII Characters use 16 bits. default u0000

Literal value enclosed between ”
like ‘a’

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

default 0

Integral short Short 2 bytes
16 bits
Max 32767
default 0
Integral int Integer 4 bytes
32 bits
Max 2147483647
default 0
Integral long Long 8 bytes
64 bits
Min -9223372036854775808
Max 9223372036854775807
default 0
Real double Double 8 bytes
64 bits
Min IEEE754
Max IEEE1985
default 0.0

4 bytes
32 bits
IEEE754 to IEEE1985
default 0.0

Non Primitive Data types:


DataType Language Elements Wrapper Class Size Range Comments
Non Primitive String String The string is a Java class that is loaded with the class coder. Typically 16-bit characters
Non Primitive Void
Non Primitive Reference stores 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 datatypes 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 in order 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 analog. 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.
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1

Leave a Comment

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

Scroll to Top