DataTypes Concept Simplified in Java

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.

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

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

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.

Floating point datatypes support a special values known as Not a Number(NaN).NaN represents the result of operation such as diving by zero,where an actual number is not produced.

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.

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 8 bits value true or false
Text Char Character 16 bits

Unicode-0 to Unicode-2 to the power 16-1 ASCII Characters use 16 bits

Literal value enclosed between ''
like 'a'
Integral byte Byte 1 byte
8 bits
To declare variables to store binary form of data
Integral short Short 2 bytes
16 bits
Integral int Integer 4 bytes
32 bits
Integral long Long 8 bytes
64 bits
Real double Double 8 bytes
64 bits
IEEE754 to IEEE1985
Real float Float 4 bytes
32 bits
IEEE754 to IEEE1985

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

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 can not 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 can not 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 operation. Oprerations on these will be slower but the resultant will be accurate.

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

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

DataTypes Concept Simplified in Java DataTypes Concept Simplified in Java Reviewed by Animesh Chatterjee on December 11, 2018 Rating: 5

No comments:

Powered by Blogger.