Tuesday, August 14, 2012
0
Learn Java Chapter 2
CONTIN... from learn java chapter 1
JAVA DATA TYPES
********************
Data type specify the size and type of value that can be stored.Java is rich in its data types the data types available allow the programmer to select the type appropriate to the need of the application.There are various categories are there.
Integer type can hold whole numbers such as 123,-96 and 56.39.The size of the value that can be stored depends on the integer data type.Java support four types of integer like short,byte,int,and long.Below shows the memory size and range of four integer data types
Floating point type:
Integer type can hold only whole numbers and therefore we use another type known as floating point type to hold numbers containing fraction part such as 23.23 and -1.29 therefore we can store two type of floating point
The float type value are single precision number while the double type represent double-precision number
Character type:
Java provides a character data type called char.The char assumes a size of 2 bytes but it can hold only single character
Boolean type:
Boolean used when we want to test particular condition during execution .There are only two values that boolean type TRUE or FALSE it is denoted by Boolean & use only one bit storage
Another program using data types
EXAMPLE PROGRAM
********************
class add
{
public static void main(String arg[])
{
int a,b,c;//Declares variables
a=20;
b=10;
c=a+b;
System.out.println("sum:"+c);//Display the output
}
}
OUTPUT:
*********
SUM:30
NOTE:
We encounter a situation where we need to store a value of one type into a variable of another type>in such situation we must cast the value to be stored by proceeding it with the type name in parentheses
JAVA DATA TYPES
********************
Data type specify the size and type of value that can be stored.Java is rich in its data types the data types available allow the programmer to select the type appropriate to the need of the application.There are various categories are there.
- Floating type
- Integer type
- Character type
- Boolean type
Integer type can hold whole numbers such as 123,-96 and 56.39.The size of the value that can be stored depends on the integer data type.Java support four types of integer like short,byte,int,and long.Below shows the memory size and range of four integer data types
Floating point type:
Integer type can hold only whole numbers and therefore we use another type known as floating point type to hold numbers containing fraction part such as 23.23 and -1.29 therefore we can store two type of floating point
The float type value are single precision number while the double type represent double-precision number
Java provides a character data type called char.The char assumes a size of 2 bytes but it can hold only single character
Boolean type:
Boolean used when we want to test particular condition during execution .There are only two values that boolean type TRUE or FALSE it is denoted by Boolean & use only one bit storage
Another program using data types
EXAMPLE PROGRAM
********************
class add
{
public static void main(String arg[])
{
int a,b,c;//Declares variables
a=20;
b=10;
c=a+b;
System.out.println("sum:"+c);//Display the output
}
}
OUTPUT:
*********
SUM:30
NOTE:
- Java is case sensitive
- Class name and the file name should be same.Here the file name should be given as add so file name should be add
We encounter a situation where we need to store a value of one type into a variable of another type>in such situation we must cast the value to be stored by proceeding it with the type name in parentheses
type variable1=(type) variable2;
Converting one data type to another is called casting.Four type can cast except Boolean.Casting into a smaller type may leads to loss of data.
Automatic conversion:
It is possible to assign a value of one type to a variable of a different type without a cast.Java does not the conversion of the assigned value automatically this is known as automatic conversion.
Automatic conversion:
It is possible to assign a value of one type to a variable of a different type without a cast.Java does not the conversion of the assigned value automatically this is known as automatic conversion.
Subscribe to:
Post Comments (Atom)
Post a Comment