Tricky string Java question #softwareengineer #computerscience #java

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Actual explanation is
The literal string value is stored in "string constant pool" inside the heap, but the string value which is created using new keyword is stored in heap but outside of string constant pool so it is considered as object which should have seperate reference name

balajisrinivasan
Автор

you shouldve noted that this is the case for all objects, only use == for primitive types (int, boolean, double, etc), everything else should be compared with .equals

gtALIEN
Автор

I guessed correctly that the "new" keyword would allocate the string literal to a different pointer in memory on the heap, but the == operator being a reference and not a value comparison is genuinely weird.

pchan
Автор

After you called inter method it should be true

sujithg
Автор

That is somehow confusing to me when it comes to reference. Can anyone explain to me "is" keyword and "==" keyword? In my mind, I thought "==" check the the value while "is" check the references. Python language. Oh if it is java, maybe "==" will point out the references.

khoilucas
Автор

Nope. First one is stack allocated. The type doesn't matter. The second is heap allocated. The new keyword should be a dead giveaway. So yes it's comparing refs because they are allocated differently.

PaulSebastianManole
welcome to shbcf.ru