Data Types in Java - FindersAdda : Tutorials of Make Money, Computer or Internet

Monday, 20 November 2017

Data Types in Java


Data type is a type of data through which data are recognized and categorized. Data type shows or determines the nature of variables like type of value to be stored, memory space, ranges etc.

It determines which type of value can be stored in declare variable.

In java every variable or every expression has some type. Each and every data type is clearly defined. Every assignment should be checked by compiler for type compatibility.

Because of the above reason, we can conclude java language is strongly typed programming language.

There are two types of data types in java.
  1. Primitive data type
  2. Non-primitive data type



data types in java



Except boolean and char remaining data types are considered as signed data types because we can represent both the positive and the negative numbers.

PRIMITIVE DATA TYPES


These are the fundamental data types provided by a programming language. In java, primitive data type includes integer, floating point, character and Boolean.

byte data type

The size of byte data type is 1 byte that means 8 bits. It can be stored the value between -128 to 127 that means its range is -128 to 127.


memory representation




The most significant bit (MSB) x is signed bit. 0 means positive number and 1 mean negative number. Positive number will be represented directly in a memory whereas negative number will be represented in 2’s complement form.

byte is the best choice if you want to handle data in terms of streams either from the file or from the network (file supported form or network supported form is the byte).


class First
{
public static void main(String[] args)
{ byte b=10;
System.out.println(b);
}
}



- : OutPut : -

D:\Program\JAVA>javac First.java 
D:\Program\JAVA>java First 
10


class First
{
public static void main(String[] args)
{ byte b=128;
byte s=true;
byte d="Findersadda";
System.out.println(b);
System.out.println(s);
System.out.println(d);
}
}



The above program will get compile time error.
- : OutPut : -

D:\Program\JAVA>javac First.java
First.java:5: error: incompatible types: possible lossy conversion from int to byte
                byte b=128;
                       ^
First.java:6: error: incompatible types: boolean cannot be converted to byte
                byte s=true;
                       ^
First.java:7: error: incompatible types: String cannot be converted to byte
                byte d="Findersadda";
                       ^
3 errors
D:\Program\JAVA>


 
short data type

 

the size of short data types is 2 bytes that means 16 bits and its range is -32768 to 32767 (-215 to 215-1). This is the most rarely data type in java.

Short data type is best of double for 16 bits processor like 8085 but these processors are completely outdated and the hence corresponding short data is also outdated data type.

int data type


The size of int data type is 4 bytes that means 32 bits and its range is -2147483648 to 2147483647 (-231 to 231-1). The most commonly uses data type in the java or other programming language is int.

long data type

Sometimes, int may not enough to hold big values then we should go for long type.
For example, the amount of distance travelled by light in thousand days. To hold this value int may not enough then we need long data type.
long L = 126000 x 60 x 60 x 24 x 1000 miles.

The size of long data type is 8 bytes that means 64 bits and its range is in between -263 to 263 – 1.

float and double data type:


float
double
If we want to use 5 to 6 decimal places then float data type is enough.
If we want to use 14 to 15 decimal places then we will be taken double data type.
float follows single precision.
double follows double precision.
The size of float is 4 bytes that means 32 bits.
The size of double data type is 8 bytes that means 64 bits.
Range of float data type is from -3.4e38 to 3.4e38.
The range of double data type is from -1.7e308 to 1.7e308.



 

boolean data type:

the size of boolean data type is not applicable that means the size of boolean data type is depend on the virtual machine. The range of boolean data type is not also applicable but it allows values are true/false.

Both the above programs are compatible in C and C++ but both are not compatible in java because java is strongly typed programming language.



char data type:


Old languages (like C and C++) are ASCII code based and the number of allowed less than or equal to 256. To represent these 256 characters 8 bits are enough. The size of char in old languages is one byte.

But Java is Unicode based and the number of different Unicode characters is greater than 256 and less than or equal to 65536. To represent these many characters 8 bits may not enough. Compulsory we should go for 16 bits. Hence, the size of char in java is 2 bytes.

NOTE: - null is a default value for object reference and we can’t apply for primitive. If we are trying to use for primitive then we will get compile time error.

SUMMARY OF JAVA PRIMITIVE DATA TYPES


DATA TYPES
SIZE(IN BYTES)
RANGE
WRAPPER CLASS
DEFAULT VALUE
byte
1
-128 to 127
Byte
0
short
2
-32768 to 32767
Short
0
int
4
-2147483648 to 2147483647
Integer
0
long
8
-263 to 263 – 1
Long
0
float
4
-3.4e38 to 3.4e38
Float
0.0
double
8
-1.7e308 to 1.7e308
Double
0.0
boolean
NA
NA (but allowed values are true/false)
Boolean
false
char
2
0 to 65535
Character
0 (represents space characters)



I hope you liked this post so much. If you loved this post, so, share with your friends and also give some suggestion that what should i improve in this post. Thank You So Much......
 

About Author

SUMIT KUMAR GUPTA
SUMIT KUMAR GUPTA

Author & Editor

Myself Sumit Kumar Gupta & 21 year's old person. I am a programmer and blogger. I spend much more time on programming and blogging and helps other programmers and bloggers.

0 comments:

Post a Comment

We are eager to see your comments.
Comment Rule :

1. Don't use any link in the comment box. If you have any problem related to link then contact me with the contact form.

2. Don't use the wrong word if you write the wrong word in comment box then it will get spam.