• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding Arrays  RSS feed

 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code Listing 1:


Code Listing 2:



I have two questions:
1. The code listing 1 gives the output - J A V A
What is the reasoning behind this ?

2. On changing the name of toString() to something else - the output is a quirky looking string ts@17590db - what exactly is happening here?

Thanks in advance!
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the both examples, the call to System.out.println() calls the specified object's toString() method. In the first case, you've overriddent the toString() method to print "J A V A", so that's what you get. In the second case, you're printing an Array object; the "funky" characters you see are the default string representation of an Array.
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve!

Do the calls for println() and print() while printing out an object always default to toString() ? Where can I find the official document for this funda?

And also is it possible to observe this using "javap":
I tried
javap java.io.PrintStream
and
javap java.lang.System

How to infer from the output of the above two - regarding the default use of toString() method?
[ September 14, 2005: Message edited by: shank ram ]
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shank,

System.out.print() and System.out.println() always print Strings no matter what the argument passed to them.

If the argument passed to them is not a String, they attempt to convert it to a String.
And to convert an object to its String representation, the object's toString() method is always called. So if you wish to change what is printed with the print methods, just over-ride the toString() method.

toString doc
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shank ram:
Thanks Steve!

Do the calls for println() and print() while printing out an object always default to toString() ? Where can I find the official document for this funda?

And also is it possible to observe this using "javap":
I tried
javap java.io.PrintStream
and
javap java.lang.System

How to infer from the output of the above two - regarding the default use of toString() method?

[ September 14, 2005: Message edited by: shank ram ]


It's probably easier to see it by looking at the source. There should be a src.zip in your JDK installation directory that contains the source code for the Java libraries.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadoc for PrintStream documents this. Actually it sas that print(Object) uses String.valueOf(Object). The javadoc for String.valueOf(Object) says that it uses Object.toString(). See here and here.

OK, now will you answer a question for me? What does "funda" mean? I gather it means something like "fact," but it seems to be unique to Indian English.
 
shank ram
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all !

Ernest,
"Funda" is the shortened form "Fundamental Logic"

Now, I know what is coming next ... a rap ... asking me to use language to suit an international audience . But, dont you worry "funda" will soon worm its way into to the OED ...
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "funda" mean? I gather it means something like "fact," but it seems to be unique to Indian English.
The only *other* place I've heard it used is an off-color pun about offensive bodily functions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!