Java Search Engine

freerice.com

Friday, March 28, 2008

Difference between String, StringBuffer, StringBuilder


String: String is immutable. You can't change a string object once after you created it. The methods in String class which appear to be changing string object are actually return a new object.

StringBuffer: StringBuffer is mutable. You can change it without creating new object. Use it when you have to modify your string a lot.

StringBuilder: StringBuilder is introduced in Java SE 5.0. It is same as StringBuffer in functionality. But it doesn't have its methods synchronized, whereas StringBuffer has synchronized methods. When you don't need thread safety for your strings use StringBuilder, it will be little fast as you don't need to get locks unnecessarily. Most of the time you don't need synchronized methods.

Thursday, February 7, 2008

Specs work in each Tomcat version

Different versions of Apache Tomcat are available for different versions of the Servlet and JSP specifications. The mapping between the specifications and the respective Apache Tomcat verisons is:

Servlet/JSP Spec Apache Tomcat version
2.5/2.1 6.0.x
2.4/2.0 5.5.x
2.3/1.2 4.1.x
2.2/1.1 3.3.x

Thursday, January 17, 2008

Loading data from a file into a table

How to import data from a file to a table

in Oracle
You have to use sqlldr utility to import data from an external file into a table, you can't do from sql plus.

First you have to create a control file, a data file. Control file should be like this

LOAD DATA
INFILE data file name
APPEND INTO TABLE table name
FIELDS TERMINATED BY 'separator'
(columns list)

Separator is the character which separates the column values in the data file.
Ex:
LOAD DATA
INFILE 'C:\data.txt'
APPEND INTO TABLE ac_mt
FIELDS TERMINATED BY ','
(acname, mtname)

Next call the sqlldr from command prompt and give it control file.

Syntax: sqlldr control='control file name' log='log file name' bad='bad file name'
Any rows which are not imported will be placed into bad file. Details will be logged into the log file.

Read more here.


in MySQL
you can give the same command directly or you can use mysqlimport utility.

Read more here.

Anonymous arrays in Java

Anonymous arrays are similar to anonymous classes in Java. Both don't have a name, so called anonymous. If you need to create an array to use only once, then you can create an anoymous array without assigning it to any variable.

Syntax: new int[] ( 15, 32, 20)

Just define the array needed followed by the keyword new. No need to specify a size, as you are assigning values to the array immediately.

For example, you need to pass an array to a function, and you don't use it anywhere else then you can do as following.

Ex: void fun( grade, new String[] ("Pass", "Fail") )

Friday, December 7, 2007

Interview - 2

1. What is the output of the following program?

int b=’A’;

System.out.println(b);


A) 65 B) A C) Compile Time Error D) Runtime Error


2. What is the output of the following program?

String a=new String("Reddy");

StringBuffer b=new StringBuffer("Reddy");

System.out.println(a.equals(b));



A) true B) false C) Compile Time Error D) Runtime Error


3. What is the output of the following program?

String a="Reddy";

a.toUpperCase();

System.out.println(a);

A) REDDY B) Reddy C) Compile Time Error D)Runtime Error


4. Which of the following declarations are wrong?

i. final public class test{}

ii. abstract public class test{}

iii. protected abstract class test{}

iv. public static interface test{}

v. final static interface test{}

A) iii & iv B) iv & v C) i & ii & v D) iii & v


Sunday, November 25, 2007

Interview - 1

What is the super class of Exception?

Throwable

What is the main controller component in Struts framework?

ActionServlet

What is a marker Interface?

Marker Interface is an empty interface which is used to indicate something to the compiler.  For example if you are implementing 'Serializable' marker interface you are indicating that the object can be converted into bytes.

Can you call doPost method from doGet ?

yes


Saturday, November 24, 2007

What is the difference between final, finally and finalize?

final:  final is a keyword used to create constants and non-inheritable classes.  If final is used with a variable, some value should be assigned to that variable, and that value can't be changed again.  If final is used for classes, you can't extend those classes.

finally:  finally is used with try...catch block.  finally is a block of statements which executes nevertheless the exception occurred or not.  If exception occurs execution will come to catch block, executes the necessary statements then comes to finally block.  If exception doesn't occur then try block will be executed fully, then execution comes to finally block.  This finally block was commonly used to cleanup any resources held.

finalize:  finalize is a method which will be called before the object is garbage collected.  Garbage collector will call this method once, before it reclaims the memory of the object.  So, any cleanup code will be put in the finalize block.

Ratings by outbrain