Results 1 to 6 of 6
Thread: about String..~
- 07-14-2011, 07:21 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
about String..~
about genetics ..~
Java Code:public class Box{ private Object object; public void add( Object object ){ this.object = object; } public Object get(){ return object; }According to Introduction (The Java™ Tutorials > Learning the Java Language > Generics) , there are error because of cast exception due to java.lang.String cannot be cast to java.lang.Integer .Java Code:public class BoxDemo{ public static void main(String[] args ){ Box integerBox = new Box(); integerBox.add("10"); Integer someInteger = (Integer)integerBox.get(); System.out.println(someInteger); } }
i really cant understand why "10" cant be converted into 10 ..~
- 07-14-2011, 07:23 PM #2
It can be done very easily. Look at the Integer class for a method to do that.really cant understand why "10" cant be converted into 10
However the compiler is a pretty simple program and is not going to try to figure out what you want to do. You must tell it explicitly with the correct code.
For the case of converting an int to an Integer (called boxing) the compiler will do it for you.
- 07-14-2011, 07:32 PM #3
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
"10" is a String type that contains two UNICODE characters, the number 10 is a numeric type of value 10. Java has been designed so that conversions between different types are not automatic, but must be explicitly requested. This reduces the number of coding errors and makes programs more robust.
- 07-14-2011, 07:33 PM #4
I take everything literally. That makes me stupid.about genetics ..~
The computer takes everything literally. That makes it stupid. That's where programmers come in...i really cant understand why "10" cant be converted into 10 ..~No bug ever had to calculate its fitnessfunction.
- 07-14-2011, 07:43 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 9
- Rep Power
- 0
o..~ i tot the compiler will automatically convert "10" to 10 for me..~
i just need to change [ integerBox.add("10") ] become [ integerBox.add(Integer.parseInt("10")) ] ?
- 07-14-2011, 07:48 PM #6
Similar Threads
-
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
The constructor Person(String, String, Date) is undefined
By fh84 in forum New To JavaReplies: 7Last Post: 11-03-2009, 02:18 AM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks