0

Suppose I have strings:

String a = "hello"; 
String b = "h";
String c = "ello";

String d = b+c;

When I check for a==d it returns false.

Please correct me if am wrong, the bytecode would contain string d value as hello right? I want to know why is it that during execution of the program,string d is not picked up from string pool as hello is already available in string pool and hence returning false as above when checked for equality.

Trooper
  • 145
  • 3
  • 15

4 Answers4

2

During the execution StringBuilder will be created which itself will create a String object out of char array.

Sample: StringBuilder dBuilder = new StringBuilder(); dBuilder.append(b); dBuilder.append(c); String d = dBuilder.toString(); // here new String(value, 0, count); will be called, where value is the char array and count is the size of the resulting string.

noobsaibot
  • 151
  • 2
  • 7
  • Hi do you have any source for this information? Thanks. – Trooper Jun 24 '18 at 19:04
  • Sure, assuming you have class Test. Run `$ javac Test` to compile then `$ javap -c Test.class` to disassemble. Sample: https://pastebin.com/Fbbu9uCz – noobsaibot Jun 24 '18 at 19:59
  • There is a note in the JLS that compiler may perform such kind of optimization. https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1 – noobsaibot Jun 24 '18 at 20:06
2

Please correct me if am wrong, the bytecode would contain string d value as hello right?

You are wrong. (You can see that you are wrong by inspecting the bytecode for yourself.)

The value of d is evaluated at runtime, by concatenating b and c.

It is only if you declare both b and c as final that this becomes true: then they are both compile time constant expressions (*).

This means that the value assigned to d is a compile-time constant expression, so it is evaluated at compile time to be "hello". But only one "hello" is inserted into the constant pool, because no more are necessary.

Hence, a == d would be true.


(*) final-ness is a necessary but not sufficient condition to be a compile-time constant; the other relevant fact is that they are assigned a compile-time constant value, namely a string literal value.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thank you.. Is StringBuilder also involved in this as mentioned in the 1st answer by noobsaibot – Trooper Jun 24 '18 at 18:48
  • 1
    @Anuraag it isn't necessarily involved; it could be `d = "".concat(b).concat(c)`, or any other means (the implementation of concatenation by `+` is not stipulated by the language spec). – Andy Turner Jun 24 '18 at 18:50
  • 1
    @Anuraag you can discover that by compiling your class, and then using `javap -c fully.qualified.name.of.YourClass` – JB Nizet Jun 24 '18 at 18:50
  • @JBNizet that only tells you [the colour of one side of one sheep](http://www.mathmos.net/misc/jokes.html) ;) (in other words' it would only tell OP what his/her current compiler does to that code). – Andy Turner Jun 24 '18 at 18:53
  • 1
    @AndyTurner sure, but that's the side you care about :) – JB Nizet Jun 24 '18 at 18:54
  • 1
    @JBNizet I just tried the javap functionality and found that StringBuilder is involved. – Trooper Jun 24 '18 at 19:00
0

The == operator checks two strings are point exactly the same object. here a,b,c Reference variable have different hash Code. actually == comparing the memory address. If they are both equal it will return true and false

0

If you say to Java put the d value to the pool

String d = (b+c).intern();

it will return true; the example in here(https://ideone.com/Y100H4)

dehasi
  • 2,644
  • 1
  • 19
  • 31