equals method in java code example

Example 1: equals example java

import java.util.Scanner;

public class YourProjectName {

	public static void main(String[] args) {
		Scanner keyboard = new Scanner(System.in);
		
		//D
		
		String password1;
		String password2;
		String msg;
		
		//E
		
		System.out.println("Enter password: ");
			password1 = keyboard.nextLine();
		
		System.out.println("Repeat password: ");
			password2 = keyboard.nextLine();
		
		//P
		
		if (password1.equals(password2)) {
			msg = "Password matching!";
		} else {
			msg = "Password not match";
		}
		
		//S
		
		System.out.println(msg);
		
	}

}

Example 2: java equal method

if(bob.equals("Sam")){ // Checks if strings are equal.
  // code
}

Tags:

Java Example