Hi, experimenting with type conversions I'm having some trouble.
In assignment context narrowing primitive followed by boxing conversion should be allowed according to JLS (fi is a constant expression at compile time of type int, variable B is of type Byte and 0 is representable in the type byte), instead I get an error at runtime.
$ javac AssignmentTest.java
$ java AssignmentTest ### java version: "1.5.0_15"
Exception in thread "main" java.lang.VerifyError: (class: AssignmentTest, method: main signature: ([Ljava/lang/String;)V) Accessing value from uninitialized register 3
Would you please explain what's going on?
Btw, JLS also says that "the only exceptions that an assignment conversion may cause are OutOfMemoryError, ClassCastException and NullPointerException", VerifyError is not even mentioned.
Can you try to initialize the variable that holds the byte data type?
posted 11 years ago
Sorry I don't understand. Are you talking about the comment at line 08 in my original post? In this case I think I have nothing to initialize, because the conversion narrowing + boxing happens automatically and in one step, or at least it should (but maybe is just here where I'm wrong...).
Doing the conversion in two steps solves the problem, but I think that my original B = fi; should work too, and automatically in one step only.
I guess this error is something I can't fix easily or without alchemy (Gamini I'm talking to you ;-).
At this point I don't even *really* care why the verifier complains, API says it probably it's about an internal inconsistency and I'm fine with this. Most important thing I'd like to know: is this error triggered by me due to an incorrect use of one step narrowing + boxing conversion in assignment context? Or to put it another way, I thought the original program should run out-of-the box but it doesn't. Is it my fault?
Thank you again!
posted 11 years ago
Don't know. Try printing the bytecode with
javap -c MyClass
See how that compares with narrowing initialisation and boxing in two stages.
Of course you're right, Rob. It's not easy to write a reasonable post on a Friday afternoon, five minutes before leaving work
Anyway, I would say that the latter bytecode is correct... Any ideas how come that once the (standalone) compiler generates bytecode #1 and the other time (Eclipse) #2?