This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Narrowing down of values while assigning

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have a very basic question, which is often confuses me. This is about Narrowing down of the literal values.

for eg;,

Byte b=127; // No error

Byte b=new Byte(127); //Compile error , byte expected but used int

If my assumption is correct, in the first case, value 127 is a integer literal and probably compiler would have narrowed it down to byte and autoboxed into Byte object.

But why is this not happening in the second case. Is my assumption about autoboxing and Narrowing down is wrong. Please correct me if possible.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

The simple rule to remember is that Byte constructor will take a byte or a string literal of byte value.

When you say
Byte arg = new Byte(127);

you're passing the Byte constructor an int literal which it cannot autobox to a Byte wrapper.

Anup
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but you could say
Byte b2 = new Byte(b);
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the "rules" applying to this situation have to do with conversions, as described in the Java Language Specification.

Assignment conversion occurs when the value of an expression is assigned (�15.26) to a variable...

...if the expression is a constant expression (�15.28) of type byte, short, char or int ... a narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.


Ref: 5.2 Assignment Conversion.

Method invocation conversion is applied to each argument value in a method or constructor invocation...

Method invocation conversions specifically do not include the implicit narrowing of integer constants which is part of assignment conversion (�5.2).


Ref: 5.3 Method Invocation Conversion.
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic