Can anyone please explain, why the outputs are printing two different boolean values. why aren't strings being compared not equal in this case. while simply comparing two strings with '==' operator does compare the strings. For example, System.out.print("paper" == "paper") prints true. Thanks in advance. :)
package my_pack;
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;
public class game {
public static void main(String[] args) {
//get the user input get_input()
Scanner scn = new Scanner(System.in);
System.out.println("Rock Paper or Scissor? Enter Your choice: ");
String user_choice = scn.nextLine();
user_choice = user_choice.toLowerCase();
//get the random number
Random rand = new Random();
int random_num = rand.nextInt(3) + 1;
//assign the number to specified method
String comp_choice ="";
switch (random_num){
case 1:
comp_choice = "rock";
break;
case 2:
comp_choice = "paper";
break;
case 3:
comp_choice = "scissor";
break;
}
System.out.println(comp_choice);
System.out.println(user_choice == comp_choice); //paper == paper (false)
System.out.println(Objects.equals(user_choice, comp_choice)); //paper == paper (true)
}
}