Interview Question

Solution Consultant Interview

-

Guidewire

My most unexpected question was on the nature of strings in java. e.g. the different ways that strings can be considered equal, etc...

AnswerAdd Tags

Interview Answers

4 Answers

0

String are immutable so they usually go into the string pool after creation, all identical strings there should point to the same string object.

Anonymous on

0

Thanks for extending the answer Jeff. I am always unsure as to how to phrase the rules about String.

Anonymous on

0

short answer: strings should be compared using equals method just like any other object. long answer: just like Integers strings can be cached. in a string pool. So sometimes if strings are equal == can also be true. This is called string interning. There's also a method string.intern() that will return cached string if it exists or add instance on which it was called to cache and return it. regarding equalsIgnoreCase: there are 2 methods with locale and without. Using equalsIgnoreCase without locale is almost always a bad choice, because equalsIgnoreCase depends on locale (e.g. not in all locales "i".equalsIgnoreCase("I") is true). so you should prefer using method with locale.

Anonymous on

0

I'm assuming that they did not like your answer. In Java, String values should be compared using the ".equals" method. Doing identity comparisons, using the "==" operator often fails. There is no background or "garbage collection" process that moves String values into the constant pool. Constant String values in code are put in the constant pool. But values read from databases, files, and from the user are almost never put into the constant pool. So in nearly all cases, in typical business programming, you must use the "equals" method to compare String values.

Jeff Grigg on

Add Answers or Comments

To comment on this, Sign In or Sign Up.