• Post Reply Bookmark Topic Watch Topic
  • New Topic

casting question  RSS feed

 
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the below code the line Integer d1=(Integer)sqrt(f); gives compile time error. But the line int d2=(int)sqrt(f); compiles fine. Why? Also in which situations does auto boxing/unboxing take place in Java?

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the sqrt method come from, what is its method signature (what types of argument and return value does it have)?

What exactly does the error message say?
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Math class. The complete code is below. The sqrt method returns double and is in-built method. My specific question is why using typecast with the sqrt method as a primitive int compiles fine but gives compile time error when typecasted as an Integer object?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are using Static Imports for java.lang.Math.*;

sqrt takes double and return double.
1. double can be down casted to int if you are ok with data loss. 8 bytes to 4 bytes conversion.
2. double can cased to Double
3. double/Double can not be casted Integer object.  Integer can not be constructed by taking a double. Integer(double b) does not exist. You can use Double.intValue();

Autoboxing only applies primitive type and its corresponding wrapper class.
1. int <-> Integer
2. double <-> Double.
3. Integer # Double

example:
Integer i =10;
int j = i++;


 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Side note. Or maybe it is directly related to this topic -- not sure.... anyway, this topic is about explicit casting -- ie. not about implicit casting.

With explicit casting, you can only do one step at a time. So, while this...is not allowed (already explained), this is allowed...
Now interestingly, this is also allowed...The reason is ... a double can be explicitly cast to an int, and an int can be autoboxed to an Integer (implicitly for the assignment).

Henry
 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is ... a double can be explicitly cast to an int, and an int can be autoboxed to an Integer (implicitly for the assignment).

So you are saying that compiler can only do one thing implicitly? If we want the compiler to do both type casting and autoboxing together it will not do?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raja singh kumar wrote:
So you are saying that compiler can only do one thing implicitly? If we want the compiler to do both type casting and autoboxing together it will not do?


Implicit conversion for assignment is defined by section 5.2 of the JLS. And in that section, the combinations of casting and autoboxing are listed. The combination in your example is not on that list.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!