• 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
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

final local int narrowing and autoboxing

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below code compiles fine, but when I run it I get the follwing error:
Exception in thread "main" java.lang.VerifyError: (class: TestConvert, method: m
ain signature: ([Ljava/lang/String;)V) Accessing value from uninitialized regist
er 1


I know its because of the line marked with //(1) ...but I thought "i" is a compile time constant integer which will be automaticly narrowed to byte and then autoboxed to Byte .. but this dosen't happen ... why??
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Compiles and run fine for me.
 
Saloon Keeper
Posts: 14018
316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope. Autoboxing only works between a wrapper class and its respective primitive. If you want to box an int to a Byte or a Character, either cast it to byte or char first, or even better, use the wrapper class' valueOf method.

[edit]

Apparently not. Care to shed some light on this, Henry? I get the same error as the original poster, and it won't even compile if I try to store i into a Long.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry Wong , my practice shows it will cause error at runtime.

Stephan van Hulst wrote:Nope. Autoboxing only works between a wrapper class and its respective primitive. If you want to box an int to a Byte or a Character, either cast it to byte or char first, or even better, use the wrapper class' valueOf method.

[edit]

Apparently not. Care to shed some light on this, Henry? I get the same error as the original poster, and it won't even compile if I try to store i into a Long.


but Stephan if I changed the code into:

it will comile and run fine.
and you know this assignment process contains narrowing from "int" to "byte" and then autoboxing to "Byte" ... so again why?
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.



Don't know what to tell you... I cut and pasted your code. Compiled it. Ran it. No errors.... BTW, I am using Java 6, in case you are interested.

Henry
 
Henry Wong
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Apparently not. Care to shed some light on this, Henry? I get the same error as the original poster, and it won't even compile if I try to store i into a Long.



It's a special case for autoboxing. The JLS allows boxing to occur for different types, if the assignment is from a compile time constant (and it is deemed that the value will fit into the type to be assigned).

As for the compile error with Long, the JLS doesn't include long in the case -- don't know why.

Henr
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified the code and added appropriate casts.



When i did put in external byte and char casts, then i got the following output:


It seems that we cannot convert an int directly into a Byte or Character. An external cast is required.
However, i too am not very sure if that's true.
I am assuming that from the output i got.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.



Don't know what to tell you... I cut and pasted your code. Compiled it. Ran it. No errors.... BTW, I am using Java 6, in case you are interested.

Henry


Dear Henry, I'm using the following java and javac versions:
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

javac 1.6.0
Ranchers, can you help us by trying compile and run the originally posted code?
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiles, and run fine for me.

seems very close to previous thread
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bharath Raja wrote:compiles, and run fine for me.

seems very close to previous thread



Thanks very much Bharath Raja, I'm fine with this

Adam Michalik wrote:I have posted a bug to the Sun Java Bug Database: Bug ID = 6816548.



I will try to fix my javac
Regards
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compiles and run fine for me too.

BTW if you remove final modifier from int i then it will generate error.

why?

I am not sure but i think that making final to a code will fix its value which in future cann't be changed.
If not changed and the value is in range of byte then it compiles successfully.

also try with
final int i = -30;
Byte b = i; // will compile fine
Character c = i;//will generate error as i can't be changed and it is negative. so, it is not under the range of character.

Please provide view on final and its casting
Thanks
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep Chandra wrote:compiles and run fine for me too.

BTW if you remove final modifier from int i then it will generate error.

why?

I am not sure but i think that making final to a code will fix its value which in future cann't be changed.
If not changed and the value is in range of byte then it compiles successfully.

also try with
final int i = -30;
Byte b = i; // will compile fine
Character c = i;//will generate error as i can't be changed and it is negative. so, it is not under the range of character.

Please provide view on final and its casting
Thanks


Dear Pradeep, for your first question,from A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer Third Edition by Khalid A. Mughal and Rolf W. Rasmussen
"Additionally, implicit narrowing primitive conversions on assignment can occur in
cases where all of the following conditions are fulfilled:
• the source is a constant expression of either byte, short, char, or int type
• the target type is either byte, short, or char type
• the value of the source is determined to be in the range of the target type at
compile time"
and since when you take out the final keyword you will get a local variable not a compile time constan then you will sure have the compile error.
 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.



Don't know what to tell you... I cut and pasted your code. Compiled it. Ran it. No errors.... BTW, I am using Java 6, in case you are interested.

Henry


Dear Henry, when i used NetBeans IDE 6.8, it copmiles and run very well.. thanks very much
 
Prakash Mahto
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Imad
 
pie. 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