13 Replies Latest reply: Aug 31, 2007 11:08 PM by 807600 RSS

    why the typecasting from string to int is diffrent from others

    807600
      i want 2 ask one thing sir .....
      why the typecasting from string to int is diffrent from others
      Integer.parseInt(str)
      i.e...
      for string to float , str to boolen are alike......
      Float.valueOf(Str).floatValue();
      Boolean.valueOf(Str).booleanValue();
      why it is diffrent for int ....... ?why not all are alike ?

      Message was edited by:
      java_jesh
        • 1. Re: why the typecasting from string to int is diffrent from others
          807600
          I think you may have a basic misunderstanding here.

          1) I believe that this is not typecasting but conversion from string to numeric iow, parsing that you are doing.
          2) I don't think that Float has a valueOf method. You have it backwards I think. String has the valueOf method.
          3) Float does in fact have a parseFloat(String s) method.

          Have a look at the String, Integer, Float, and Boolean api's and you'll see.
          • 2. Re: why the typecasting from string to int is diffrent from others
            807600
            yes......
            its not typecasting.for type conversion .............
            sorry i typed wrongly

            Message was edited by:
            java_jesh
            • 3. Re: why the typecasting from string to int is diffrent from others
              807600
              my mistake, Float and Boolean do have valueOf methods. Sorry

              but, Float's valueOf takes a float parameter. Boolean's valueOf takes a boolean parameter.

              Integer has a valueOf that takes an int param.

              Message was edited by:
              petes1234
              • 4. Re: why the typecasting from string to int is diffrent from others
                807600
                Float's valueOf takes a float parameter
                no
                sir ....i have learned that
                Float.valueOf()
                take string as arguments
                • 6. Re: why the typecasting from string to int is diffrent from others
                  807600
                  Float.parseFloat(String) and Float.valueOf(String) do the same thing. Integer has a couple of methods that also duplicate one another.
                  • 7. Re: why the typecasting from string to int is diffrent from others
                    807600
                    d?mn, I'm hanging it up for the night.

                    You are right of course. But then Integer has a valueOf(String s) method as well. I'm missing out on the asymmetries.
                    • 9. Re: why the typecasting from string to int is diffrent from others
                      807600
                      Float.parseFloat(String) and <float>.floatValue(Float) return floats--that is, primitive values. Float.valueOf(float) and Float.valueOf(String) return Float objects. The differences aren't as important as they were before autoboxing, but they are different. The same pattern occurs in all of the wrapper classes: Boolean, Byte, Short, Integer, Long, and Double.
                      • 10. Re: why the typecasting from string to int is diffrent from others
                        807600
                        but they are different
                        Yes - you're quite right, of course.
                        • 11. Re: why the typecasting from string to int is diffrent from others
                          807600
                          Float.parseFloat(String) and <float>.floatValue(Float) return[b] floats
                          Float.valueOf(float) and Float.valueOf(String) return Float objects

                          yes i could undestand the difference..........
                          but we can manage with any one (for conversion purpose)
                          then why we have two ?
                          • 12. Re: why the typecasting from string to int is diffrent from others
                            807600
                            So long as you are converting, or doing something that involves the values of the numbers involved there is not a lot of difference (especially as UA says with boxing and unboxing.)

                            But they are not quite the same...

                            If you send the same string to parseInt() twice, the returned ints will compare equal with ==.

                            But if you send those strings to valueOf(), the returned Integers won't compare equal with ==.

                            Here is an example:
                            import java.util.IdentityHashMap;
                            import java.util.Map;
                            
                            public class ValueEg {
                                 
                                static Map<Integer,String> map = new IdentityHashMap<Integer,String>();
                                 
                                public static void main(String args[]) {
                                    parseAndAdd("1", "a");
                                    parseAndAdd("1", "b");
                                    parseAndAdd("2", "c");
                                    System.out.println("size is " + map.size()); // 2
                                      
                                    map.clear();
                                    addValue("1", "a");
                                    addValue("1", "b");
                                    addValue("2", "c");
                                    System.out.println("size is " + map.size()); //3
                            
                                    map.clear();
                                    parseAndAdd("10000", "a");
                                    parseAndAdd("10000", "b");
                                    parseAndAdd("20000", "c");
                                    System.out.println("size is " + map.size()); //3
                                      
                                    map.clear();
                                    addValue("10000", "a");
                                    addValue("10000", "b");
                                    addValue("20000", "c");
                                    System.out.println("size is " + map.size()); //3
                                }
                                 
                                static void parseAndAdd(String key, String val) {
                                    map.put(Integer.parseInt(key), val);
                                }
                            
                                static void addValue(String key, String val) {
                                    map.put(Integer.valueOf(key), val);
                                }
                            }
                            The problem is that the IdentityHashMap is a strange sort of Map that compares keys using identity (==) rather than value (equals()). When we add entries using valueOf() we are get three different Integers to use as keys. When we parse the string into an int we only get two different ints that are "boxed" into two or three different Integers depending on how big they are.
                            • 13. Re: why the typecasting from string to int is diffrent from others
                              807600
                              Before autoboxing, we had to do conversions between primitives and wrapper objects manually:
                                List listOfInts = new ArrayList(); // no generics, either :-(
                                int n0 = 42;
                                listOfInts.add(new Integer(n0));
                                ...
                                int n1 = ((Integer)listOfInts.get(0)).intValue();
                              With autoboxing and generics, we can write that much more concisely:
                                List<Integer> listOfInts = new ArrayList<Integer>(); // OK, this part's longer :-/
                                int n0 = 42;
                                listOfInts.add(n0);
                                ...
                                int n1 = listOfInts.get(0);
                              But, along with autoboxing, they introduced caching for the integral types, which is why they added the valueOf(int) method. (Float and Double aren't cached, but they have valueOf(float/double) methods for the sake of consistency--or so I assume.) As for parseInt(String) and valueOf(String), one returns a primitive and the other, a wrapper object--a distinction, as I said, that used to matter much more than it does now.