Java string comparison

Mostly just a ‘duh’ moment by myself.

I was fetching properties out of a database using hibernate objects. In my test scenario I asked if "true" == "true" which does work. However a String object does not always equal a string literal nicely.

public class Test {

	public static void main(String args[]) {
		System.out.println("String comparison");
		// BAD could be true, but could also be false
		boolean result = "true" == "true" ? true : false;
		// GOOD will actually compare the strings, not the objects
		boolean result2 = hibernate_object.getProperties().get("boolean property").equalsIgnoreCase("true") ? true : false;

		System.out.println(result);
		System.out.println(result2);
	}
}

String JavaDocs


Author’s Note

Initial md Generated using https://github.com/jsr6720/wordpress-html-scraper-to-md

Original Wordpress categories: [‘Java’]

Original Wordpress tags: “Java”, “Java”, “string comparison”

Original Wordpress comments: None

Significant revisions

tags: 2011, wordpress, txcowboycoder, java, strings

  • May 6th, 2024 Converted to jekyll markdown format and copied to personal site
  • May 23rd, 2011 Originally published on txcowboycoder wordpress site

EOF/Footnotes