Java Programming

I would like to know how the toString is being called from the constructor in the following code.Any feedback would help me to improve my understanding of java concepts.

***************************************************************************************

// CharSequenceDemo presents a String value -- backwards.

public class CharSequenceDemo implements CharSequence {

private String s;

public CharSequenceDemo(String s) {

//It would be much more efficient to just reverse the string

//in the constructor. But a lot less fun!

this.s = s;

}

//If the string is backwards, the end is the beginning!

private int fromEnd(int i) {

return s.length() - 1 - i;

}

public char charAt(int i) {

if ((i < 0) || (i >= s.length())) {

throw new StringIndexOutOfBoundsException(i);

}

return s.charAt(fromEnd(i));

}

public int length() {

return s.length();

}

public CharSequence subSequence(int start, int end) {

if (start < 0) {

throw new StringIndexOutOfBoundsException(start);

}

if (end > s.length()) {

throw new StringIndexOutOfBoundsException(end);

}

if (start > end) {

throw new StringIndexOutOfBoundsException(start - end);

}

StringBuilder sub =

new StringBuilder(s.subSequence(fromEnd(end), fromEnd(start)));

return sub.reverse();

}

public String toString() {

StringBuilder s = new StringBuilder(this.s);

return s.reverse().toString();

}

//Random int from 0 to max.

private static int random(int max) {

return (int) Math.round(Math.random() * max + 0.5);

}

public static void main(String[] args) {

CharSequenceDemo s =

new CharSequenceDemo("Write a class that implements the CharSequence interface found in the java.lang package.");

//exercise charAt() and length()

for (int i = 0; i < s.length(); i++) {

System.out.println(s.charAt(i));

}

//exercise subSequence() and length();

int start = random(s.length() - 1);

int end = random(s.length() - 1 - start) + start;

System.out.println(s.subSequence(start, end));

//exercise toString();

System.out.println(s);

}

}

***************************************************************************************

[2434 byte] By [shreedaddya] at [2007-11-26 13:29:46]
# 1
What? I don't see you calling toString() method from your constructor.
lupansanseia at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 2
Called from constructor...? No comprendo, senor...Elaborate...As for errors, <never mind>you're missing a few closing brackets, and</never mind> in toString, the StringBuilder can't have a identifier same as an existing variable.Message was edited by:
atoxica at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 3
toString is usually called implicitly. If you pass an object to PrintStream.println or other such methods, the method will call toString on it and then print the result. You almost never call toString directly, yourself, in practice.
paulcwa at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 4
To add on, toString() will print out the "memory reference" of the object if it is pass into PrintStream.println or other such methods. So if you want to print the content of the object, u have to overwrite the toString() method.
khookha at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 5
If you run the program you will find that the string is being reversed right after the call to constructor though there is no methods such as println.This code was picked from the java tutorial for Java 1.5. Yet I appreciate you taking time to post the reply.RegardsSree
shreedaddya at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 6
If you run the program you will find that the string is being reversed right after the call to constructor though there is no methods such as println.This code was picked from the java tutorial for Java 1.5. Yet I appreciate you taking time to post the reply.RegardsSree
shreedaddya at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 7
If you run the program you will find that the string is being reversed right after the call to constructor though there is no methods such as println.This code was picked from the java tutorial for Java 1.5. I appreciate you taking time to post the reply.RegardsSree
shreedaddya at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 8
> System.out.println(s);This line ends up invoking s.toString(), because that's what the println(Object) method does on its argument in its implementation.
warnerjaa at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 9

> > System.out.println(s);

> This line ends up invoking s.toString(), because

> that's what the println(Object) method does on its

> argument in its implementation.

But toString method is being invoked at the time when CharSequenceDemo is being instantiated and that is not making sense.System.out.println(s) is the last statement in the main method.That is the reason I am confused.Thank you very much for the time and reply.

Regards

Sree

shreedaddya at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 10

> But toString method is being invoked at the time when

> CharSequenceDemo is being instantiated

No it isn't. Temporarily put this in your toString() implementation as the first line:

System.out.println("toString() is being invoked...");

You'll see that it doesn't print that (and hence the method isn't invoked) until you do the:

System.out.println(s);

statement.

And put this:

System.out.println("Invoking System.out.println(s) now...");

right before the above System.out.println(s) line too, to help you see what is happening.

warnerjaa at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 11
That helped.It is quite a relief.Thank you so much.:)RegardsSree
shreedaddya at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...
# 12
Never mind...
glevnera at 2007-7-7 20:34:37 > top of Java-index,Java Essentials,New To Java Technology Archive...