Data Types
A data type tells us what kind of information we can store. When writing a program we might deal with different types of data such as characters, integers, decimals and strings to name a few. We have 2 data types primitive and non-primitive. The non-primitive data types are classes , interfaces, arrays and they are usually created with the word "new" . The primitive data types are part of the core language. They are stored on the stack and can be created without using the "new" call. They are not objects.
boolean data type
byte data type
char data type
short data type
int data type
long data type
float data type
double data type
Boolean data type an store true or false. The "byte" data type can store numbers from -128 to 127 . The int can store numbers from - 2,147,483,648 .
File: "data1.java"
public class data1
{
public static void main( String args[] )
{
boolean b1 ;
byte b2 ;
char ch1 ;
short sh1 ;
int i1 ;
long l1 ;
float f1 ;
double d1 ;
b1 = true ;
b2 = 100 ;
ch1 = 'c' ;
sh1 = 122 ;
i1 = 23000 ;
l1 = 4000000 ;
f1 = 2.4f ;
d1 = 2.6 ;
System.out.println( "b1 = " + b1 ) ;
System.out.println( "b2 = " + b2 ) ;
System.out.println( "ch1 = " + ch1 ) ;
System.out.println( "sh1 = " + sh1 ) ;
System.out.println( "i1 = " + i1 ) ;
System.out.println( "l1 = " + l1 ) ;
System.out.println( "f1 = " + f1 ) ;
System.out.println( "d1 = " + d1 ) ;
}
}
Output:
M-BAT453-LAB23:ajay ccsfcomputers$ java data1
b1 = true
b2 = 100
ch1 = c
sh1 = 122
i1 = 23000
l1 = 4000000
f1 = 2.4
d1 = 2.6
We can always assign a variable of a data type with a smaller range to a data type with a higher range but if we try to do the reverse then we will encounter a compiler error,
File: "data2.java"
public class data2
{
public static void main( String args[] )
{
byte b2 ;
int i1 ;
b2 = 100 ;
i1 = b2 ; //This is fine
//because the int's range is higher than the byte
// b2 = i1 ; //Compiler complains
i1 = 513 ;
b2 = (byte)i1 ; //Type casting
//We are telling the compiler that trust me
// I know what I am doing.
System.out.println( "b2 :" + b2 ) ;
i1 = 128 ;
b2 = (byte ) i1 ;
System.out.println( "b2 :" + b2 ) ;
}
}
Output:
M-BAT453-LAB23:ajay ccsfcomputers$ java data2
b2 :1
b2 :-128
In the following line we are trying to assign an integer type to a byte type and get a compiler error.
// b2 = i1 ; //Compiler complains
We can compile it by "forcing" the type .
b2 = (byte ) i1 ;
The part "(byte)" is called a type cast and converts "i1" to a "byte" type . Why did Java force us to do a type cast. Well let's see what the value was that got printed out for i1 equal to 128 was -128 .
File: "var2.java"
public class var2
{
public static void main( String args[] )
{
int a = 10 ;
int b = 20 ;
System.out.println("sum=" + ( (a + b) * 2 ) );
System.out.println( "sum:" + a + b ) ;
System.out.println( a + b + " :string" ) ;
// a = b / 0 ;
double d1 = 3.5 ;
// float f1 = 3.0 ;
System.out.println( a / 4 ) ;
System.out.println( a / 4.0 ) ;
System.out.println( d1 / 4 ) ;
System.out.println( (float) ( 12 / 5) ) ;
}
}
Output:
sum=60
sum:1020
30 :string
2
2.5
0.875
2.0
Let us study the output.
System.out.println("sum=" + ( (a + b) * 2 ) );
Output:
60
If we have the string on the left hand side then the right hand side is converted to a string also. If we want to do arithmetic computation we should use brackets to enclose our expression. Remember the brackets expression is always evaluated first.
System.out.println( "sum:" + a + b ) ;
produces:
sum:1020
The string "sum:" converts the number "a" to a string and makes a new string "sum:10" . The next number "b" is concatenated to the string and we get "sum:1020" .
System.out.println( a + b + " :string" ) ;
produces:
"30 :string"
In the above case the arithmetic expression is evaluated as there is no string to the left of it.
// a = b / 0 ; //Run time exception
If we uncomment the above line then the program will compile but when we run it we will encounter an Exception and the program will stop running.