Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Char array println output problem

Thread view: 
qu0ll - 29 Jan 2007 23:54 GMT
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".

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

Muggle - 30 Jan 2007 03:23 GMT
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

> I am a little confused by the following program:
>
[quoted text clipped - 23 lines]
> qu0llSixF...@gmail.com
> (Replace the "SixFour" with numbers to email)
qu0ll - 30 Jan 2007 03:30 GMT
> Simple.
>
[quoted text clipped - 9 lines]
> Thank you
> Muggle

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

Signature

And loving it,

qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)

Proton Projects - Moin - 30 Jan 2007 04:11 GMT
> > I am a little confused by the following program:
>
[quoted text clipped - 3 lines]
> >   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
> >  }
>
[quoted text clipped - 15 lines]
> > qu0llSixF...@gmail.com
> > (Replace the "SixFour" with numbers to email)

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.