• Post Reply Bookmark Topic Watch Topic
  • New Topic

String pool & function call  RSS feed

 
Catherine Fang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

String pool puzzles me again. I think a==b and a==c are both true...




Thanks.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method g() returns String "abc" which is in a String pool because it is a compile time constant.
So when it is compared to "abc" with equality operator it yields true.

On the other hand, the result of f() + "c" is calculated at a runtime and a new object is created as a result of that.
So it is not surprising that equality operator yields false.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Method g() returns String "abc" which is in a String pool because it is a compile time constant.
So when it is compared to "abc" with equality operator it yields true.
On the other hand, the result of f() + "c" is calculated at a runtime and a new object is created as a result of that.

Which just leaves 'd'.

@Catherine: I think the reason why a==d returns true is because the compiler is smart enough to understand that the concatenation of two literals is another literal - in this case "abc" - so it doesn't strictly have anything to do with the String pool; it's more about the way the compiler handles stuff like that.

And TBH, this stuff really isn't terribly important. You might need it to get through an SCJP exam, but after that you can probably forget all about it. And furthermore, you should NEVER use '==' with Strings. See AvoidTheEqualityOperator (←click).

Well, almost never...

Winston
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
@Catherine: I think the reason why a==d returns true is because the compiler is smart enough to understand that the concatenation of two literals is another literal - in this case "abc" - so it doesn't strictly have anything to do with the String pool; it's more about the way the compiler handles stuff like that.


It is defined in the Java Language Specification as such -- the concatenation of two compile time constants is a compile time constant.

Henry
 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Method g() returns String "abc" which is in a String pool because it is a compile time constant.

Am I correct in assuming that the String returned by g() is not a compile-time constant because it is not known at compile-time?

In the following code, x is not a compile-time constant and this is the reason for the compiler error.

In the following code, x is a compile-time constant and so there is no compiler error.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Jacobs wrote:Am I correct in assuming that the String returned by g() is not a compile-time constant because it is not known at compile-time?

In your first example, the variable x is not a compile time constant. You are correct.
Note that I never said any variable was a compile time constant. I said "abc" is a compile-time constant.
It is quite obvious. It would be strange it String literal were not a compile-time constant.

You are wrong about your second example. It does not compile.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Jacobs wrote:
Paweł Baczyński wrote:Method g() returns String "abc" which is in a String pool because it is a compile time constant.

Am I correct in assuming that the String returned by g() is not a compile-time constant because it is not known at compile-time?

In the following code, x is not a compile-time constant and this is the reason for the compiler error.


These are actually two different concepts being discussed here. The OP's topic is in regards to string literals, being compile time constants, and hence, whether or not they are in the string pool.

This latest discussion, is in regards to whether a variable is a compile time constant variable or not, and hence, whether the compiler can confirm that something has been initialized, and hence, compiles or not.

How did one topic of discussion lead to the other topic of discussion?
Henry
 
Tess Jacobs
Ranch Hand
Posts: 71
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:It does not compile.

The second example prints hello

Henry Wong wrote:How did one topic of discussion lead to the other topic of discussion?

I misunderstood the comment posted by @Paweł. In the statement String c = g();, I thought he was implying that c is a compile-time constant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!