jump to navigation

Performance of string August 20, 2006

Posted by Allu in JAVA, Uncategorized.
add a comment

+ operator gives best performance for String concatenation if Strings resolve at compile time

StringBuffer with proper initial size gives best performance for String concatenation if Strings resolve at run time.

Advertisements

String Buffer is Thread Safe August 20, 2006

Posted by Allu in JAVA.
add a comment

java.lang.StringBuffer

  If you are using StringBuffer in a non-multithreaded environment (or you at least know that nobody will be modifying the StringBuffer at the same time you are, which is essentially a single-threaded environment) you will want to write your own StringBuffer (I use one called SuperStringBuffer) that does not synchronize it’s methods. Calling a synchronized method is about 4x slower than calling one that’s not synchronized).

  If you’re doing something like this:

StringBuffer b = new StringBuffer();
b.append(foo);
b.append(bar);
b.append(baz);

it’s considerably faster to do it like this:

StringBuffer b = new StringBuffer();
synchronized (b)
{
  b.append(foo);
  b.append(bar);
  b.append(baz);
}

because the former has to obtain three object locks, where as the latter only has to obtain one.

Why String class is immutable? August 16, 2006

Posted by Allu in JAVA.
3 comments

JVM internally maintains the “String Pool”. To achive the memory
efficiency, JVM will refer the String object from pool. It will not
create the new String objects. So, whenevr you create a new string
literal, JVM will check in the pool whether it already exists or not.
If already present in the pool, just give the reference to the same
object or create the new object in the pool. There will be many
references point to the same String objects, if someone changes the
value, it will affect all the references. So, sun decided to make it
immutable.

Forward Vs Redirect August 12, 2006

Posted by Allu in J2EE.
9 comments

A Controller servlet may perform either a forward or a redirect operation at the end of processing a request.

Forward

  • forward is performed internally by the servlet
  • the browser is completely unaware that it has taken place, so its original URL remains intact
  • any browser reload will simple repeat the original request, with the original URL

Redirect

  • redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
  • a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
  • redirect is always slower than a forward, since it requires a second browser request
  • beans placed in the original request scope are not available to the second request

Forward should be used if the operation can be safely repeated upon reload; otherwise, redirect must be used. Typically, if the operation performs an edit on the datastore, then a redirect, not a forward, is required.

Diff between Thread and Runnable August 12, 2006

Posted by Allu in JAVA.
301 comments

To implement threads, you can use both Thread class or a class that implements Runnable interface as follows :

class MyThread extends Thread {

    public void run() {

       

    }

}

or

class MyRunnable implements Runnable {

    public void run() {

       

    }

}

MyRunnable r = new MyRunnable() ;

Thread t = new Thread(r);

In an Object oriented aspect, the second method is more preferrable, because you extend a class when you want to make it different from it’s superclass, and change it’s behaviour(s) . But when you extend Thread class for use as a thread generally you do not make a change on thread object’s behaviours. So you want to extend Thread when you really change the class Thread. And if you extend Thread class, you cannot extend another class, so using Runnable interface gives you the ability of extending another class. And (as far as I know) these two methods cause no performance difference when you prefer one for another.

Checked Vs Unchecked Exceptions August 12, 2006

Posted by Allu in JAVA.
1 comment so far

Checked exceptions:

  • represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
  • are subclasses of Exception
  • methods are obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)

Unchecked exceptions:

  • represent defects in the program (often invalid arguments passed to a non-private method)
  • are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException
  • methods are not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)

It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).