Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString()?

 
jordan gong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run this code, I am getting runtime error
Exception in thread "main" java.lang.NoSuchMethodError: main
Please explain
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jordan gong
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic