What is Shankh???


Shankh is a synonym for Conch Shell.So why that name for this blog?? My grandma used to say "When you put the conch shell to your ear, you hear the sound of ocean". Ignoring all scientific explanations to it, i would like this blog to bring you the sound of vast ocean of technology. Take a look

Some Random quotes


Innovation distinguishes between a leader and a follower. -Steve Jobs

It is not that i am smarter than others, i just persist with problems longer

Archive: programming

Equals Method in StringBuffer

Consider this code snippet

StringBuffer s1 = new StringBuffer("shankh");
StringBuffer s2 = new StringBuffer("shankh");

System.out.println("Equality Check 1:"+(s1==s2)); //returns false as expected
System.out.println("Equality Check 2:"+(s1.equals(s2)));
//returns false but was expecting true

Why did the equals method return false? The answer lies in one of the most common mistakes of Java equals usage. StringBuffer doesn’t override the equals method and it is using the equals method inherited from Object. So behind the scenes you ended up checking this if(s1==s2) and not comparing the contents.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html#equals%28java.lang.Object%29states
“The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).”

If you want to compare the contents of StringBuffer then this is one way of doing it.

System.out.println("Equality Check 3:"+(s1.toString().equals(s2.toString())));
 //true as expected

It works fine as String overrides the equal method defined in Object Class.

There is an interesting article written by the Scala guys(Martin Odersky, Lex Spoon, and Bill Venners) about implementing equality in Java. http://www.artima.com/lejava/articles/equality.html

Reblog this post [with Zemanta]
  • Share/Bookmark

Clear Flash Log file

Few days back one of my colleagues, a flash developer was asking about any simple utility available to clear the flash log created by the debug flash player. Flash Debug player will log the output to this folder in windows
C:\Documents and Settings\\Application Data\Macromedia\Flash Player\Logs\ . After testing the code, it is very annoying to go to that folder, open the file and then clear the log. He wanted a simple one click utility to clear the flashlog file without deleting the file (he is tailing the file using baretail)

So i wrote a simple batch script to do that trick.You can download it here Clear Flash Log Utility

Download the attached zip file to your desktop and unzip it. Inside that there is a file called Clear Flash Log.bat. Open the file in any text editor like notepad or wordpad. On line 3 replace the word ‘your_windows_user_name’ with the username you are using to log into windows. Save it and hooray!!! you have got a simple flash log clearing utility. I am curious to hear whether this was helpful for anyone else …

  • Share/Bookmark

JavaFx Coding Challenge Results

musicExplorerFx Image

musicExplorerFx Image

1st Place


Music Explorer FX, Sten Anderson

lifescope image

lifescope image

2nd Place

Lifescope, Naoaki Suganuma

shining EtherFx Image

shining EtherFx Image

3rd Place

ShiningEtherFX, Evgeni Sergeev

No Image Student Prize

Caesar Photobook Mobile, Ramin Mohammadi

Real Track car race Image

Real Track car race Image

Student Prize

Real Track Car Race, Diego Benna

Calcfx image

Calcfx image

Student Prize

CalcFX, Kazuki Hamasaki

Congratulations to all winners. Well Done !!! Of all the entries MusixExplorerFx looks really cool and no wonder it got the first prize.

  • Share/Bookmark

Maven Commands Reference Mini Guide

In maven , we have several commands to remember and i was thinking of having a page with simple command reference so that i can have a quick look whenever needed. I have compiled a list of mostly used commands and some settings and useful links. Now publishing it so that it could be useful to others too . Feel free to add more commands to the comments section.

1. Compile

 mvn compile

2. Compile (offline)

 mvn -o compile

will save you a lot of time

3. Install the generated output to the respective repository

mvn install

4. Clean Target Directory

mvn clean

5. Package as jar or war

mvn package

6. Generate Eclipse project descriptors

mvn eclipse:eclipse

7. Run unit tests

mvn test

8. Run unit tests and print the output to console rather than to file

mvn -Dsurefire.useFile=false test

9. Run individual test

mvn test –Dtest=org.shankh.mavenTest

10. Package without running tests

mvn package -Dmaven.test.skip

11. Install without Running Tests

mvn install -Dmaven.test.skip

12. To download source code into your maven repo:

mvn eclipse:clean eclipse:eclipse –DdownloadSources=true

13. Create a new project

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app

Comment : Instead of ‘generate’ i wrote ‘create’ initially, but as simonz correctly pointed out, mvn archetype:create is now deprecated. so use mvn archetype:generate instead.

14. Create a Web Application

mvn archetype:generate
        -DarchetypeGroupId=org.apache.maven.archetypes
        -DarchetypeArtifactId=maven-archetype-webapp
        -DgroupId=com.mycompany
        -DartifactId=my-app

15. Start embedded Jetty

mvn jetty:run

16. Deploys a WAR to embedded instance of Jetty and starts it

mvn jetty:run-war

17. Specify Java Build version

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
    </configuration>
  </plugin>
 </plugins>
</build>

18. Generate site for a single project

mvn site

19. Generate Javadoc

mvn javadoc:javadoc

20. Set Maven_opts System environment variable (will save you from out of memory error)

MAVEN_OPTS=”-Xmx512m -Xms256m -XX:MaxPermSize=128m”

21. Setting M2 variable in eclipse (informing of repository location

Goto Window -> Preferences -> Java -> build path -> classpath variable -> New
Name: M2_REPO
Path:
C:\Users\my_user_name\.m2\repository  (In vista)
C:\Documents And Settings\my_user_name\.m2\repository  (In XP)
~/.m2/repository (in Linus/ unix flavours)

22. Links

1. Settings.xml sample: http://maven.apache.org/maven-settings/settings.html

2. Maven Download: http://maven.apache.org/download.html

3. M2 eclipse download: http://m2eclipse.codehaus.org

4. M2 eclipse update site: http://m2eclipse.sonatype.org/update

5. Cargo: http://cargo.codehaus.org/

6. Jetty: http://jetty.mortbay.com/jetty/

  • Share/Bookmark

Some fun with Boolean.getBoolean

What will be the output of this code snippet ?

public class BooleanFun {
	public static void main(String[] args) {
		Boolean status = Boolean.getBoolean("true");
		System.out.println("First Case:"+status);
	}
}

You may think that it will obviously print First Case:true.. But it is not that straightforward. The output is following

First Case:false

Well .. How did this happen? Boolean.getBoolean property does something else than what its name denotes. According to its Javadoc
” Boolean.getBoolean Returns true if and only if the system property named by the argument exists and is equal to the string ‘true’. (Beginning with version 1.0.2 of the JavaTM platform, the test of this string is case insensitive.) A system property is accessible through getProperty, a method defined by the System class. ”

See the following code snippet to understand how it works..

public class BooleanFun {
	public static void main(String[] args) {
		Boolean status = Boolean.getBoolean("true");
		System.out.println("First Case:"+status);

         System.setProperty("bfun", "true");
		 String bfunString = System.getProperty("bfun");
		 System.out.println("System Property bfun value:"+bfunString);
		 status = Boolean.getBoolean("bfun");
		System.out.println("Second Case:"+status);

		System.setProperty("bfun2", "random");
		String bfun2String = System.getProperty("bfun2");
		System.out.println("System Property bfun2 value:"+bfun2String);
		status = Boolean.getBoolean("bfun2");
		System.out.println("Third Case:"+status);
	}
}

Output

First Case:false
System Property bfun value:true
Second Case:true
System Property bfun2 value:random
Third Case:false

So Boolean.getBoolean will check whether the System property of a given name exists and also whether its value is “true”. In only this case will it return true..

Hopefully this information will help somebody trying to figure out why their program always returns false for Boolean.getBoolean

  • Share/Bookmark