How the following java statement works?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itiger
    New Member
    • Sep 2010
    • 12

    How the following java statement works?

    Code:
    System.out.println(3.2 + " 4.5");
    The value 3.2 is of what type (double or String). If it is 'double' then is it possible to typecast 'double to String'. If it String then please explain why it is String.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the 3.2 is a double and the " 4.5" is a String
    the 3.2 is converted to a String and concatenated (the + operator) with the " 4.5" so when run prints
    Code:
    3.2 4.5

    Comment

    • itiger
      New Member
      • Sep 2010
      • 12

      #3
      Is it true that all the things written inside the "System.out.pri nt()" is automatically gets converted into the String type.

      Comment

      • nathj
        Recognized Expert Contributor
        • May 2007
        • 937

        #4
        Where possible Java will use the toString() method on the object in such cases. As Object has a toString() method on it this will be called if no custom version has been provided.

        Comment

        Working...