• 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

primitive conversion

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) byte b=123 //this works 123 is an int but you do not need a cast for it to be converted to a byte since 123 is in the range of -127 to 128.
2) Byte b = Byte(123);//would generate a compile error explicit cast needed to convert int to byte.But this worked fine in 1).
In RHE it said that the rules for conversion in Assignment is the same in Method Call why wont no.2 work?
thanks,
joy
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

The constructor for the Byte takes only byte as an argument or string as the argument.
in your question u have said Byte(123)
123 is an int which cannot be downcasted to byte or be converted to string.Hence it is giving u the error.
i hope your doubt is cleared now.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi joy,
Jyothi, has already told you the reason for the error you are getting. I am just adding few details.
There are two ways to construct a wrapper class object.
1. By passing the value to be wrapped into the appropriate constructor. For e.g
boolean bool = true;
Boolean bn = new Boolean(bool);

so the constructor is of the form,
Boolean bn = new Boolean(boolean bool);
In the example mentioned by you, it should be constructed in
the following way.
byte b = 123;
Byte bt = new Byte(b);
2. You can pass into the constructor a string that represents the value to be wrapped. The only exception to this way of constructing is the wrapper class Character.
So you can also say,
Byte bt = new Byte("123");
Note:
There is always a possibility that a string will not represent a valid value. In such cases, exception "NumberFormatException" is thrown. Only Boolean does not throw this exception.
I hope the above explanation clears your doubt.
Suma.
[This message has been edited by Suma Narayan (edited May 23, 2000).]
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joy,
I just want to add on what has been said already. Specifically I want to correct you on this statement and I quote :
----------------------------------------------------------------
1) byte b=123 //this works 123 is an int but you do not need a cast for it to be converted to a byte since 123 is in the range of -127 to 128.
---------------------------------------------------------------
123 is not an int in the snipet above. If it were explicitly defined as an int prior to the assignment, that assignment would not succeed and would raise a Compiler error, even though 123 can fit in a byte. Here is works OK because 123 is used as a literal whose size fits in a byte.
Regds,
Herbert
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic