Some Intelligent Questions By my blog followers
1. String s1 = new String("Hello");
how does this thing work in memory. i.e. First the "Hello" object is created and then its reference is passed to the string s1 variable. But how can a constructor return something when it cannot return anything?
Ans: Following steps are executed for the above statement:
2. ***A new String Object is created by the keyword "new" and string object reference is assigned to s1
3. The value and hash of the argument string is assigned to the new string created in step 2
Please note: we should understand how "new" keyword works to understand how an object is created.
The below example would help us understand the work of new keyword:
class A {
// ...
new B();
// ...
}
Is this equivalent to
class A {
// ...
A.class.getClassLoader().loadClass("B's canonical name").newInstance();
// ...
}
The newInstance() method returns an object of loadClass type.
***So I our case it creates a String object and assigns it to s1.
2. String class is Final that's y Strings are immutable. This was the concept behind Immutability. Please can you explain y is String Buffer Thread safe and String Builder NOT.. ?
Ans: First of all I would like to highlight the concept of have Strings as "Immutable".
If you follow my string tutorials you will understand that JVM does not create String every time it gets a new request, but it looks for the same in the string pool. If it is available, then it returns the String object from the pool else let it proceeds in creating a new String.
Now, if you think -Strings were "mutable" then it would have been a catastrophe. Anyone can play with the String you created and you would be guessing how your String values are changing!!
Now I come to your question: Why StringBuffer is threadsafe while StringBuilder is not?
I hope you have understood that why there is the requirement of mutable sequence of character class. For this reason Java provides two versions of mutable sequence of character classes:
- StringBuffer- a thread safe, mutable sequence of character class, since JDK 1.0
- StringBuilder- mutable sequence of character class, since JDK 1.5
Now why StringBuffer is thread safe and why there was a new requirement for a non- thread safe version of StringBuffer – StringBuilder?
The reason for making StringBuffer thread safe is pretty straight forward, ie: to use a StringBuffer object by several threads without any problem. Its methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
So, we had our solutions for mutable class which is thread safe, but why again non-thread safe StringBuilder?
The reason for having a non-thread safe StringBuilder class is for replacement of StringBuffer class where ever string buffer was being used by single thread (as in most of the cases). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be much faster under most implementations.
3. Its always the reference which is set to variables, y java does not work on assigning values directly to variables.... y it always refer to variables whereas in other languages we can directly assign values to variables .. this will consume less space (i.e. Variables & reference Object spaces individually).. ?
Ans: In all high level languages there are always two memory sections blocked for every successful initialization of a variable –
a. Memory location where the value is stored
b. Memory location where the reference to the value is stored
so, if we have a statement like
int i= 5;
- A new integer object with value 5 is created and stored in a memory location
- A reference to the new object is created (i) and stored in another memory location and which points to the integer object having value= 5
We say in Java, that an integer Object i is created with value 5. But actually the above two steps (a,b) are executed in all high level programming languages.
In certain languages like C we can access the memory location of the reference variable but is JAVA this is not permitted.