Datatypes in Java | Readstall

Datatypes in Java

07-02-19 Devendra Ganta 0 comment

Datatypes :

                       variables are nothing but reversed memory location to store values.
That means when you create a variable you reserve some spacing memory based
on the datatype of a variable the jvm allocate memory and decides what can
stored in memory.

                     There are two types of datatypes
a.Primitive datatype
b.Non-primitive datatype
a.Primitive datatypes:

There are 8 primitive datatypes supported by java.These are predefined
by the language and named by a keyword.

1.Boolean datatype:

It represents two values ‘true’ and ‘false’

ex:Boolean flag=true;

2.Character datatype:

In java this datatype used to store characters. These includes
alphabets and symbles.

ex:char ch=’A’;

3.Integral datatype:

it is one of the most commonly used datatype in java. It is used
to store integral values.

ex:int a=10; [4 bytes]

4.Byte:
it is smallest integer type in java

ex:byte b=10; [1 byte]

5.Short:
A short is twice the size of a byte [2 bytes]

ex:short s=2;

6.Long datatype:

Long datatype is usefull for where an int type is not enough to hold
the value. Range [8 bytes].

7.Float:

Any decimal or fractional values are declared as float [4 bytes].

ex:float f=22.3;

8.Double:

Double is a datatype that is twice the size of a float

ex:double d=22.32; [8 bytes].
EXAMPLE:

class Data
{
public static void main(String args[])
{
char c=’a’;
int a=45;
byte b=12;
short s=89;
long l=4535;
float f=45;
double d=4512.352;
boolean flag=true;
System.out.println(“character is:”+c);
System.out.println(“integer is:”+a);
System.out.println(“byte is:”+b);
System.out.println(“short is:”+s);
}
}

output:

character is:a
integer is:45
byte is:12
short is:89

b.Non-primitive datatypes:

Non-primitives can be classified into two types.
1.Arrays
2.Strings
1.Arrays:

An array represents the group of elements of same datatype.
That means we can store a group of ‘int’ values or a group of ‘float’
values or a group of string in the array.

Array in java is index based first element of the array is
stored at ‘0’index.

there are two different types of arrays
*Single dimentional array
*Multidimentional array

*single dimentional array:

Single dimentional array or a one dimentional array represents
either a row or a column of the elements.There are different ways of
creating arrays

case 1: We can declare the one-dimentional array and can directly
store elements at the time of declaring.
Syntax:
int a[]={1,2,3};//here ‘int’ is the datatype
a[]-is the name given to array
[]-Represents the one dimentional array//

2.Strings:

Group of characters are called as ‘strings’.for example, A person
enter details like name,creditcard no,address etc.

In java string is a class which is located java.lang which contains
all necessary methods to manipulate the string eventhough string is a class
it is often used as a datatype.

Syntax:
String s=”java”;

Creating String:

we can create a string just by assigning a group of characters to a
string type variable

String s=”hello”;
we can create a string by using new operator

String s=new String[“hello”];
Third way creating the string is by converting array into strings. Let us take
a character type array Arr[].

Syntax:

char Arr[]={‘h’,’e’,’l’,’l’,’o’};



Leave a reply