Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive conversion

 
joy
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
jyoti Agrawal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
 
Suma Narayan
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • 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).]
 
Herbert Maosa
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic