What are the outputs of b1 and b2?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What are the outputs of b1 and b2??

This is a very common Java OCA like question for those willing to get Java Certified at an Associate level. This exam is a multiple choice one, with a lot of code to analyze but only 1 right answer. Check the code to analyze here: https://code.sololearn.com/cy1eg0at8vdi/#java Provide your answer as per your logic of String handling in Java. Good luck.

9th Nov 2018, 3:35 PM
Roberto Guisarre
Roberto Guisarre - avatar
2 Answers
+ 8
//duplicate removed
9th Nov 2018, 4:24 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
One of the key goals of any good programming language is to make efficient use of memory. As an application grows, it's very common for string literals to occupy large amounts of a program's memory, and there is often a lot of redundancy within the universe of String literals for a program. To make Java more memory efficient, the JVM sets aside a special area of memory called the String constant pool. When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created. (The existing String simply has an additional reference.) Now you can start to see why making String objects immutable is such a good idea. If several reference variables refer to the same String without even knowing it, it would be very bad if any of them could change the String's value.
9th Nov 2018, 9:32 PM
Danijel Ivanović
Danijel Ivanović - avatar