• 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

Why Byte boxing doesn't work here

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's the output?

class Fruit{
Fruit(){System.out.println("Super constructor executing just before any Initialization blocks in child");}

}
class Banana extends Fruit {
static {System.out.println("Static first");}
Byte x = 1; // use, byte x=1, instead to make it compile
public static void main(String [] args) {
int x = 2;
Banana b = new Banana();
b.go();
}
{ x += x; }
void go() {
++x;
System.out.println(x);
}
}

Why is the output as follows:

Banana.java:13: inconvertible types
found : int
required: java.lang.Byte
{ x += x; }
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Simple reason is that Byte constructor take an argument of type byte.



Will this compile or not? See There is only one constructor defined in Byte class and whose signature is Byte(byte b).

In above code, It tries to call Byte(1). Remember integer literals are by default 32 bit size..

In method or constructor call, ALWAYS most specific will be called. If most specific is not there, then in that case value will be promoted but no implicit narrowing.

It can't implicitly narrow the value of 1 (32 bit ize) to byte which is 8 bit.

regards

Naseem.K
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of the compiler are you using?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code compiles on Java 5 compiler.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code works for me.
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have version 1.5.0_06. How come, some here were able to compile it but not me? I use "javac -cp . Banana.java" to compile the code above (which has Byte). I know about narrowing (from int to Byte) should make it break. But some had it working!! It's strange that narrowing doesn't seem to apply to bytes and ints. But here it seems it matters?!! Like byte z=1; works!!! Is this narrowing logic consistent and applied here (or not applicable to bytes and ints)? Besides 1 fits fine in int value.

It's only when using Byte it breaks. The following executes fine:

class Fruit{
Fruit()
{System.out.println("Super constructor executes just before any instance"+
"initialization blocks in child");}
}

class Banana extends Fruit {
static {System.out.println("Static first");}
byte x = 1; // using Byte x=1 results in compilation error, why?
public static void main(String [] args) {
int x = 2;
Banana b = new Banana();
b.go();
}
{ x += x; }
void go() {
++x;
System.out.println(x);
}
}

Output:

Static first
Super constructor executes just before any instance initialization blocks in child
3
[ May 23, 2006: Message edited by: Firas Zureikat ]
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried javac -version to make sure that javac refers to the correct version?
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I did as follows:

C:\myJava\source>javac -version Banana.java
javac 1.5.0_06
 
Firas Zuriekat
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte Unboxing doesn't work here and I don't know why.

Is it really because Byte constructor doesn't promote an int to a Byte? Byte Constructor signature not used correctly is the issue?

It's not an issue about an "int" not fitting in a "byte" because the following compiles fine:

class Fruit{
Fruit(){System.out.println("Super constructor executes just before any instance initialization blocks in child");}

}
class Banana extends Fruit {
static {System.out.println("Static first");}
byte x = 1; // using Byte x=1 results in compilation error, why?
public static void main(String [] args) {

byte z=1;
Byte Y=1; // Some said it's a narrowing issue that result in compile error. But this compile fine (1 fits in Bye)
int x = 2;
Banana b = new Banana();
b.go();
}
{ x += x; }
void go() {
++x;
System.out.println(x);
}
}
[ May 23, 2006: Message edited by: Firas Zureikat ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had such problem here using Java 1.5.0_06. I made lots of tests and came to some conclusions.

Please, correct me if I am wrong because that is important to all of us.


Any combination of <primitive += primitive> or <primitive += wrapper> can be made because the += operator implicitly casts the result of the operation to left side primitive type. Here are some strange examples:

Double D = 1.0;
Character C = 1;
byte b = 1;

b += C; // OK
b += D; // OK

Using wrappers on the left side of += operator (like <wrapper += wrapper> or <wrapper += primitive> , it seems to work like an ordinary addition operation, where its result will be cast implicitly to int, long, float or double, depending on the primitives involved on the operation (at least one of them is an unboxed wrapper). Then, some combinations are not allowed, like

Integer I = 1;
Double D = 1.0;

float f = 1.0f;

I += D; // Compiler fails � the result is a double
I += f; // Compiler fails � the result is a float

(But that is valid with primitive on the left side of += operator, that�s it, if variable 'I' were an int instead of an Integer wrapper.)

Maybe that explains why Byte, Short and Character wrappers do not work with += operator, since they can not get assigned to any of those primitives without explicit cast (narrowing).
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C:\javasrc>java -version
java version "1.5.0_04"

Following is the compiler error i am getting.

C:\javasrc>javac Banana.java
Banana.java:15: inconvertible types
found : int
required: java.lang.Byte
++x;x += x;
^
1 error
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic