Char array println output problem

Discussion in 'Java' started by qu0ll, Jan 29, 2007.

  1. qu0ll

    qu0ll Guest

    I am a little confused by the following program:

    public class CharArrayTest {
    public static void main(String[] args) {
    char[] foo = {'A', 'B', 'C'};
    System.out.println(foo);
    System.out.println("Output: " + foo);
    System.out.println("Output: " + foo.toString());
    }
    }

    The output is:

    ABC
    Output: [C@3e25a5
    Output: [C@3e25a5

    Why is the output of foo different when it is printed with or without a
    String literal before it? I am trying to achieve the output of "Output:
    ABC".

    --
    And loving it,

    qu0ll
    ______________________________________________

    (Replace the "SixFour" with numbers to email)
     
    qu0ll, Jan 29, 2007
    #1
    1. Advertising

  2. qu0ll

    Muggle Guest

    Simple.

    When you pass foo only you are invoking println(char[]) method of the
    PrintStream class (of which System.out is an instance) which prints
    array of characters and terminates the line. When you pass "Output: "
    + foo , you are invoking println(String ) method of PrintStream class
    which prints the string and terminates the line.

    Your desired result can be achieved by issuing the following :
    System.out.print("Output: "); System.out.println(foo);

    Thank you
    Muggle


    On Jan 29, 6:54 pm, "qu0ll" <> wrote:
    > I am a little confused by the following program:
    >
    > public class CharArrayTest {
    > public static void main(String[] args) {
    > char[] foo = {'A', 'B', 'C'};
    > System.out.println(foo);
    > System.out.println("Output: " + foo);
    > System.out.println("Output: " + foo.toString());
    > }
    >
    > }The output is:
    >
    > ABC
    > Output: [C@3e25a5
    > Output: [C@3e25a5
    >
    > Why is the output of foo different when it is printed with or without a
    > String literal before it? I am trying to achieve the output of "Output:
    > ABC".
    >
    > --
    > And loving it,
    >
    > qu0ll
    > ______________________________________________
    >
    > (Replace the "SixFour" with numbers to email)
     
    Muggle, Jan 30, 2007
    #2
    1. Advertising

  3. qu0ll

    qu0ll Guest

    "Muggle" <> wrote in message
    news:...
    > Simple.
    >
    > When you pass foo only you are invoking println(char[]) method of the
    > PrintStream class (of which System.out is an instance) which prints
    > array of characters and terminates the line. When you pass "Output: "
    > + foo , you are invoking println(String ) method of PrintStream class
    > which prints the string and terminates the line.
    >
    > Your desired result can be achieved by issuing the following :
    > System.out.print("Output: "); System.out.println(foo);
    >
    > Thank you
    > Muggle


    Thanks Muggle, that does the trick. I should have thought of that.

    --
    And loving it,

    qu0ll
    ______________________________________________

    (Replace the "SixFour" with numbers to email)
     
    qu0ll, Jan 30, 2007
    #3

  4. >
    > On Jan 29, 6:54 pm, "qu0ll" <> wrote:
    >
    > > I am a little confused by the following program:

    >
    > > public class CharArrayTest {
    > > public static void main(String[] args) {
    > > char[] foo = {'A', 'B', 'C'};
    > > System.out.println(foo);
    > > System.out.println("Output: " + foo);




    API exist, that takes the input char[] and which internally run an
    loop and calls the System.out.print version to print the char.

    > > System.out.println("Output: " + foo.toString());


    Here the case: foo is an array object...when u call a toString method
    on an Object, it will try to print the Object itself..Object format is
    like type of Object appended by the hex format of memory address.

    to cross check usethe System.identityHashCode on foo..this will return
    the same int of the same address and then convert the int value to hex
    decimal to display the same memory location
    > > }

    >
    > > }The output is:

    >
    > > ABC
    > > Output: [C@3e25a5
    > > Output: [C@3e25a5

    >
    > > Why is the output of foo different when it is printed with or without a
    > > String literal before it? I am trying to achieve the output of "Output:
    > > ABC".

    >
    > > --
    > > And loving it,

    >
    > > qu0ll
    > > ______________________________________________
    > >
    > > (Replace the "SixFour" with numbers to email)



    Hope this will help u understand the difference between the two
    printlns
     
    Proton Projects - Moin, Jan 30, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Stien
    Replies:
    0
    Views:
    359
    Thomas Stien
    Jan 5, 2005
  2. JGH
    Replies:
    2
    Views:
    6,983
    Malte
    Feb 23, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,058
    Ian Collins
    May 9, 2006
  4. Replies:
    3
    Views:
    737
  5. davidb
    Replies:
    0
    Views:
    764
    davidb
    Sep 1, 2006
Loading...

Share This Page