Monthly Archives: April 2012

A quick comparison between == vs equals in Java

/**
@author Larry Battle <bateru.com/news>
@date April 19, 2012
@purpose To show a simple comparison between == and equals().
*/
 
public class SameObjectTest {
	public static boolean areAssertsEnabled(){
		boolean isEnabled = false;
		try{
			assert false: "Yes asserts are enabled.";
		}catch( AssertionError e ){
			isEnabled = true;
		}
		return isEnabled;
	}
	public static void main(String[] args ){
		// All asserts should be without an error.
		String message = "Test Completed with no errors.";
		int[] ints = new int[]{ 10, 10, 20 };
		String[] strings = new String[]{ new String( "Duck" ), new String( "Duck" ), new String( "Goose!" ) };
		Worker[] workers = new Worker[]{ new Worker( "Working" ), new Worker( "Working" ), new Worker( "Sleeping" ) };
 
		assert ints[0] == ints[1] : "10 is 10";
		assert ints[1] != ints[2] : "10 is not 20";
		// Primative data types can't use ints[i].equals( ints[j] ) because they don't have methods.
 
		// Strings are a little bit more tricky. Go here for more inforamtion. http://www.janeg.ca/scjp/lang/strLiteral.html
		assert strings[0] == strings[0]: "An equality check, ==, on objects compares the references, not the values.";
		assert strings[0] != strings[1]: "strings[0] and strings[1] do not have the same reference point. In otherwords, that don't have the same Class and hashCodes.";
		assert strings[0].equals( strings[1] ): "String equals methods is predefined to compare the value of the string objects.";
		assert !strings[0].equals( strings[2] ): "the string duck should not equal the string goose!";
 
		// You have to override the equals methods for user-defined objects.
		assert workers[0] != workers[1]: "workers[0] and workers[1] have two different hash values.";
		assert workers[0].equals( workers[1] ): "However workers[0] and workers[1] are equivalent to eachother, according to the equals method.";
		assert !workers[1].equals( workers[2] ): "But this is not the case for workers[1] and workers[2].";
 
		message = ( areAssertsEnabled() ) ? message : "Asserts are disabled! Please enable with the switch -ea, ex. java -ea ";
		System.out.println( message );
	}
}
class Worker{
	public String status;
	public Worker( String status ){
		this.status = ( status != null ) ? status : "Unknown";
	}
	public boolean equals( Object obj ){
		boolean areSame = false;
		Worker x;
		if( obj != null  ){
			if( obj == this ){
				areSame = true;
			}
			if( this.getClass() == obj.getClass() ){
				x = (Worker) obj;
				if( x != null && x.status.equals( this.status ) ){
					areSame = true;
				}
			}
		}
		return areSame;
	}
}

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
Twitter

Video of 5 Autonomous Robots

New Honda Robot ASIMO 2012 – All features and behaviors

BigDog Evolution

A Swarm of Nano Quadrotors

Freaky Life Like A.I Robot

Awesome Robot For Kids, 15 inches High

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
Twitter

4 Notepad++ Plugins for Javascript Developers

Here are some Notepad++ plugins for JavaScript Developers that I found useful.

Tip:
You can install all the plugins using Notepad++ Plugins Manager located in the “Plugins” Menu.

  1. JSLint


    Description:
    JSLint is a static code analysis tool used in software development for checking if JavaScript source code complies with coding rules.


    Tip:
    If jslint turns out to be strict for you, then I recommend that you try jshint.com.
    Does it make any sense to use JSLint and follow it?

  2. JSMin


    Description:
    JSMin is a filter which removes comments and unnecessary whitespace from JavaScript files.


    Tip:
    The great thing about this plugin is that it formats and minimizes.
    Link:
    JavaScript Compressor and Comparison Utility.

  3. JSON Viewer


    Description:
    A JSON viewer plugin for Notepad++. Displays the selected JSON string in a tree view.


    Tip:
    If you get “Could not parse!!” error message, then reformat your code with JSON.stringify().

  4. RegEx Helper


    Description:
    A Notepad++ plugin that allows users to develop regular expressions and test them against their open documents.

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.

More Posts - Website

Follow Me:
Twitter