aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString()?" Watch "toString()?" New topic
Author

toString()?

jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
Dear Helper:
1. For the following code application:
class Z{
public static void main(){
System.out.println("AAA" + new Z());
}
public static String toString(){
System.out.println("###");
return "Z";
}
}
The output is "###AAAZ".
I don't understand how the toString() is called here and how the program works. Can anybody tell me that?
2. What is "&sim". Is it as same as "~"?
3. Must the Imput/Output methods, like XInputStream, be used in try/catch block, otherwise causes compile error?
thanks.

wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39

notice the "static" key word, you are trying to override the
instance method "public String toString()" of Object class , but change it into a class method. The compiler should not allow you to do that and you will get a compiling error.
Wei
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Jordan,
Couple of things to be noted here :

  1. new operator is executed before the + operator.Look at the precedence chart in any of the Certification Guides.
  2. When you call System.out.println(Object), implicitly Object.toString() is called.

  3. As Wei mentioned in his post, the toString() method incorrectly coded.You cannot override a instance method by static method.
    The correct version of the code should be :

    Going by the steps I listed, new Z() is evaluated first as new Z().toString(), which would print ### and return Z.
    So the equation reduces to "AAA" + "Z", with "###" already printed.Hence, the output is "###AAAZ".
    Hope this helps,
    Sandeep
    SCJP2,OCSD(JDeveloper),OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Ans2
&sim i'm 99% sure is not an operator . This was posted sometime back & most of my friends & a few guy's at the ranch were of the opinion that they'd never come across this . I hope that the person who brought it here clarifies this with the folks who had it at their site .

I think that it's some sorta esquape sequence that creeped onto the web-site , Jus a hypothesis though . Anybody share this opinion !
Ji Wang
Greenhorn

Joined: Feb 05, 2001
Posts: 13
Some answers:
1.Donotforget the right main should be

public staticvoid main(String[] args)
2. &sim should be ~. I think it is a type error or because the terminalcannot display it probly.
3. All the XStreams throw IOException. This is why they should be used with the try/catch block.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Jordan,

2. What is "&sim". Is it as same as "~"?

Where did you come across this?I have never seen it being used so far!Under what context this so-called escape sequence (or whatever!) is being used?
-- Sandeep
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
When i said escape sequence i meant it for HTML . Don't get confused , i don't think this thing exists in Java .
Is the person who brought this here listenin ? Clarify with the people who had it & plz let us know !
Peace
Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
When I run this code, I am getting runtime error
Exception in thread "main" java.lang.NoSuchMethodError: main
Please explain


Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Bob,
You have to change <code>public static void main()</code> to <code>public static void main(String[] args)</code>. This is the correct signature if you want Java to start a program using main().
Now, for the &ism and ~ confusion. A while back someone posted a question saying they'd heard of an '&ism' operator that inverted bits. I've never heard or seen anything about '&ism' and the only inversion operator, to my knowledge, is ~. Whatever '&ism' is, I wouldn't worry about it; has nothing to do with studying for the certification.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
jordan gong
Ranch Hand

Joined: Jun 12, 2001
Posts: 37
Sorry guys: I mis-typed the main() function.
The "&sim" came from one of RHE's sample exam. The answer explains the function of this is as same as "~". Maybe my machine can not show the " ~ ", so show the "&sim" instead.
But I doubt about it, we are talking about JAVA...
Mr.Sandeep: thanks for your answer. I am amazed by the code
like this. Do we program like this in reality?
Thanks.
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Jordan,
As I manage to get more and more experience on Java, I have started believing that SCJP2 is more about how much agile and smart you are in finding bugs in the code.In real projects, it is more about analysing and designing OO systems.Nobody would write a code with static modifier for toString().Perhaps the IDE you use will not allow you to do so!
-- Sandeep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString()?