Data Types In Java
DATA TYPES:
- A data type is a scheme for representing values. An example is int which is the Integer, a data type.
- Values are not just numbers, but any manner of data that a computer can process.
- The data type defines the kind of data that is represented by a variable.
- As with the keyword class, Java data types are case sensitive.
There are two types of data types
In primitive data types, there are two categories
- numeric means Integer, Floating points
- Non-numeric means Character and Boolean
In non-pimitive types, there are three categories
Following table shows the datatypes with their size and ranges.
Data type |
Size (byte) |
Range |
byte |
1 |
-128 to 127 |
boolean |
1 |
True or false |
char |
2 |
A-Z,a-z,0-9,etc. |
short |
2 |
-32768 to 32767 |
Int |
4 |
(about) -2 million to 2 million |
long |
8 |
(about) -10E18 to 10E18 |
float |
4 |
-3.4E38 to 3.4E18 |
double |
8 |
-1.7E308 to 1.7E308 |
Integer data type :
Integer datatype can hold the numbers (the number can be positive number or negative number). In Java, there are four types of integer as follows:
We can make ineger long by adding ālā or āLā at the end of the number.
Floating point data type :
It is also called as Real number and when we require accuracy then we can use it.
There are two types of floating point data type.
It is represent single and double precision numbers. The float type is used for single precision and it uses 4 bytes for storage space. It is very useful when we require accuracy with small degree of precision. But in double type, it is used for double precision and uses 8 bytes of starage space. It is useful for large degree of precision.
Character data type :
It is used to store single character in memory. It uses 2 bytes storage space.
Boolean data type :
It is used when we want to test a particular condition during the excution of the program. There are only two values that a boolean type can hold: true and false.
Boolean type is denoted by the keyword boolean and uses only one bit of storage.
Following program shows the use of datatypes.
CODE/PROGRAM/EXAMPLE
//Program:
import java.io.DataInputStream;
class cc2
{
public static void main(String args[]) throws Exception
{
DataInputStream s1=new DataInputStream(System.in);
byte rollno;
int marks1,marks2,marks3;
float avg;
System.out.println("Enter roll number:");
rollno=Byte.parseByte(s1.readLine());
System.out.println("Enter marks m1, m2,m3:");
marks1=Integer.parseInt(s1.readLine());
marks2=Integer.parseInt(s1.readLine());
marks3=Integer.parseInt(s1.readLine());
avg = (marks1+marks2+marks3)/3;
System.out.println("Roll number is="+rollno);
System.out.println("Average is="+avg);
}
}
// O/P: C:\cc>java cc2
Enter roll number:
07
Enter marks m1, m2,m3:
66
77
88
Roll number is=7
Average is=77.0
MIXING DATA TYPES :
Java allows mixing of constants and variables of different types in an expression, but during assessment it hold to very strict rules of type conversion.
When computer consider operand and operator and if operands are different types then type is automatically convert in higher type.
Following table shows the automatic type conversion.
|
char |
byte |
short |
int |
long |
float |
double |
Char |
int |
int |
int |
int |
long |
float |
double |
Byte |
int |
int |
int |
int |
long |
float |
double |
Short |
int |
int |
int |
int |
long |
float |
double |
Int |
int |
int |
int |
int |
long |
float |
double |
Long |
long |
long |
long |
long |
long |
float |
double |
Float |
float |
float |
float |
float |
float |
float |
double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |
Double |