Hi, Will the following code create 100 instances of String object or will just create one.
for(int i = 0; i < 100; i++){ String name = request.getParameter("name"); } In other words is this a better way: String name = ""; for(int i = 0; i < 100; i++){ name = request.getParameter("name"); }
Cheers, Martin
Ernest Friedman-Hill
,
author and iconoclast
staff
Neither will create any instances of String. The literals ("" and "name") will be pulled from the internal String pool, and getParameter() will return a reference to the same object each time it is called.
All Java variables which refer to Objects are references to objects, not the objects themselves. Assigning a reference to a variable has no effect on any object, nor does it create any new objects.
Finally, note that (except for the assignment of a "" to the name variable in the second example) your Java compiler will generate identical bytecode for both of these examples. Entering or exiting a scope doesn't create or destroy anything; scopes are meaningful in Java, but when you look at the bytecode, you see that they are just a notational convenience.
Head over and read the JavaRanch Cup Size and Pass by Value, Please campfire stories, and I guarantee you'll understand this perfectly afterwards.
Ernest, thanks for pointing me to the resources. But this brings another question in my mind. If for example I have a class A that invokes some methods in class B.
class A{ public boolean insertDataInTable(String s){ B b = new B(); boolean result = b.insertData(s); return result; } class B{ public boolean insertData(String s){ String sql = "insert in table"; //call jdbc to insert return result; } }
Does method insertData() in class B have to be synchronized??? Because everytime its going to be invoked from class A it is from a "new B()" instance. So the inserts will be exclusive. But from what I understood in terms of the article each time you say new you will get a reference to Sony32 object ( or object of type B). So you will need to synchronize the insertData method.
Soapbox alert. I just wanted to add that initialising the "name" variable inIs bad style. Your intent is for name not to be used until it has been assigned to in the for loop; by initialising it, you obscure that intent. It would be better to sayThe added benefit is that the compiler will tell you if your code can potentially access the variable without it having been assigned to. In other words, this is not just clearer, it might help you catch bugs too.
- Peter [ August 06, 2004: Message edited by: Peter den Haan ]
Originally posted by Martin Lira: Does method insertData() in class B have to be synchronized???
No, for two reasons:
You are creating a new instance of B every time, so B is only ever executed by one thread.
But even if B were executed by more than one thread, it is stateless, i.e., it has no instance or class variables. Stateless objects are always threadsafe. Actually this is a special case of the fact that immutable objects are always threadsafe.
Originally posted by Rovas Kram: Are you saying that int the bytecode the variable name is declared only once?
That's what Ernest is saying. Go ahead, have a look yourself - bytecode isn't that hard to read and every JDK has the javap disassembler. Upon entry into a method, the JVM allocates space for all local variables in the method regardless of their scope.
Does method insertData() in class B have to be synchronized???
Martin,
We have no idea what the original scope of your String object is. This is the data that must be thread safe so I have to say from your example that whether or not it's thread-safe is undefined.
insertData(String) only needs to be sychronized if the input string is a refernce to a String object that can be accessed by multiple threads at the same time. I have no way of knowing if this unless I see the whole program.
The technical term for caching string literals is "interning". Class String has an intern() method. (It's a native method, which means that the various JVMs have their own ways of caching strings.) If you're interested in the topic, you might enjoy reading: http://www.javaworld.com/javaqa/2003-12/01-qa-1212-intern_p.html
In my case class A = Servlet and the doPost method invokes the class B = Java Bean method for inserting data into a table. Hope this will give a big picture of the program.
Originally posted by Rovas Kram: insertData(String) only needs to be sychronized if the input string is a refernce to a String object that can be accessed by multiple threads at the same time. I have no way of knowing if this unless I see the whole program.
It doesn't matter if the String can be accessed by multiple threads at the same time. String objects are immutable so there's no need to synchronize access to them.
Originally posted by Martin Lira: Will it still be an immutable object if I have static members
class B{ private static Hashtable hash; static{ hash = new Hashtable(); hash.put("one",1); hash.put("two",2); } public boolean insertData(String s){ //same code of inserting } }
It doesn't matter if your class B is immutable or not as long as insertData() only deals with the String s that is passed to it as a parameter and whatever else it creates locally (inside the method), then it is thread safe and does not need to be synchronized.
BTW, if that's all the code in class B, then it's still immutable since there is no way for to change the Hashtable once the class has been loaded (assuming insertData() doesn't touch the Hashtable). [ August 06, 2004: Message edited by: Junilu Lacar ]
Post by:autobot
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards