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

wrapper class program error

 
Aneesh Kannel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me whats wrong with my code?
I am Getting an Error like this.
/*
Error

WrapperTest.java:7: cannot find symbol
symbol : class valueOf
location: class java.lang.Integer
Integer i3=new Integer.valueOf("101011",2);
^
WrapperTest.java:8: cannot find symbol
symbol : class valueOf
location: class java.lang.Integer
Integer i4=new Integer.valueOf("101011",8);
^
WrapperTest.java:9: cannot find symbol
symbol : class valueOf
location: class java.lang.Integer
Integer i5=new Integer.valueOf("101011",16);
^
WrapperTest.java:23: cannot find symbol
symbol : class valueOf
location: class java.lang.Float
Float f3=new Float.valueOf("3.14f");
^
4 errors

*/
Coding Below

class WrapperTest
{
void inttest()
{
Integer i1=new Integer(42);
Integer i2=new Integer("42");
Integer i3=new Integer.valueOf("101011",2);
Integer i4=new Integer.valueOf("101011",8);
Integer i5=new Integer.valueOf("101011",16);

System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);

}

void floattest()
{
Float f1=new Float(3.14);
Float f2=new Float("3.14");
Float f3=new Float.valueOf("3.14f");

System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
}

void booleantest()
{
Boolean b1=new Boolean(false);
Boolean b2=new Boolean("false");

System.out.println(b1);
System.out.println(b2);
}

public static void main(String arr[])
{
WrapperTest wt=new WrapperTest();
wt.inttest();
wt.floattest();
wt.booleantest();
}
}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the lines that look like

Float f3=new Float.valueOf("3.14f");

should just be

Float f3=Float.valueOf("3.14f");

I've removed the "new". The keyword "new" is for creating objects and invoking their constructors; you don't use it to call an ordinary method which returns an object.

Note that inside the code for these "valueOf()" methods you would find a use of "new" to create the objects these methods return.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Note that inside the code for these "valueOf()" methods you would find a use of "new" to create the objects these methods return.

Not necessarily. The Byte, Short, Integer and Long classes creates wrappers for all numbers from -128 to 127 when the class is loaded, and returns those when possible. For every other number a new object is created. Character does the same but without the negative part, as negative characters are not possible.
Float and Double do just return a new Float/Double object each time.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but even so, those cached objects were originally created with the new keyword. It's just that this happened when the class was loaded & initialized, not when valueOf() was called. And either way, "new" does occur in the code, even if it's not executed every time valueOf() is called.
[ November 06, 2007: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic