pre-knowledge

Remark

For String literals you work in the String Class, therefore you could use the equals method but you can also use the == operator.

CREATING A SITUATION WITH String literals

Adding some text to this space.. “ey bro”text java

package Compare;

class Compare {
	
	void start() {
		String text = "ey bro";
		
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}

SCENARIO’S

1. ONLY ONE SCENARIO COULD OCCUR FOR PRIMITIVE DATATYPES

If I want to compare the text, e.g. “ey bro” to the text “ey bro”, it is like a reference to itselfreference java

 

One could just use the == command because it is a reference to itself. However, because the text is saved in the String Class, you can also use the built-in function .equals().

==
package Compare;

class Compare {
	
	void start() {
		String text = "ey bro";
		if (text == "ey bro") {
			System.out.println("true");
		}
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}

 

.equals()
package Compare;

class Compare {
	
	void start() {
		String text = "ey bro";
		String text2 = "ey bro";
		if (text.equals(text2)) {
			System.out.println("true");
		}
	}

	public static void main(String[] args) {
		new Compare().start();
	}
}