A quick comparison between == vs equals in Java


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: Undefined array key "layout" in /home/bateeqjg/public_html/news/wp-content/plugins/wp-about-author/wp-about-author.php on line 94

/**
@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

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

js2coffee.org helps your learn coffeescript faster


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

coffeescript icon
coffeescript icon

Coffee-script is a smoking hot simple language that converts 100% into Javascript code. Coffee-script allows you to spend more time focusing on the algorithm rather than the syntax.
js2coffee.org is an awesome website that converts your Coffee-script to Javascript and vice versa on the fly. So you can learn Coffee-script faster.
Example:
Let’s convert my Random Number Generator code from a previous post to Coffee-script.

// Javascript version
var getRandomNum = function( i, decLen ){
	return (Math.random() * (i||1) ).toFixed(decLen);
};

# My Coffee-script version
getRandomNum = ( i=1, decLen ) -> (Math.random()*i).toFixed(decLen)

# js2coffee.org Coffee-script verison
getRandomNum = (i, decLen) ->
	(Math.random() * (i or 1)).toFixed decLen

Awesome huh?

Check out a demo of Coffeescript here.

I’ll like to leave you with a HTML5 Coffee-script presentation.

Cheers.

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Simple fix for MSI Windows Installer Error

Here’s a quick post over how to install applications that encounter the Windows Installer Error.


Error Message:

Windows Installer
This installation package could not be opened.
Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.

Alternative Solutions not presented in the video.
1) Avoid msi files and go for an alternative file format to install your application.
2) Install the application using the hidden Administrator account.
Here’s a Guide
3) Reinstall Windows on your computer.
4) Re-download the file from a different website.
5) System Restore to a period where the problem didn’t exist.
6) Extract the content of the MSI file and run the executable from there.
Extract MSI contents.batch

REM Extracts the filename.msi file to C:\extracted_files
msiexec /a filename.msi /qb TARGETDIR=C:\extracted_files

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube

Check out this awesome Coffeescript Tutorial



http://bodil.github.com/coffeescript/

Larry Battle

Larry Battle

I love to program, and discover new tech. Check out my <a href="http://stackoverflow.com/users/527776/larry-battle">stackoverflow</a> and <a href="https://github.com/LarryBattle">github</a> accounts.

More Posts - Website

Follow Me:Add me on XAdd me on LinkedInAdd me on YouTube