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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Jeanne Boyarsky
• Devaka Cooray
• Paul Clapham
Sheriffs:
• Tim Cooke
• Knute Snortum
• Bear Bibeault
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Ganesh Patekar
Bartenders:
• Frits Walraven
• Carey Brown
• Tim Holloway

# Wrapper Class Problem

Ranch Hand
Posts: 45
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: 2410

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 ]

 It is sorta covered in the JavaRanch Style Guide.