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
Auf dieser Seite können Sie das Online-Video How to compare Strings in Java tutorial | String pool in Java |Why we don't use == to compare String mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Muhammad Shahzaib 19 Mai 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 37 Mal angesehen und es wurde von 6 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!