• 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

wrapper classes

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when we say
byte b = 1;

we dont get any compiler error

but look at the following code:



why do i get error at line no 3?

2)does Float class constructor accepts double type arg???
[ June 14, 2006: Message edited by: alwin das ]
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Das

when u say byte b=1; it will not generate th complier error bcoz if

"at any time if u assign an integral value to any of the primitive types(byte,char,short) and the right hand side value is within the range of the left hand side datatype ,it will not generate an error"

hence as 1 is within the range of byte ,it is not generating compiler error.


Byte b2 = new Byte(1); is generating error bcoz u r passing an integer to the constructor of the Wrapper class Byte , complier will not do the implicit casting here.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vikram Kohli:
...at any time if u assign an integral value to any of the primitive types(byte,char,short) and the right hand side value is within the range of the left hand side datatype ,it will not generate an error...


If the right-hand is a variable, then it must be final with the value known at compile time. Otherwise, the conversion ("implicit cast") will not work. See this thread.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But I tried the follwoing, it still works:

public static void main (String[] args)
{
byte pb=(byte)(Math.random()*10); //pb is not final here
Byte b1=new Byte(pb);
System.out.println(b1.byteValue());

}
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roy, "pb" is not a "right-hand" variable;


If the right-hand is a variable, then it must be final with the value known at compile time. Otherwise, the conversion ("implicit cast") will not work.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does Float class constructor accepts double type arg???
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried giving a double value to a Float constructor , it does compile and run
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic