aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the result and why?please help me! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the result and why?please help me!" Watch "what is the result and why?please help me!" New topic
Author

what is the result and why?please help me!

Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
1. class Z {
2. public static void main(String[] args) {
3. System.out.println("AAA" + new Z());
4. }
5.
6. public String toString() {
7. System.out.println("###");
8. return "Z";
9. }
10. }


help you means help me
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
print will be
###
AAAZ
Reason is same as for recursive calls.
Try to find out how factorial's method with recursion works
AW method calls are put on a stack (means LIFO)
so 1st call- S.o.p
2nd call - toString() - last call
now as toString is Last In so First Out.
1st toString() is executed.
2nd S.o.p is executed.
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
In line 3 a string needs to be constructed before it can be output. Within the call to System.out.println(...) a call is made to create a new object from class Z. Now, when we try to display this object, a call to it's toString() method is made. So line 7 get executed first, then the string "Z" is returned and appended to the string "AAA" giving you the outputs
###
AAAZ
The thing to understand is how toString() works!
NOTE: all classes implicitly are subclasses of class Object which has a method called toString(). The default just returns a string name of the class followed by '@' and a hashcode value.
Try the code below to see this.

public class Test {
static public void main(String[] arg) {
MyClass m = new MyClass();
System.out.println(m);
}
}
class MyClass {
}

Now we can override the toString() method to return our own string.
Try this code to see this
public class Test {
static public void main(String[] arg) {
MyClass m = new MyClass();
System.out.println(m);
}
}
class MyClass {
public String toString() {
return "VIP_Class";
}
}
Now go back to your original question and hopefully you can see why the output is the ways it is
[ January 22, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
Originally posted by ravish kumar:
print will be
###
AAAZ
Reason is same as for recursive calls.
Try to find out how factorial's method with recursion works
AW method calls are put on a stack (means LIFO)
so 1st call- S.o.p
2nd call - toString() - last call
now as toString is Last In so First Out.
1st toString() is executed.
2nd S.o.p is executed.
HTH


According what you said, it should give the result:
###ZAAA
is there any error?
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Seany Iris:

According what you said, it should give the result:
###ZAAA
is there any error?

there is no error :
see the toString()
there is S.o.println. so the after printing ### cursor will be next line and then
S.o.p of main() will be executed which will print AAAZ.
I think u are confused with the position of 'Z'
when you call toString() it will be executed normally like any other method and return the value which is Z at the place where it was called.
It was called after AAA + returnedValueFromtoString
=> AAA + Z
=> AAAZ
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I see,thank you very much!
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
if the toString method is not overridden then the method is called as from class Object.
but when it is called from class Object the output is
AAAZ@hascode
(where Z is name of class)
they are all printed on one line and seems logical too.
But when the overidding method toString defined is called the result is that the AAA is printed later ie after toString method is called and its return value put in.
why does the order vary. what is the difference un calling an inherited method vs overidding method. I am asking about the order of result printed.
Originally posted by Rajinder Yadav:
In line 3 a string needs to be constructed before it can be output. Within the call to System.out.println(...) a call is made to create a new object from class Z. Now, when we try to display this object, a call to it's toString() method is made. So line 7 get executed first, then the string "Z" is returned and appended to the string "AAA" giving you the outputs
###
AAAZ
The thing to understand is how toString() works!
NOTE: all classes implicitly are subclasses of class Object which has a method called toString(). The default just returns a string name of the class followed by '@' and a hashcode value.
Try the code below to see this.

public class Test {
static public void main(String[] arg) {
MyClass m = new MyClass();
System.out.println(m);
}
}
class MyClass {
}

Now we can override the toString() method to return our own string.
Try this code to see this
public class Test {
static public void main(String[] arg) {
MyClass m = new MyClass();
System.out.println(m);
}
}
class MyClass {
public String toString() {
return "VIP_Class";
}
}
Now go back to your original question and hopefully you can see why the output is the ways it is
[ January 22, 2002: Message edited by: Rajinder Yadav ]

[ January 23, 2002: Message edited by: mark stone ]
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
please comment n this
Originally posted by mark stone:
if the toString method is not overridden then the method is called as from class Object.
but when it is called from class Object the output is
AAAZ@hascode
(where Z is name of class)
they are all printed on one line and seems logical too.
But when the overidding method toString defined is called the result is that the AAA is printed later ie after toString method is called and its return value put in.
why does the order vary. what is the difference un calling an inherited method vs overidding method. I am asking about the order of result printed.

[ January 23, 2002: Message edited by: mark stone ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi mark,
the problem here is the S.o.pln stmt in the overriddent toString() method.
first toString() is called on the object as per the discussion goes so far. this is the case in any scenario.
but here we have a speciality of having that println() stmt in toString(). now, as toString() is called first, it will print newline for sure after printing "###" right?
so now the pointer is on the new line and then toString() returns with whatever value we have as return. and then it prints the contatenated final value which is "AAAZ"...
if u remove println() from the toString() it work as u argued here.
regards
maulin.
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
mauline
why ? first in any case AAA will be printed.
then there is + ie concatenation sign and then toString method would be called.
let's forget about the S.o.p statement for this moment. i need to know the order in which things get called and executed.
here is the statement
System.out.println("AAA"+new "Z");
tell me how things are executed. in all cases AAA will be executed or rather printed ? but in the case of overidden method that has another S.o.p statement this is not happening. it seems that that S.o.p is being run. I think the overidden method should be called after AAA has been printed. right ? i am stuck here.
Originally posted by Maulin, Vasavada:
hi mark,
the problem here is the S.o.pln stmt in the overriddent toString() method.
first toString() is called on the object as per the discussion goes so far. this is the case in any scenario.
but here we have a speciality of having that println() stmt in toString(). now, as toString() is called first, it will print newline for sure after printing "###" right?
so now the pointer is on the new line and then toString() returns with whatever value we have as return. and then it prints the contatenated final value which is "AAAZ"...
if u remove println() from the toString() it work as u argued here.
regards
maulin.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Before S.o.p prints anything, its argument must be evaluated. Take a look at the JavDdoc for a PrintStream object (System.out is a PrintStream).
Notice that all of the print() and println() methods only take a single arugment; either a primitive, a char array, a String, or an Object.
System.out.println("AAA" + new Z());
The argument is an expression, so the expression must be evaluated first. The left-hand side is a string, and the operator is a '+', so this operation will be a String concatenation. String concatenation operations are performed using a StringBuffer object. The arguments to the println get replace by
new StringBuffer().append("AAA").append(new Z());
Now, before the last append can be completed, the expression "new Z()" must be evaluated to find its value.
This expression is called an instance creation expresssion, and its value is the reference value for the newly created Z object. Since Z has a default constructor, nothing special happens during the new object's instantiation.
so now we have a StringBuffer object with "AAA" inside it, and we are appending an object reference. Look at the javadoc for StringBuffer.append(Object obj); You will see it uses the static String method valueOf() to retrieve the value of the object. valueOf() call toString() on the object if it is not null. Since we just created this object we know it's not null, so to append it to the string buffer we must call the toString() method of Z.
NOW when StringBuffer's append method makes the toString() call in the Z object, there is a println() statement there. So, it executes, and prints the text "###". Then the string value for the toString() method returns a value to the caller.
This value is "Z". So that Z gets appended to the existing StringBuffer (the one with "AAA" already in it.) So now the StringBuffer has "AAAZ" in it.
Finally, the argument in the original s.o.p has been fully evaluated, and we have
System.out.println(theStringBuffer);

(theStringBuffer is the StringBuffer automatically created for us by the compiler, and contains "AAAZ"). Look again at the javadocs for PrintWriter, and see how it handles a method call to println() with an Object argument. It uses the static valueOf() method from string, which will invoke the toString() method of theStringBuffer, causing "AAAZ" to be printed.
<whew>
This is exactly how this gets printed.
Rob
[ January 23, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the result and why?please help me!