Posts

Showing posts from April, 2011

Static Gotcha in java

class PreTesting {     static void doSomething()     {         System.out.println("Method in parent class");     } } public class Testing extends PreTesting {     static void doSomething()     {             System.out.println("Method in child  class");     }     public static void main(String args[]) throws Exception {       PreTesting preTesting =  new Testing();       preTesting.doSomething();   } } //outout is : Method in parent class

String Reverse in java

import org.apache.commons.lang.StringUtils; public class Testing {   public static void main(String[] args) { String words = "I am walking"; System.out.println("Letter Reverse : " + StringUtils.reverse(words)); System.out.println("Words Reverse " + StringUtils.reverseDelimited(words, ' ')); } }

Converting Strings to int and int to String

//Integer to String String a = String.valueOf(2);   //String to integer int i = Integer.parseInt(a); n

update the class in the JAR

jar uf c:\ CodeReview .jar com\pmd\rules\ NameRule .class

array to list conversion

List list = Arrays.asList(array);

Increase Heap Size in Java

Tomcat on Windows (started manually) If you run Tomcat (eg. from JIRA Standalone) on Windows, and are starting it manually by running bin\startup.bat, edit bin\setenv.bat and add the line: setenv.bat set JAVA_OPTS =-Xms256m -Xmx256m and then restart. Adjust 256 to the maximum memory you want to allocate. If bin\setenv.bat does not exist, create it.

Stopwatch in java

import org.apache.commons.lang.time. StopWatch ; StopWatch clock = new StopWatch (); clock.start(); for( int i = 0; i < 100000000; i++ ) { Math.sin( 0.34 ); } clock.stop(); System.out.println( "It took" + clock.getTime() + " milliseconds" );

Java programming best practices

Following are some performance tips in Java. We know many of them still do not use them in regular practice. Compile-Time Initialization for (int i =0; i < loop; i++) { //Looping 10M rounds String x = "Hello" + "," +" "+ "World"; } Better way is.... for (int i =0; i < loop; i++) { //Looping 10M rounds String x = new String("Hello" + "," +" "+ "World"); } ------------------------------------------------------------------------------------------------------------------- Runtime String Initialization String name = "Smith"; for (int i =0; i < loop; i++) { //Looping 1M rounds String x = "Hello"; x += ","; x += " Mr."; x += name; } Better way is.... String name = "Smith"; for (int i =0; i < loop; i++) { //Looping 1M rounds String x = (new StringBuffer()).append("Hello").append(",").append(" ").append(name).toString(