GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized on System.out.toString() 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 "synchronized on System.out.toString()" Watch "synchronized on System.out.toString()" New topic
Author

synchronized on System.out.toString()

Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

public class ThredsLetters extends Thread {

public static void main(String[] args) {
synchronized(System.out.toString()) {
System.out.println("Working Man");
}
}
}

output : Working Man

This Works fine, but what is synchronized here the thing we got from toString() method ?
can any body explain ?
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

try synchronizing on a void method call and see what happens.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Malatesh Karabisti wrote:
This Works fine, but what is synchronized here the thing we got from toString() method ?


Don't you check the API? Please UseCodeTags when you post codes. There is one thread, so no need of synchronized here!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


System is a class
out is a object and
toString will convert the out object into string which is again a object.
synchronized context will occupy a lock on the string object


SCJP6.0,My blog Ranchers from Delhi
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

Thanks Shanky !
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Malatesh Karabisti wrote:Thanks Shanky !


welcome
Tushar Chhibber
Greenhorn

Joined: Aug 30, 2010
Posts: 6
toString() returns a String i believe... and System is a class, out is a reference of print stream class in class System , now if.. If System.out.toString() works fine...and returns a String... which in this case is in Synchronize(),which here is asking for an Object whose lock it wants to aquire to run the code...So there is no Object is synchronize.. its a String.. even if it is a single thread and we are not calling any wait() method in Synchronize(){} .. how can this code run?? can anyone explain?


SCJP 6.0(OCPJP) <96%> , going for SCWCD 5.0 :-)
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Tushar Chhibber wrote:toString() returns a String i believe... and System is a class, out is a reference of print stream class in class System , now if.. If System.out.toString() works fine...and returns a String... which in this case is in Synchronize(),which here is asking for an Object whose lock it wants to aquire to run the code...So there is no Object is synchronize.. its a String.. even if it is a single thread and we are not calling any wait() method in Synchronize(){} .. how can this code run?? can anyone explain?


I think that is in fact something like


So you are synchronizing the code for this String object. The String is an Object, so everything is fine as far as I can see.

I think that if you would like to call wait / notify on the object (String) you are synchronizing on you could do that, like

which should be in fact something like


If I'm wrong somebody please rectify me.


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

@Pedro Kowalski ....correct
wait,notify and notifyALL are the methods of the object class.
So before calling them you must have to occupy a lock on the object.
here the object is String.

but what i think in your above explaination

should be equal to

hexadecimal representation comes when we donot convert the object to string
@4a5ab2:-element this

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

@Shanky Sohar

I'm a bit confused right now about this hexadecimal part of toString() - could you please explain it?

You are saying that not every class (which isn't overriding the toString() method) will return the string of ClassName@HashInHex format but just the ClassName?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Pedro Kowalski wrote:@Shanky Sohar

I'm a bit confused right now about this hexadecimal part of toString() - could you please explain it?

You are saying that not every class (which isn't overriding the toString() method) will return the string of ClassName@HashInHex format but just the ClassName?


Yes Lets see the below code.dont bother about the name of the class.





Output will be "Tested@17854"

but if you change the line from
System.out.println(ab);
to
System.out.println(ab.toString());


output should be
Tested

but i am getting Tested@17854

but as per the API.
toString().wil convert the object to a String form.So. i think there should not be any hexadecimal representation in String Form

Now i also got confused.lets see others reply now
Divyeshh Patel
Ranch Hand

Joined: Aug 03, 2010
Posts: 37

The above statement in your program would produce the output as it is producing now. API would not know how do you want to represent your class as a String, so the toString() method provides the default mechanism of printing the classname appended by the memory address where the object is residing in memory(might even be additional maths on the address, don't know).
If you want to customise the output of System.out.println(ab); , you need to override the toString() method of Object class.
If you define the methods like the below in your class

then you will get the expected output and the statements System.out.println(ab); or System.out.println(ab.toString()); in your program would produce the desired output.


Cheers,
Divyesh.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shamky Sohar and Pedro Kowalski, don't you guys check the API?

The toString() method is inherited from Object class in java, if you don't override the method in your own class, then the inherited method from Object class will be invoked. And check the API here for rest. And when you override the method, use it get a meaning full description for the instance of that class.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

@Abimaran Kugathasan

Yes, I did and I understand what is the purpose of toString() method.

Therefore - in my opinion, there is no difference between


and


as in the first example, the toString() is called implicitly, when in the second example, the toString() is called explicitely.

Hovewer, I am still learning for the SCJP and don't know a whole bunch of tricks in Java. That's why I asked if there is a possibility that a class which doesn't override the toString method can print out only the classname part (without the hexadecimal representation of the hashCode after the @ sign).

Cheers!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Correct Pedro Kowalski! Keep going! Wish you all the best for your exam!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:Shamky Sohar and Pedro Kowalski, don't you guys check the API

Checked now.I have little confusion over there...

And it is shanky not shamky

By the way Thanks..Abhimaram
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:
Abimaran Kugathasan wrote:Shamky Sohar and Pedro Kowalski,

Checked now.I have little confusion over there...

And it is shanky not shamky

By the way Thanks..Abhimaram

Who is this? We made same mistake there!

I couldn't understand your confusion Shanky! Could you elaborate?
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:

I couldn't understand your confusion Shanky! Could you elaborate?


its related to a toString method.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Shanky Sohar wrote:
its related to a toString method.

Could you please tell, where you don't understand? I couldn't figure out that.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

The confusion was related to the working of toString() method but not it is preety clear..

So i am happy with this discussion..

Great work..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized on System.out.toString()