How to compare Strings in Java tutorial | String pool in Java |Why we don't use == to compare String

Опубликовано: 19 Май 2024
на канале: Muhammad Shahzaib
37
6

ASSALAM o alaikum
Code:
/*
真主
穆罕默德 真主啊,为我们的大师穆罕默德和他的家人祈祷与和平 77 85 72 65 77 77 65 68 32
83 104 97 104 122 97 105 98
*/

public class StringsComparison
{
public static void main(String[] args)
{
/*
== operator compares the objects (references) of Strings. If the reference is same
it says true else false. It doesn't compare the values of Strings
Syntax:
String a==String b
*/

System.out.println("\n Using == and same object refernece : ");

String firstString="12"; ////////////// an object of String class with name "firstString" and value "12"
String secondString="12"; ///////////// same object of String class with new name "secondString" and value "12"
/// (new object didnt formed) as JVM checked that "12" value has an object in strings pool so it reused that object

if(firstString==secondString)
{
System.out.println("Both are same");
}

else
{
System.out.println("Both are different");
}


System.out.println("\n Using == and different object referneces : ");


String thirdString="12"; ///////////// an object of String class with name "thirdString" and value "12"
String fourthString= new String("12"); //////////// now a new object is formed as we specifically wrote "new String"
////////////// Both objects are now different one with name "thirdString" and one with "fourthString"

if(thirdString==fourthString) ///// It will always be false as now objects are different no matter values are same as == compares the objects
{
System.out.println("Both are same");
}

else
{
System.out.println("Both are different");
}


//// ------------------------------------------------------------------------------------------------------------///////
//// ------------------------------------------------------------------------------------------------------------///////

System.out.println("\n Using .equals() and different object referneces : ");

/*
.equals() operator/method compares the values of Strings. If the values are same
it says true else false.
Syntax:
String a.equals(String b)
*/

String fifthString="12"; /// an object of String class with name "fifthString" and value "12"
String sixthString=new String("12");//////////// now a new object is formed as we specifically wrote "new String"
////////////// Both objects are now different one with name "fifthString" and one with "sixthString"
if(fifthString.equals(sixthString)) ///// It will true as both objects have same values and .equals() compares Strings values
{
System.out.println("Both are same");
}

else
{
System.out.println("Both are different");
}


}

}

For any suggestions/complaint:
mshahzaib6699@gmail.com


На этой странице сайта вы можете посмотреть видео онлайн How to compare Strings in Java tutorial | String pool in Java |Why we don't use == to compare String длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Muhammad Shahzaib 19 Май 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 37 раз и оно понравилось 6 зрителям. Приятного просмотра!