-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathStringcomp.java
38 lines (25 loc) · 1.34 KB
/
Stringcomp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
public class Stringcomp{
public static void main(String [] args)
{
String password = "Let me in Man";
System.out.print("Guess the pass to get in: ");
Scanner pass2GetIn= new Scanner(System.in);
String guess = pass2GetIn.nextLine();//guess is a container that points to the string
System.out.println(password.equals(guess));//How to compare the value of 2 strings
//prints true
//Comparison -> password == guess ... This works when comparing primitive types
//For example Strings would error when using .equals because String are ! primitives
//When comparing objects we are not comparing the values we are comparing the addresses
//in memory.
//But NOT RECOMMENDED when comparing objects.
//Because values of objects are a reference to the object not PBV they're PBR
System.out.println(password==guess);
String alpha = new String("a");
String betaButAlpha = new String("a");
System.out.println(alpha==betaButAlpha);
//false because alpha and betaButAlpha are String objects
//the == compares their address in Memory
//alpha is stored in a different location than betaButAlpha in Memory
}
}