• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting Variables

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Head First Java (page 117)

A long is bigger than an int and the compiler can't be sure where that long has been. It might have been out drinking with the other longs, and taking on really big values. TO force the compiler to jam the value of a bigger primitive variable into a smaller one, you can use the cast operator. It looks like this:

long y = 42 // so far so good
int x = (int) y; // x = 42 cool!

My question is can we jam the value of a smaller primitive variable into a bigger primitive variable?
something like this:-

int x = 4;
long y = (int) x;

If yes, then how?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . // don't need a cast if you are up scaling . . .

. . . and that cast is redundant; you are simply casting something to the type it already has. It is of course permissible to cast something to the same type, just as other operations with no effect are also permitted, e.g. x = x;
 
Manish Pamnani
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:. . . // don't need a cast if you are up scaling . . .

. . . and that cast is redundant; you are simply casting something to the type it already has. It is of course permissible to cast something to the same type, just as other operations with no effect are also permitted, e.g. x = x;



SO, it's like pouring water from a small glass to a larger one?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's a great analogy.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish Pamnani wrote:. . . SO, it's like pouring water from a small glass to a larger one?

The cast is more like pouring water from one glass back into the same glass because the variable is an int before and after. The = assignment is like pouring the water into a larger glass, because it automatically changes the expression from an int to a long. The javac tool won't let you even try pouring a long into an int, because you are pouring the water from a large glass into a small glass and most of it will go onto the floor.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic