This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

What's up with these literal assignments?

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello once again ranchers !
I've been gone for a while, but I'm glad to be back certainly.

You know I have read that any literal assignment when talking about numbers is implicitly an int.
Does the compiler have to do a (implicit) Cast of some sort from int to another type in a case like the one below?:



Is the number on the right of the equals an int that needs to be Cast & then boxed ?

That'd be it for today,
Thanks in advance and good luck!
Sincerely,
Jose
 
Marshal
Posts: 74745
336
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That sort of question comes up quite often here.

You are obviously using J5 or J6, and you are using autoboxing. In an initial assignment of a byte you can put a number to the right of the assigns (=) sign, and the compiler will presume it is supposed to be a byte. Provided it is in the range -0x80 to 0x7f (-128 to +127), without needing to write a cast.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer literals are indeed ints, but there is a special case for byte.

If you assign a value to a byte (or Byte, via autoboxing), and the value that you are assigning is a compile-time constant, then the compiler can check at compile-time if the value is in the range for the byte data type (-128 to +127), and then you can do this without casting.

If the value is not a compile-time constant, then you must use a cast. For example:

[ December 15, 2007: Message edited by: Jesper Young ]
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,

Whoa!, That's interesting. Really interesting. For a minute I thought I asked a dumb-question; But it's good to see that there actually is more than meets the eye when talking about assignments.
I'm satisfied with the explanation guys, I could never have figured it out on my own,

Thank you, your work is always appreciated!

Sincerely,
Jose
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic