DataTypes Concept Simplified in Java

Spread the love
  • 1

Datatypes in Java
Datatypes in Java

DataTypes in Java:

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 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 heap. Datatypes in java is divided into two major parts. Namely Primitive/intrinsic datatype and Non primitive/derived/reference type.Primitive datatype is divided into Numeric,non numerical datatype. 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.

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 member of a class and initialized.This defaulting behavior of variable does not applies 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,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 other 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 throws 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.Default value id zero.

Short Type:

short data type represents 16 bits of data ranging from  -32768 to 32767.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 the integer we choose. Storage also gets allocated accordingly.Wider data types requires more time for manipulation hence,we should use smaller data types whenever possible.This will improve the speed of execution(Example-instead of using int 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. interger datatype can be further sub divided 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. 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: 

Floating point can hold containing fractional parts of a number.(Example-12.34,56.789 etc). Floating point types is 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 storage of floating point numbers. All mathematical functions-Sin,cos,sqart,etc return double type values.double value number needs to be represented by d or D.

Floating point datatypes(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 can not be treated or converted to boolean.

Primitive Datatypes in Java:

DataType Language Elements Wrapper Class Size Range Comments
Logical boolean Boolean 1 bits 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 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 String is a java class that is loaded with class coder . Typically 16 bit characters 
Non Primitive Void
Non Primitive Reference stores the handle default value is null


String is an instance of String Class.String is appeared in  double quotes.
like String name=”xyz”;
Compiler automatically create an String object when it encounters a String provided by a 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 as casting. However widening casting is also a type of casting. This is implicit and compiler will do that automatically. For narrowing conversion or down casting 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 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.

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


Please enter your comment!
Please enter your name here