String Tricky Question (SCJP Forum at JavaRanch)
String Tricky Question (SCJP Forum at JavaRanch)
Java FAQ
Recent Topics
Register / Login
This week's giveaway is in the Jobs Discussion forum. We're giving away four copies of Presenting for Geeks and have Dirk Haun on-line! See this thread for details.
Author
hello friends. just go through following code What is the output (Assuming written inside main) String s1 = new String("amit"); System.out.println(s1.replace('m','r')); System.out.println(s1); String s3="arit"; String s4="arit"; String s2 = s1.replace('m','r'); System.out.println(s2==s3); System.out.println(s3==s4); a) arit amit false true b) arit arit false true c) amit amit false true d) arit amit
www.coderanch.com/t/195396/java-programmer-SCJP/certification/String-tricky
1/5
7/16/13
true true Correct answer is a) why? 2) ) Answer based on following code 1 Choice c1 = new Choice(); 2 c1.add("First"); 3 c1.addItem("Second"); 4 c1.add("Third"); 5 c1.insert("Lastadded",1000); 6 System.out.println(c1.getItemCount()); a) Compile time error b) Run time error at line 5 c) No error and line 6 will print 1000 d) No error and line 6 will print 4 Correct ANswer is d) why? what is the meaning of c1.insert("Lastadded",1000); Mansi [This message has been edited by Mansi Shah (edited November 17, 2000).] [This message has been edited by Mansi Shah (edited November 17, 2000).]
String is Immutable, so a method like replace would only return a new String but no change the string. So the first two output are obvious as being "arit" and "amit". As the replace method returned a new string that was written to System.out , yet the original String stayed the same as being immutable. For the last two, you know that Java optimizes String variable memory allocation so that there is only one String object when its based on the same characters, except when it is created by the new operator or returned by a method such as replace. So String S1 = "amit"; String S2 = "amit"; Would be actually the same object "amit" being pointed to by two reference variables. However String S1 = "amit"; String S2 = new String("amit"); are two different objects. Hence the result you get is as shown in answer a. zulfiqar raza PS: I would have assumed it is a, if the method replace() actually returned a new String. But since the answer you changed to is different, replace does not return a new String like the contructor new String("amit") would have. This makes more sense. Read up on replace method and see what is does.. so the answer would be d? [This message has been edited by zulfiqar raza (edited November 17, 2000).]
Mansi , There are lots of similar interesting question on Strings which have been discussed in details on this site. Try doing a search and check the results.
www.coderanch.com/t/195396/java-programmer-SCJP/certification/String-tricky
2/5
7/16/13
Hemal Mehta Ranch Hand Joined: Nov 16, 2000 Posts: 101
It's cool! In the first question when you print: //First print System.out.println(s1.replace('m','r')); It's obvious amit 'm ' is replaced by r and becomes arit and a a new string is created //Second print System.out.println(s1); The original string s1 is same, whihc is amit. //Third case System.out.println(s2==s3) s2 is pointing to the original value of s1,whihc is amit even though the new string arit is created but s1 value cannot be chnaged it will always be amit. Therefore as s3 is arit and s2 amit it's false //Fourth case s3==s4 Here both point to same string in the string pool ie arit arit is created only once when you use = operator. I hope that clears it...
Please guide me in following example... first returns false where as second returns true... public class exstr{ public static void main (String args[]) { String s0 = "amit"; String s1 = "amita"; String s2 = (s0.concat("a")); String s3 = (s1.toLowerCase()); System.out.println("s1==s2 : "+(s1==s2)); System.out.println("s1==s3 : "+(s1==s3)); } }
Suresh Selvaraj Ranch Hand Joined: Nov 14, 2000 Posts: 104
Hi, The "==" on String objects checks if the memory address of the String objects being compared are same or not. case1: The concat operation on String object s0 i.e.
www.coderanch.com/t/195396/java-programmer-SCJP/certification/String-tricky
3/5
7/16/13
String s2 = (s0.concat("a")); results in a new String object and since the memory address of this newly created object referred to by s2 is different, "s1==s2" returns "false". However, System.out.println("s1.equals(s2) : " + s1.equals(s2))); returns "true", as the equals() method on String objects checks for the equality of the contents of the two String objects compared. case2: String s1 = "amita"; String s3 = (s1.toLowerCase()); As the String object refered to by s1 is already in lowercase, the toLowercase() operation on s1 does not create a new object and hence s3 still refers to the memory address of s1. It's equivalent to assigning s1 to s3 i.e, s3=s1; Therefore String s3 = (s1.toLowerCase()); returns "true". - Suresh Selvaraj
Suresh Selvaraj, (author of JQuiz)<br />SC JP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a> Suresh Selvaraj Ranch Hand Joined: Nov 14, 2000 Posts: 104
Hi, Here is the explanation for the 2nd question. Answer based on following code: 1 Choice c1 = new Choice(); 2 c1.add("First"); 3 c1.addItem("Second"); 4 c1.add("Third"); 5 c1.insert("Lastadded",1000); 6 System.out.println(c1.getItemCount()); a) Compile time error b) Run time error at line 5 c) No error and line 6 will print 1000 d) No error and line 6 will print 4 Correct ANswer is d) why? what is the meaning of c1.insert("Lastadded",1000); Mansi Answer: A choice is a pull-down list. You can create a new Choice menu by creating a new object of Choice. Ex: Choice c = new Choice; addItem(String item) The addItem() method adds an item to the Choice menu. public int getItemCount() You can get the number of items in a Choice menu by using the method getItemCount(). Inserting items at specified index: public void insert(String item, int index) Inserts the item into this choice at the specified position. So Line1 creates a new Choice object. Line2, 3 and 4 adds items "First", "Second" and "Third" to the Choice menu respectively.
www.coderanch.com/t/195396/java-programmer-SCJP/certification/String-tricky
4/5
7/16/13
Line4 inserts "Lastadded" item at index/position 1000. Since four items are added to the Choice component, the getItemCount() method returns '4' which is answer 'd'. - Suresh Selvaraj
For the 1st question the answer is (a) because, System.out.println(s1.replace('m','r')); // will print arit System.out.println(s1); // will print amit // (since the string objects are immutable (not changeable)) String s3="arit"; // s3 & s4 references will point to the same String s4="arit"; // String object in the string pool. String s2 = s1.replace('m','r'); // Here s2 will recieve new String object which will be located out side of the string pool System.out.println(s2==s3); // will print false since the references are different System.out.println(s3==s4); // will print true since the references are same.
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads replace() method in strings....... String Creation one more on == doubt about Strings from Whizlabs immutablity of strings
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 16, 2013 07:21:36 .
www.coderanch.com/t/195396/java-programmer-SCJP/certification/String-tricky
5/5