aspose file tools*
The moose likes Cattle Drive and the fly likes placement of cast within expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "placement of cast within expressions" Watch "placement of cast within expressions" New topic
Author

placement of cast within expressions

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi all,
I got some funny results of a cast used within an expression - maybe someone can explain why this happens...
int oneIntVariable = ( (int)myLongVariable % anotherIntVariable ) ;
The above line of code resulted in strange values for (int)myLongVariable and, consequently, oneIntVariable.
I got the desired cast to work only when I placed it outside the expression, like this:
int oneIntVariable = (int)( myLongVariable % anotherIntVariable ) ;
Adding inner parentheses like this
int myVariable = ( ( (int)myLongVariable ) % myIntVariable ) ;
got me the same strange values for (int)myLongVariable and myVariable.
Any hints? Thanks once again,
Pauline

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
When you cast a long to an int, it just chops off the front of the number, it doesn't round it off. For example, if you cast the binary long
0000 0000 0000 0000 0000 0000 1111 0000 0000 0000 0000 1111 0000 0000 0000 0000
to an int, the compiler assumes that you know what you are doing because you are using an explicit cast and chops off the first 32 bits, leaving you with
0000 0000 0000 1111 0000 0000 0000 0000

If you try to assign a long to an int without explicitly casting it, the compiler will generate a warning.

This can get really weird sometimes. If the number has 1's in certain spots, you could end up with a negative number.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Yeah, it was weird alright. Didn't get any compiler errors, but when I tested for the actual value of the (int)myLongVariable, it was getting big negative values. But this happened only when the cast was placed directly next to it and not outside the expression parens.
I still don't understand why the one way works and the other two don't. Dunh...
Does it have something to do with precedence?
Of course, the basic problem is "...the compiler assumes you know what you are doing..."
Ha!

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
The key is that you have to get the number small enough to fit into an int before you do the cast. Isn't it amazing how powerful parens can be.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Aha! thanks, Marilyn.
hmmm, powerful parens - how fitting for mother's day
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: placement of cast within expressions