Java Reference
In-Depth Information
double x=2; // implicit casting int->double
double x=( double )2; // explicit casting
long x=2.0; // implicit casting double->long
double x=2.0d; // The constant 2.0 is declared of type double
// add a 'd' after the number
The implicit casting rules of primitive types are summarized in Figure 1.1.
double
float
long
char
int
short
byte
Figure 1.1 Implicit casting rules of primitive types
For example, consider the following code snippet that implicitly casts a
character (of type char ) into its corresponding ASCII integer code:
char c='X';
int code=c;
System.out.println(code);
We get the ASCII code 88 for the capital letter 'X'.
There are slight restrictions on the names of variables that should not begin
with a digit, nor bear the name of a reserved keyword of the language. Trying
to use a reserved keyword for the name of a variable will result in a compiler
error as illustrated in the following code:
Program 1.11
Variable names should not belong to the list of reserved
keywords
class ReservedKeyword
{ public static void main ( String arg [ ] )
{
double x,y;
// Generate a syntax error:
// "not a statement"
int import ;
}
}
 
 
Search WWH ::




Custom Search