Hi Bryan,
In the first case
System.out.print("New name: " + generateNewName(args));
the method print() from the PrintWriter class which accepts a String
argument is called. Since you are concatenating your character array which
is returned by generateNewName(args) with a String "New name: ", the
toString() method is called on the character array to convert it to a String
as well so that concatenation can occur. But calling toString() on the array
causes the memory reference to be printed.
In the second case
System.out.print(generateNewName(args));
the method print() from the PrintWriter class which accepts a character
array as an argument is called. So it prints correctly.
To make your first case print the output correctly instead of the memory
reference, use the valueOf method from the String class. Your code should be
System.out.print("New name: " + String.valueOf(generateNewName(args)));
Hope it makes sense. Kindly refer the JavaDoc too.
Thanks & Regards,
Babu Rajendran
On Sat, Jun 27, 2009 at 1:11 AM, Bryan <[email protected]> wrote:
>
> I am attempting to get my generate new name project to output the name
> it creates. My generateNewName method outputs the new name as a char
> array.
> Behold two lines of code-
> System.out.print("New name: " + generateNewName(args));
> System.out.print(generateNewName(args));
>
> The former outputs "New name: [...@3e25a5"
> The latter outputs the actual name (in this case, lola).
>
> Why do these (which both appear to call the same method with the same
> parameters) output different results? I assume that the former is a
> memory reference. How do I make it work (better)?
>
> Thanks!
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---