Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Char array println output problem

Reply
Thread Tools

Char array println output problem

 
 
qu0ll
Guest
Posts: n/a
 
      01-29-2007
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
______________________________________________
http://www.velocityreviews.com/forums/(E-Mail Removed)
(Replace the "SixFour" with numbers to email)


 
Reply With Quote
 
 
 
 
Muggle
Guest
Posts: n/a
 
      01-30-2007
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" <(E-Mail Removed)> 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
> ______________________________________________
> (E-Mail Removed)
> (Replace the "SixFour" with numbers to email)


 
Reply With Quote
 
 
 
 
qu0ll
Guest
Posts: n/a
 
      01-30-2007
"Muggle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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 Removed)
(Replace the "SixFour" with numbers to email)


 
Reply With Quote
 
Proton Projects - Moin
Guest
Posts: n/a
 
      01-30-2007

>
> On Jan 29, 6:54 pm, "qu0ll" <(E-Mail Removed)> 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
> > ______________________________________________
> > (E-Mail Removed)
> > (Replace the "SixFour" with numbers to email)



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
length of 2D Array >> char **myString= (char **) malloc (sizeof (char *)); davidb C++ 0 09-01-2006 03:22 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Problem- strcat with char and char indexed from char array aldonnelley@gmail.com C++ 3 04-20-2006 07:32 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
System.out.println vs out.println? JGH Java 2 02-23-2005 05:33 PM



Advertisments