Data types

On the basis of the data types and their confirmation there are two types of programming languages.

  1. Statically Typed Programming Languages
  2. Dynamically Typed Programming Languages.

Statically Typed Programming Languages:

In any programming language, if the type of the data is confirmed before representing data then that programming language is called “Statically Typed Programming language”.

EX: C, C++ and JAVA

EX:

int a = 10;
Or
int a;
a = 10;

Dynamically Typed Programming Languages:

In any Programming Language, if the type of the data is confirmed after representing data then that programming language is called Dynamically Typed Programming Languages.

EX: Python

int  a = 10; —> Error
a = 10
print(type(a));----> int

In statically Typed programming Languages, data must be represented after confirming its type , here to confirm the type of the data before representing data we have to use “Data Types”.

Data types in Java

There are two types of data types in Java.


1. Primitive Data Types / Primary data types:

Numeric Data Types:
I. Integral Data Types / Integer data Types :
byte -----> 1 byte —---> 0
short -----> 2 bytes —--> 0
int -------> 4 bytes —---> 0
long -----> 8 bytes —---> 0L

II. Non Integral data Types :
float -----> 4 bytes —----> 0.0f
double ----> 8 bytes —----> 0.0

Non Numeric Data Types :
char -------------> 2 bytes —-----> ‘ ’
boolean ----------> 1 bit —-------> false

2. User defined data Types / Secondary data Types:

All Classes Types, All interfaces types, all Arrays,....

Note: No fixed sizes for the user defined data types, the size of the data is depending on the data which we represented in the user defined data types.

Note: In the case of user defined data types , null is the default value.

int a;  // 0

class A {
}

A a; // null

In Java applications, the main advantage of the data types is : 

  1. We can identify memory sizes for the data on the basis of the Data Types only.

int a = 10; // to store 10 value a variable will have 4 bytes 
double d = 33.33D; // d variable will take 8 bytes to store 33.33d

  1. We can identify the range values on the basis of the data types to assign to the variables.

EX: byte b = 150;

It is not possible , because the byte variable is able to allow only the data from -128 to 127 range.

To find range values for the data types JAVA has given the following formula.

  n-1             n-1
-2 to 2 - 1

Where ‘n’ is the number of bits.

EX: byte: 1 byte = 8 bits

8-1 8-1
-2 to 2 - 1

7 7
-2 to 2 - 1

-128 to 128-1

-128 to 127

Note: The above formula is applicable for only Integral data Types, it is not applicable for the remaining data types.

To find min value and max value for all the data types JAVA has provided the following constant variables from all the wrapper classes.

MIN_VALUE    and     MAX_VALUE

Note: To represent all primitive data types in the form of Object types Java has provided a set of predefined classes in the form of Wrapper classes in java.lang package.

byte    —-------------> java.lang.Byte 
short —-------------> java.lang.Short
int —-------------> java.lang.Integer
long —-------------> java.lang.Long
float —-------------> java.lang.Float
double —-------------> java.lang.Double
char —-------------> java.lang.Character
boolean —-------------> java.lang.Boolean

Note: We are able to apply MIN_VALUE and MAX_VALUE constants for the data types like Byte, Short, Integer, long, Float and Double, they are not applicable for Character and Boolean.

Note: If we apply MIN_VALUE and MAX_VALUE constants for the Character data type then we are able to get ‘ ‘ and ‘?’ values.

Note: If we apply MIN_VALUE and MAX_VALUE constants for the data type Boolean then we are able to get the compilation errors.

public class Test{
public static void main(String[] args) {
System.out.println(Byte.MIN_VALUE+"-------->"+Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE+"-------->"+Short.MAX_VALUE);
System.out.println(Integer.MIN_VALUE+"-------->"+Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE+"-------->"+Long.MAX_VALUE);
System.out.println(Float.MIN_VALUE+"-------->"+Float.MAX_VALUE);
System.out.println(Double.MIN_VALUE+"-------->"+Double.MAX_VALUE);
System.out.println(Character.MIN_VALUE+"------>"+Character.MAX_VALUE);
//System.out.println(Boolean.MIN_VALUE+"->"+Boolean.MAX_VALUE);-->Error
}
}

-128-------->127
-32768-------->32767
-2147483648-------->2147483647
-9223372036854775808-------->9223372036854775807
1.4E-45-------->3.4028235E38
4.9E-324-------->1.7976931348623157E308