Data Types in Java.

Data Types in Java :

Data type specifies memory size and type of values that can be stored into the memory location
Or
Every variables in java has a data type.
Data types specify the size and type of values that can  be stored.


There are two data types available in Java:
I ] Primitive  Data type
II ] Non -Primitive Data type


I ] Primitive(Built- in/Basic/Instrinsic/Simple) Data Types :
Primitive data types are predefined by the
language and named by a keyword.
These data types are already hard coded into the compiler to be recognized when the program is executed.



Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean.

i. Numeric Data type :

Integer -->( byte ,short,int ,long )
All integer data types are signed i,e +ve or 

-ve .

Java deos not support unsigned or only +ve integer.

Floating point --> (float,double)
Also known as real number.
It is used for evaluating expression that require fractional precesion.


ii. Non -Numeric Data type :


Boolean -->boolean
Character -->char

Let us now look into detail about the eight primitive data types.

1. byte:
-->Storage size is 1 byte.
--> Byte data type is a 8-bit signed two's complement integer.
-->Minimum value is -128 (-2^7)
-->Maximum value is 127 (inclusive)(2^7 -1)
-->Default value is 0
-->Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.

Example : byte a = 100 , byte b = -50


2. short:
-->Storage size is 2 byte.
-->Short data type is a 16-bit signed two's complement integer.
-->Minimum value is -32,768 (-2^15)
-->Maximum value is 32,767(inclusive) (2^15 -1)
-->Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
-->Default value is 0.

Example : short s= 10000 , short r = -20000


3. int:
-->Storage size is 4 byte.
-->Int data type is a 32-bit signed two's complement integer.
--> Minimum value is - 2,147,483,648.(-2^31)
--> Maximum value is 2,147,483,647(inclusive).(2^31 -1)
--> Int is generally used as the default data type for integral values unless there is a concern about memory.
-->The default value is 0.

Example : int a = 100000, int b = -200000


4. long:
-->Storage size is 8 byte.
--> Long data type is a 64-bit signed two's complement integer.
--> Minimum value is -9,223,372,036,854,775,808.(-2^63)
-->Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
-->This type is used when a wider range than int is needed.
-->Default value is 0L.

Example : long a = 100000L, int b = -200000L


5. float:
-->Storage size is 4 byte.
-->Float data type is a single-precision 32-bit IEEE 754 floating point.
--> Float is mainly used to save memory in large arrays of floating point numbers.
-->Default value is 0.0f.
-->Float data type is never used for precise values such as currency.

Example : float f1 = 234.5f


6. double:
-->Storage size is 8 byte.
-->double data type is a double-precision 64-bit IEEE 754 floating point.
--> This data type is generally used as the default data type for decimal values. generally the default choice.
-->Double data type should never be used for precise values such as currency.
--> Default value is 0.0d.

Example : double d1 = 123.4d


7. boolean:
-->Storage size is only 1 bit.
-->boolean data type represents one bit of information.
-->There are only two possible values : true and false.
--> This data type is used for simple flags that track true/false conditions.
-->Default value is false.

Example : boolean one = true


8. char:
-->Storage size is 2 byte.
-->char data type is a single 16-bit Unicode character.
-->Minimum value is '\u0000' (or 0).
-->Maximum value is '\uffff' (or 65,535 inclusive).
-->Char data type is used to store any character.
-->Default value is  '\u0000'

Example :char letterA ='A'



II ) Non primitive (Reference/Object /Derived) Data Types :
These data types are special types of data which are user defined.
Non-primitive data types do not store the value itself but they store a reference or address (memory location) of that value.
Non-primitive data types are not defined or created by Java but they are created by the programmers.


-->Default value of any reference variable is null.


Example :
long modelNumber =23334563578

Where
ModelNumber -Variable
23334563578 -Value of the variable
1004 - Address of the variable


There are five categories of derived types:
1.Classes
2.Interfaces
3.Arrays
4.Strings
5.objects

1.Classes : 
Class is a blueprint or protype for creating objects. 
A class contains fields(variables) and methods to describe the behavior of an object.

Syntax:
<Access modifier> class <className>
{
<fields>
<methods>
}

Example :
public class Student
{
int rno;
String name;
double per;
public void getdata(){//code}
public void putdata(){//code}
}


2.Interface : Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body) and the variables declared in interface  are by default static final.

Syntax:
<Access specifier> interface <interfaceName>
{
<static final fields>
<abstract methods>
}

Example :
public interface Shape
{
public static final double pi=3.142;
public abstract void getdata();
public  abstract void putdata();
}


3.Array : An Array is defined as collection of similar type of data elements.
Or
Arrays are homogeneous collection of data elements.

Types of arrays :
Single dimensional array
Multi dimensional array

Syntax:
<data-type> <arrayName>[]=new <data-type>[size];

Example :
int a[]=new int[5]; //declaration

a[0]=5;
a[1]=10;//initialization
a[2]=15;
a[3]=20;
a[4]=25;


4.Strings : String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.

Syntax:
String <String_variable_name> = “<sequence_Of_Charecters>” ;

Example:
String str= "Hello";
Or
String str=new String("Hello");


5.Objects : objects are basically (represents) the real-world entities.
An Object is an instance of a class.

Syntax:
<Classname> <obj-name> = new <Classname>();

Example :
Student s= new Student();


Questions:
1.Explain the various  data types used in java.
2.Explain primitive data types in java.
3.List the standard default values of  each data types used in java.
4.Write all primitive data types available in java with their storage sizes(width) in bytes.



Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java