aspose file tools*
The moose likes Beginning Java and the fly likes casting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting question" Watch "casting question" New topic
Author

casting question

Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
I noticed that I can't cast a Float to an int, but that if I multiply the Float by an int, then I am allowed to do the cast. I'm wondering what is going on in the Java logic that accounts for this.


The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?

I tried using instanceof as a test, and got two objections from the compiler:

I'm studying for the Java Associate Certification, and want to be sure I understand this. The question didn't come from a practice test, but some coding I'm doing. However, it seems like the tricky sort of stuff that shows up in the practice tests. Usually casting is between objects where one "is a" subclass of another. But having primitives involved is confusing me. How does the "is a" relationship pertain there?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Phil Freihofner wrote:I noticed that I can't cast a Float to an int, but that if I multiply the Float by an int, then I am allowed to do the cast. I'm wondering what is going on in the Java logic that accounts for this.


The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?

I tried using instanceof as a test, and got two objections from the compiler:



I'm studying for the Java Associate Certification, and want to be sure I understand this. The question didn't come from a practice test, but some coding I'm doing. However, it seems like the tricky sort of stuff that shows up in the practice tests. Usually casting is between objects where one "is a" subclass of another. But having primitives involved is confusing me. How does the "is a" relationship pertain there?



You can use to return only the integer part of the float primitive. Infact if you see JLS, all Wrapper classes have these xxxValue() apis. Does that solve your problem Phil?


~ Mansukh
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7794
    
  21

Phil Freihofner wrote:I noticed that I can't cast a Float to an int...

Phil,

Please DontWriteLongLines, even in comments. I've fixed it this time, but it makes your thread very hard to read.

@Mansukhdeep: Please don't copy (or quote) code with long lines for the same reason.

Thanks

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7794
    
  21

Phil Freihofner wrote:The best I can make out is that an operation using a Float creates a float value, not another Float, unless you assign the result to a Float. I'm guessing this generalization works for all primitive Wrappers?

Pretty much, but there are some gotchas.

In this case, f (your Float) is first unboxed to a float, and the result of float * int is a float.

I tried using instanceof as a test, and got two objections from the compiler:

Because instanceof requires an object as its left-hand operand; and primitives are not objects.

having primitives involved is confusing me. How does the "is a" relationship pertain there?

It doesn't, for the reason given above.

Winston
Phil Freihofner
Ranch Hand

Joined: Sep 01, 2010
Posts: 115
    
    1
Thanks to both Winston and Mansukhdeep!
My apologies for the "long lines".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting question