Showing posts with label tricks. Show all posts
Showing posts with label tricks. Show all posts

09 September 2013

Start tomcat using javaw

To start tomcat using javaw instead of java - to avoid opening cmd window in M$ windows ; edit the following file:

%CATALINA_HOME% in\setclasspath.bat

And replace:
set _RUNJAVA="%JRE_HOME% in\java"

by:
set _RUNJAVA="%JRE_HOME% in\javaw"

Now you can put in your windows startup folder (from start-menu) to autostart.

22 February 2011

Java: Please make best use of String.equals and equalsIgnoreCase

Al salamo Alykom,

May developers don't make the best use of java.lang.String.equals and java.lang.String.equalsIgnoreCase

Take this example:
public static CreditCart getCreditCard(String creditCardId)
{
    if (creditCardId != null && creditCardId.equalsIgnoreCase("vi"))
        return new Visa();
    else if (creditCardId != null && creditCardId.equalsIgnoreCase("mc"))
        return new MasterCard();
    else
        return new NullCard();
}

But the best use of equals and equalsIgnoreCase will make your code much compact, watch out:

public static CreditCart getCreditCard(String creditCardId)
{
    if ("vi".equalsIgnoreCase(creditCardId))
        return new Visa();
    else if ("mc".equalsIgnoreCase(creditCardId))
        return new MasterCard();
    else
        return new NullCard();
}

this is because, equalsIgnoreCase and equals returns (from Javadoc):
true if the argument is not null and it represents an equivalent String ignoring case; false otherwise

So, if the argument (in our case `creditCardId` is null, no NullPointerException will be thrown, but simple the expression will be evaluated to false!)