+ 15

[SOLVED]Why is output true in this code?

Saw similar code question in a challenge https://code.sololearn.com/cQn20Y4Iv8ME/?ref=app

22nd Oct 2017, 5:41 PM
David Akhihiero
David Akhihiero - avatar
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.
22nd Oct 2017, 6:04 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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
22nd Oct 2017, 9:24 PM
David Akhihiero
David Akhihiero - avatar
+ 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
22nd Oct 2017, 7:26 PM
David Akhihiero
David Akhihiero - avatar
+ 8
@Marko no problem 😊
22nd Oct 2017, 7:51 PM
David Akhihiero
David Akhihiero - avatar
+ 7
thanks Pranit
23rd Oct 2017, 7:31 PM
David Akhihiero
David Akhihiero - avatar
23rd Oct 2017, 4:10 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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:)
23rd Oct 2017, 5:26 PM
Pranit Gandhi
Pranit Gandhi - avatar
+ 3
Dear friends, which is a most selling language skills. java, Php, python...?
22nd Oct 2017, 9:21 PM
Smart
Smart - avatar
0
Wow i am really sorry then i saw your profile and exp i thought you were kidding.
22nd Oct 2017, 7:32 PM
Marko Majstorovic
Marko Majstorovic - avatar
0
It is going to be true because you are comparing two strings, but when implementing this using objects you use the .equals () method
8th Dec 2017, 11:52 PM
Tobiloba Williams
Tobiloba Williams - avatar
- 1
You have 6k experience in Java and you dont know why is it true ?
22nd Oct 2017, 6:04 PM
Marko Majstorovic
Marko Majstorovic - avatar