Char array println output problem

Q

qu0ll

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
______________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email)
 
M

Muggle

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
 
Q

qu0ll

Muggle said:
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
______________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email)
 
P

Proton Projects - Moin

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.

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".
qu0ll
______________________________________________
(e-mail address removed)
(Replace the "SixFour" with numbers to email)


Hope this will help u understand the difference between the two
printlns
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top