Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized on System.out.toString()

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try synchronizing on a void method call and see what happens.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shanky !
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Malatesh Karabisti wrote:Thanks Shanky !


welcome
 
Tushar Chhibber
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct Pedro Kowalski! Keep going! Wish you all the best for your exam!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:

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


its related to a toString method.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic