-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringPool.java
More file actions
48 lines (43 loc) · 2.2 KB
/
Copy pathStringPool.java
File metadata and controls
48 lines (43 loc) · 2.2 KB
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
39
40
41
42
43
44
45
46
47
48
public class StringPool {
public static void main(String[]args){
method1();
method2();
method3();
}
public static void method1(){
String mystring1 = "Hello World"; //Creates an entry in the String Pool. mystring1 has a unique memory location
String mystring2 = new String("Hello World"); //Creates a new String object. mystring2 is a new String therefore has a unique memory location
if (mystring1 == mystring2) //fundamental operator == checks if the arguments memory location are the same. ie the exact same instance
{
System.out.println("M1 The 2 strings are equal"); //different memory locations so will NOT be printed to screen
}
else
{
System.out.println("M1 The 2 strings are not equal"); //This statement is correct
}
}
public static void method2(){
String mystring1 = "Hello World"; //Creates an entry in the String Pool.local variable declared within a method mystring1 has a unique memory location
String mystring2 = new String("Hello World"); //Creates a new String object. mystring2 is a new String therefore has a unique memory location
if (mystring1.equals(mystring2)) //equals() is an instance method which is fundamentally defined by the java.lang.Object class.
{ //The method, .equals() tests to see if the two objects being compared to each other are equivalent , but they need not be the exact same instance of the same object.
System.out.println("M2 The 2 strings are equal"); //"Hello World" = "Hello World" this statement is correct and will be output
}
else
{
System.out.println("M2 The 2 strings are not equal"); //incorrect the 2 strings are equal
}
}
public static void method3(){
String mystring1 = "Hello World"; //Creates an entry in the String Pool
String mystring2 = "Hello World"; //Creates an entry in the String Pool
if (mystring1 == mystring2) //Both Strings are stored in String Pool and have the same memory location
{
System.out.println("M3 The 2 strings are equal"); //This is correct and will be printed to screen
}
else
{
System.out.println("M3 The 2 strings are not equal"); //incorrect both strings are in the same location therefore == is true.
}
}
}