Posts

Comparable vs. Comparator in java

Comparable vs. Comparator Comprable interface is used for natural ordering of any object. Comprable interface is the part of lava.lang package. Suppose you have a class named Car had have attribute name carName and engPower and want to ordering according to carName. Car class must implements Comparable interface. class Car implements Comparable{ private String carName; public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } // sort for car name public int compareTo(Object obj) { Car car = (Car)obj; /*If you want to desc order*/ //return car.carName.compareTo(this.carName); /*If you want to asc order*/ return this.carName.compareTo(car.carName); } } public class Test { public static void main(String[] args) throws IOException { Car car1 = new Car(); Car car2 = new Car(); Car car3 = new Car(); car1.setCarName("Safari"); car2.setCarName(...

override equals() and hashcode() in hashset

Why override equals() and hashcode() in hashset Ans:- By nature HashSet has unique element. When any new element is added in HashSet, HashSet check uniqueness of new element through the equals() and hashcode() method.  By Default equals() method code in Object class is public boolean equals(Object obj) { return (this == obj); } So through this code, two object can equal only if they refer to the same object. Same as , if equals() method for two object return true then hashcode() method also return same code. This is failure if two std object with same Student name . For Example : Student std1 = new Student (); std1.setLegalName("raju"); Student std2 = new Student (); std2.setLegalName("raju"); Both the objects are same but based on the default above method both objects are dirrefent because references and hashcode are different . So You will found duplicate variable in HashSet. For resolve this problem, HashSet ...

is actionservlet thread safe

ActionServlet is controller in Model View Controller design pattern for web applications. ActionServlet is not thread safe There is only a method named getRequestProcessor is synchronized. Code of this method is below :- /** * <p>Look up and return the {@link RequestProcessor} responsible for the * specified module, creating a new one if necessary.</p> * * @param config The module configuration for which to * acquire and return a RequestProcessor. * * @exception ServletException if we cannot instantiate a RequestProcessor * instance * @since Struts 1.1 */ protected synchronized RequestProcessor getRequestProcessor(ModuleConfig config) throws ServletException { // :FIXME: Document UnavailableException? RequestProcessor processor = this.getProcessorForModule(config); if (processor == null) { try { processor = (RequestProcessor) RequestUtils.applicationInstance( config.getControllerConfig().getProcessorClass()); } catch (Exception e) { throw new UnavailableException...

Difference NoClassDefFoundException and ClassNotFoundException

NoClassDefFoundException occurs when class is loaded as statically and ClassNotFoundException occurs when class is loaded as dynamically. Static class loading:- Car c = new TestClass(); Dynamically class loading:- Class.forName(“ TestClass ”); According to arun:- Consider, you have two custom classloaders - CL1 and CL2. A class loaded by CL1 when tried to be accessed by CL2 will also throw a ClassNotFoundException. And consider you have Class1 loaded by both CL1 and CL2, they both are not compatible even through they are the same. There we give some job to ClassCastException. Thanks Arun www.arunma.com

convert Object Array to String in java examples

How to convert Object Array to String in java examples public static String arrayToString(Object[] a, String separator) {     String result = "";     if (a.length > 0) {         result = a[0].toString();    // start with the first element         for (int i=1; i             result = result + separator + a[i];         }     }     return result; }

write pdf file using java with itext

Make a pdf file through itext jar in java write pdf file using java with itext import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; /**   * @Manoj Shakya  */ public class Test {     public static void main(String [] aa){         try{         Document document = new Document(PageSize.A4, 10, 10, 10, 10);         PdfWriter.getInstance(document, new FileOutputStream("C:\\test.pdf"));         document.open();         PdfPTable bodytable = new PdfPTable(6);                 for(int i=0; i<5; i++){             bodytable...

Interceptor in struts2 java

Interceptor Definition:-                      In struts2, An Interceptor is an interface under the com.opensymphony.xwork2.interceptor package. An Interceptor extends the Serializable Interface. An interceptor is a stateless class that follows the interceptor pattern.     Interceptor give the choice to write code that can be invoke before and after the execution of an action. Interceptor also have the facility to stop an action from executing. Interceptors provide developers a way to encapulate common functionality in a re-usable form that can be applied to one or more Actions.

Recent Post

Recent Posts Widget