Results 1 to 3 of 3
  1. #1
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Location
    Delhi
    Posts
    6
    Rep Power
    0

    Default why it is like this?

    i have a query though stupid one


    Java Code:
    int []n={10,20,30};
    System.out.println(n);
    output:{I@7b11a3ac

    and
    Java Code:
    char []ch={'j','a','v','a'};
    System.out.println(ch);
    Output: java


    why this difference in output? is there any specific reasons or just like that it happens.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by himanshu19 View Post
    i have a query though stupid one


    Java Code:
    int []n={10,20,30};
    System.out.println(n);
    output:{I@7b11a3ac

    and
    Java Code:
    char []ch={'j','a','v','a'};
    System.out.println(ch);
    Output: java


    why this difference in output? is there any specific reasons or just like that it happens.
    The System.out object is a PrintStream; when you read the API documentation you'll see that that class has a print(char[] c) method so it knows how to print a character array; it doesn't know how to print an int array so it uses its toString() method to at least be able to print something.

    Int arrays don't have a toString() method, they inherit one from the Object class which just prints the class name, and at sign and the hash code of the object.

    kind regards,

    Jos

  3. #3
    himanshu19's Avatar
    himanshu19 is offline Member
    Join Date
    Feb 2010
    Location
    Delhi
    Posts
    6
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •