Saturday, January 7, 2017

Data Type in java

Data Type:

--------------------------------------------------------------------------------------------------

Java is strictly a typed programming language, where in java applications before representing data, first, we have to confirm which type of data

we are representing, we have to use "Data Type".

In JAVA applications, data types are able to provide the following two advantages.

1. we can identify memory sizes to store data.
2. we can identify range value for the variables in order to assign.

To prepare java applications, java has provided the following data types:

1. Primitive data type/ primary data types:
---------------------------------------------------------------------------------

1. Numeric Data Types:
------------------------

1. Integral/ Integer data types
---------------------------------
data type memory default value
--------- --------- --------------
byte -----------> 1 byte ---------> 0
Short ----------> 2 bytes --------> 0
int ------------> 4 bytes --------> 0
long -----------> 8 bytes --------> 0

2. Non Integral Data type:
---------------------------
float ----------> 4 bytes --------> 0.0f
double ---------> 8 byte ---------> 0.0

2. Non-Numeric Data Types:
-----------------------------
char -----------> 2 bytes --------> ' '[single space]
boolean --------> 1 bit  ---------> false

2. User Defined data types/ secondary data types:
------------------------------------------------------------------------

All classes interfaces, abstract classes, arrays...

Note: The default value for user defined data types is 'null'.

To get range values on the basis of the data types, we have to use the following formula:

     n-1                    n-1
   -2            to        2  - 1
where 'n' is number of bit.

Note: The above formula is applicable only for integral data types.

EX: byte
size: 1 byte = 8 bits


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

     7                      7
   -2            to        2  - 1

   -128  to 127

EX: calculate minimum & maximum value of Long:

class Test {
public static void main(String[] args) {
System.out.println(Long.MIN_VALUE+ "---->" + Long.MAX_VALUE);
 }
}

To get minimum value & maximum value for each and every primitive data type. java has provided the following two constants from each and every  wrapper class.

MIN_VALUE    MAX_VALUE

Note: Classes representation of premitive data type is called as "Wrapper Classes". JAVA has provided 8 number of wrapper classes in java.lang
package w.r.t. the 8 number of premitive data types.

Primitive Data Types               Wrapper classes
---------------------                     -----------------
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

EX: 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
}
}
Output:

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

You may also like:

--> Type Casting in JAVA

No comments:

Post a Comment