+ 15
[SOLVED]Why is output true in this code?
Saw similar code question in a challenge https://code.sololearn.com/cQn20Y4Iv8ME/?ref=app
11 Antworten
+ 8
When comparing String values in Java you should use the .equals() method. Strings in Java are an immutable Object type and not a primitive type. You may however, still get a true result when using == instead, on occasion, but this should not be relied upon. This is due to how Java creates and uses an optimized String pool in memory for commonly used Strings within a Java program. If the Java compiler can find an exact matching String within a program at compile time it may assign both variables to the same memory address. Thus, making them the same object (==) and value (.equals()) at that time and until one is changed and assigned a new address and value as String types are immutable in Java.
+ 10
@Smart can't really say but Android ( based on java) and web are quite popular but money should not be the main reason to learn a language
+ 9
@Marko no one knows everything, I am still learning, for a long time I thought "==" would always give false when comparing string values and that only equals() could be used
+ 8
@Marko no problem 😊
+ 7
thanks Pranit
+ 4
i hope you understand
https://code.sololearn.com/chbPQa8a6mt8/?ref=app
+ 4
@Yerucham
Actually it's very big explanation for your questions but I want to clear your doubt in deep way so I just explained in detail...
String is an predefined class in Java lang.
u can create String in two ways
1) Direct option->
String str1 = "Hi" ;
2) With the help of 'new' Operator
String str2 = new String (Hi);
so for this method the major difference is memory allocation
--------------------------------------------------------------------- If u create a String object with 'new' Operator it will create object in heap memory and every time creating object with 'new' Operator will Creates a new object for every reference variable (if there is old object is present with same content in heap memory it will not check it , every time just create new one)
so ex:
String str1 = new String (Hi);
String str2 = new String (Hi);
both having same data but pointing to different object so there memory address is different that's why
"str1==str2 is false in this case"
---------------------------------------------------------------------
---------------------------------------------------------------------
if you create object with direct method like
String str1 = "Hi" ;
String str2 = "Hi" ;
it will create object in ""String Constant Pool"" inside the heap memory and every time creating object with direct method , it firstly check is there any old object is present with same content if yes(available) then the new object just point to that old content(which avoid duplication) (if there is old object is present with same content it will always check and if available just point to same data and if not available then create new one )
so bcoz of these in below example
String str1 = "Hi" ;
String str2 = "Hi" ;
str1 and str2 pointing to same object so
str1==str2 results to true
---------------------------------------------------------------------
thank you:)
+ 3
Dear friends, which is a most selling language skills. java, Php, python...?
0
Wow i am really sorry then i saw your profile and exp i thought you were kidding.
0
It is going to be true because you are comparing two strings, but when implementing this using objects you use the .equals () method
- 1
You have 6k experience in Java and you dont know why is it true ?