Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
1
of 1 vote

This is a classic example of immutability of String objects. I think the first 3 lines are more efficient. Because if say one more String object is created in such a manner:
String z="deol";
Then the heap will not contain one more string object "deol". Since "deol" is already referenced by 'b', both 'b' and 'z' will refer to one String object on the heap.
Whereas if we use the new keyword:
String z=new String("deol");
Then one more object will be created on the heap and 'z' and 'b' will refer to two different objects that contain same data.

Check out the following URL for more information:
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

- badalrocks January 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks Badalrocks.

- Nimmo January 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why are we talking about Garbage collection when we know about string literal pool. This example is a mere test of immutability of strings and how string created on heap using string literal or new operator.

Closely looking at above example Creating String by using String Literal or New approach looks the same. In both the cases String object gets created on the heap. We need to check out for 'a+b', from my experience and theory one more string object gets created and that will be printed on to IO by system.out statement.

So no difference except the fact that.. if String literal lookup approach in first case is costlier if lookup doesn't find any existing string. In other case creating string objects using new operator directly creates the object on the heap without even bother to perform string lookup. So time wise second is much faster than first one for given example.

'badalrocks' comments are fine for third string with same string literal. Question is which one is more efficient for given example. ( Efficiency = Both Time and Memory ) . So for me second is more efficient.

- MasterSolution June 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

guys, someone answer specifically with proper reasons saying which one is more efficient and why?

- smartAss July 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

both are same !

- Anonymous August 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In 1st case... it will lookup in the heap for the string objects, it they exist variables will point to them... if they do not exist then they will be created....so if they do not pre-exist then I guess the creation time will be same as using "new String"... however in first case we had some lookup time....however if they pre-existed then it would be faster in first case. so I guess MasterSolution is right.

- Anonymous KS November 27, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think first way will be more efficient. As compiler will optimize sysout as follows

System.out.println("abhaydeol"); // It will also add a+b to the string pool.

comments?

- jk January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think first way will be more efficient. As compiler will optimize sysout as follows

System.out.println("abhaydeol"); // It will also add a+b to the string pool.

comments?

- jk January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no, + operator does the same thing as concat() does. so here, it will be, "abhay".concat("deol") which will create a new object.

- tetura February 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The explicit String constructor has the same result as using double quotes to implicitly create a string reference, in both cases the given string is assigned to the reference variable. However, passing a double quoted string argument to the String constructor actually makes two strings; the argument string is implicitly created inline and then its contents are copied to the new String instance. For this reason this constructor is largely redundant and not recommended for general purposes. The String constructor with String argument is rarely used except to create an independent copy of an existing string variable.

- MostUnlucky February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

String a = new String("All the best for your job search");//will creates two string instances
String a = "All the best for your job search";//will create one string instance

- MostUnlucky February 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Adding to what mostUnlucky said..
String a =new string("tmp"); 2 instance..one in heap,one in string pool.
String b=new String("tmp");.. only 1 instance..one in heap,able to find string literal "tmp" in String pool thats why wont create one more instance.



String a="tmp" one instance..only in heap.
String b="tmp" one instance..

- sourabh March 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

String a=new String("abhay");
String b=new String ("deol");
System.out.println(a+b);

in the above example, these two strings can be garbage collected.
but String a="abhay", String b="deol" cannot.

- question April 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

are you in your senses ?? what are you writing ?

- Anonymous May 22, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The string literals used in the latter case will sit in a string pool, but are you sure they can never be garbage collected?

- Anonymous May 25, 2009 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More