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

530 Valid Hostname Is Expected When Setting Up IIS 10 For Multiple Sites

C Perror Example

Converting A String To Int In Groovy