More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. It will print "true" with integers as well as strings. The reason is "Integer constant pool" and "String pool"
String pool maintains pool of string literals. When a string literal is used for the first time, a new string object is created and is added to the pool. Upon it's subsequent usage , the reference for the same object is returned. Similarly java uses integer constant pool.
Help us improve. Please let us know the company, where you were asked this question :
Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool.
String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory.
String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  string pool  integer constant pool  string comparison  integer comparison
Q66. What is the purpose of String Pool ? Don't you think it's a performance overhead to have unnecessary check for string in string pool ?
Ans. String Pool makes Java more memory efficient by providing a reusable place for string literals. It might be a little performance inconvenience but results in good amount memory saving.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Being final in the sense of objects or object reference means that the reference cannot be reassigned to a different object whereas being immutable means that the object contents cannot be changed.
Strings in Java are immutable.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  string  immutable  immutability  final vs immutable  immutability
Ans. String has an argument constructor that take char array as argument and creates a string.
There is no constructor available with String that takes in a character and creates a String. We can use StringBuilder which has a char argument constructor.
Help us improve. Please let us know the company, where you were asked this question :
LikeDiscussCorrect / Improve  character  char  char array  String  char to String  char array to String
Q76. Why shouldn't we use string concatenation extensively or in a loop ?
Ans. Because String being an immutable object creates a new object upon each concatenation cycle. If there is any such need , we should use String Builder whose objects are mutable.
Help us improve. Please let us know the company, where you were asked this question :
Q77. Suppose we have a string "Java is object oriented language" and we have to reverse every alternate word in string. How would we do that using Java program.
Q78. Given a String with letters , numbers and special characters , extract Tokens from it. The rules for Token extraction are ac follows
1. Should for token of all characters till a number or special character is found
2. Should form token of all numbers till a character or special character is found
3. Special character in itself is a token
4. Ignore white spaces
For example - Bob Said "He is here"
should result in
Bob A
Said A
" S
He A
is A
here A
" S
where a is specifying alphabet and S as special character
Ans. It depends on how complex it is and if in future it would need any sort opf debugging. It's not easy to debug code if it's making heavy use of shortcuts like Lambda , Patterns etc.
Help us improve. Please let us know the company, where you were asked this question :