Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Beginner
Nov 13, 1998



Application Security Testing: An Integral Part of DevOps

Use Object.equals(), not ==, for Logical Comparison

One of the most common gotchas for beginning Java programmers involves comparing Objects, especially Strings, with the == operator. Every Object in Java has a unique location in the Java Virtual Machine's memory. The == operator returns true if the two handles on the left and right sides refer to the same location in memory. The function Object.equals() is intended to return true if two handles refer to logically equivalent instances. In the most basic case, this function works by comparing the value of each of the data fields of the two instances and returning true only if they're all equivalent. You should override Object.equals() in your own classes to provide more robust logic:
public class StringCompare{
	public static void main(String[] args){
		If(args[0] == "test"){
			System.out.println(args[0] + " == \"test\"");
			System.out.println(args[0] + " != \"test\"");

				args[0] + ".equals(\"test\") == true");
				args[0] + ".equals(\"test\") == false");
Larry O'Brien
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date