Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting question

 
Phil Freihofner
Ranch Hand
Posts: 120
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both Winston and Mansukhdeep!
My apologies for the "long lines".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic