Coparing Strings Java Code Example


Example 1: java how to compare strings


System.out.println("hey".equals("hey")); //prints true

/*
always use .equals() instead of ==,
because == does the compare the string content but
loosely where the string is stored in.
*/

Example 2: Java compare two strings


// compare two strings in java .equals() method
public class EqualOperatorDemo
{
public static void main(String[] args)
{
String str1 = new String("helloworld");
String str2 = new String("helloworld");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable