• 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 Class Problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please correct me ,can I do something like this .

1: Byte b =new Byte((byte) 8);
Byte b1=new Byte(b);//2


2: Byte b =new Byte((byte) 8);
byte byteval=b;

3:
// this is the example from K&B book which says an error at line 1
but if I try to compile it executes fine .

a) int intval=Integer.valueOf("345");//line 1
System.out.println(intval);

b) Float f=new Float(12.3);
float f1=Float.valueOf(23.4f);
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kasak Tahilramani:
Please correct me ,can I do something like this .

1: Byte b =new Byte((byte) 8);
Byte b1=new Byte(b);//2


2: Byte b =new Byte((byte) 8);
byte byteval=b;

3:
// this is the example from K&B book which says an error at line 1
but if I try to compile it executes fine .

a) int intval=Integer.valueOf("345");//line 1
System.out.println(intval);

b) Float f=new Float(12.3);
float f1=Float.valueOf(23.4f);



The valueOf methods of Integer return a reference to an Integer object. If you are using a JDK >= 1.5, then autoboxing will automatically convert the Integer to an int.

However, if you use a JDK < 1.5, then you will get a compile-time error on line 1.

The same thing is true for Byte. JDK >= 1.5 will automatically convert a Byte to a byte.

Also if JDK >= 1.5, then a Byte will automatically be converted to a byte so that you can call the constructor of Byte that accepts a byte.
[ December 31, 2006: Message edited by: Keith Lynn ]
reply
    Bookmark Topic Watch Topic
  • New Topic