CONVERSIONS AND PROMOTIONS
Conversions and Promotions
5
Casting conversion ( 5.4) converts the type of an expression to a type explic 
itly specified by a cast operator ( 15.15). It is more inclusive than assignment
or method invocation conversion, allowing any specific conversion other than
a string conversion, but certain casts to a reference type may cause an excep 
tion at run time.
String conversion ( 5.4,  15.17.1) allows any type to be converted to type
String
.
Numeric promotion ( 5.6) brings the operands of a numeric operator to a
common type so that an operation can be performed.
Here are some examples of the various contexts for conversion:
class Test {
public static void main(String[] args) {
//
Casting conversion ( 5.4) of a
float
 literal to
//
type
int
. Without the cast operator, this would
//
be a compile time error, because this is a
//
narrowing conversion ( 5.1.3):
int i = (int)12.5f;
//
String conversion ( 5.4) of
i
's
int
 value:
System.out.println("(int)12.5f==" + i);
//
Assignment conversion ( 5.2) of
i
's value to type
// float
. This is a widening conversion ( 5.1.2):
float f = i;
//
String conversion of
f
 s
float
 value:
System.out.println("after float widening: " + f);
//
Numeric promotion ( 5.6) of
i
's value to type
// float
. This is a binary numeric promotion.
//
After promotion, the operation is
float*float
:
System.out.print(f);
f = f * i;
//
Two string conversions of
i
 and
f
:
System.out.println("*" + i + "==" + f);
//
Method invocation conversion ( 5.3) of
f
's value
//
to type
double
, needed because the method
Math.sin
//
accepts only a
double
 argument:
double d = Math.sin(f);
//
Two string conversions of
f
 and
d
:
System.out.println("Math.sin(" + f + ")==" + d);
}
}
53






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved