Skip to main content

Posts

Can a Servlet have a Constructor

Points To Remember Servlet is a java class that implements javax.servlet.Servlet interface. Servlets are initialized by the servlet containers using init() method . Servlets like any other java class can have constructors . Servlet must have a no argument constructor in case it have a non zero arguments constructor. Servlet needs an object of ServletConfig for initialization . Interfaces cannot have constructors. Can we have Constructors in a Servlet ? Yes we can, please read How a servlet is initialized by the servlet container.  So this article will make clear, why we cannot use a constructor for initializing a servlet. However there is no problem in having a constructor in a servlet. We can have both argumented and no arguments constructor in a servlet. Example What we are trying to do is to create a simple servlet with a constructor and assign a value to a String variable named as "msg". Servlet : ServletConstructor package com.ekiras.demo; import java.io.IOException; im...

How a Servlet is initialized by Servlet Container

Points To Remember Servlets are  initialized by the servlet containers . Servlets like any other java class  can have constructors . Servlet needs an object of ServletConfig for initialization . Interfaces cannot have constructors. Why a Servlet is initialized using init() method ? A servlet is a java class that implements javax.servlet.Servlet interface.A servlet can be given some init parameters in web.xml, called the  ServelConfig  and some global init parameters called the ServletContext. Thus they both are required at the initialization time of a servlet. Also Servlet is an interface so it cannot have a constructor, so how will we pass the ServletConfig  object to the servlet for its initialization. This is the reason the Servlet interface have a init(ServletConfig config) method and the implementing class needs to override this method to pass a ServletConfig object to it. GeneralServlet is an abstract class that implements the Servlet interface for us and ...

Difference Between HashMap and HashTable

Points To Remember HashTable have the legacy code and were introduced before HashMaps. There is nothing in a HashTable that a HashMap cannot do except multithreading. ConcurrentHashMap is a replacement for HashTable, it a synchronized HashMap. Differences : HashMap and HashTable  HashMap HashTable They implement Map Interface. It extends Dictionary(obsolete from jdk 1.7) class and implements Map interface They are a part of Collections API They are not a part of Collections API They allow a single null key and any number of null values. They do not allow null values to be added. They are not synchronized or thread-safe They are synchronized or thread-safe They use Iterator to iterate over objects They use Enumeration to iterate over objects They are faster and uses less memory They are comparatively slower. Similarities : HashMap and HashTable  Both of them implements Map interface. Both HashMap and Hashtable works on the Principle of Hashing. Both HashMap and HashTable do not...

Find Factorial of a number using Recursion

Program : Factorial Using Recursion class Factorial{ public static void main(String args[]){ int number = Integer.parseInt(args[0]); System.out.println("Factorial of " + number + " = " + factorial(number)); } public static int factorial(int n){ // Check if the number is greater than equal to 0. // because factorial of a number < 0 is undefined. if(n == 1) return 1; return n * factorial(n-1); } } Factorial of 5 = 120 Here the function factorial for number =1 will return   1 number =2 will return  2 * factorial(1)  = 2 * 1 = 2 number =3 will return   3 * factorial(2)  = 3 * 2 * factorial(1) =  3 * 2 * 1 = 6

ClassNotFoundException and NoClassDefFoundError

Points To Remember ClassNotFoundException is an Exception. NoClassDefFoundError is an Error. ClassNotFoundException This is a type of error when we are trying to access a class but that class is not available at the classpath or at the path specified by us. This type of error is thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader. The loadClass method in class ClassLoader. Example : When ClassNotFoundException occurs class A{} class Test{ public static void main(String args[]){ try{ System.out.println("classA = "+Class.forName("A").getClass()); System.out.println("classB = "+Class.forName("B").getClass()); }catch(ClassNotFoundException e){ System.out.println("ClassNotFoundException"); } } } classA = class java.lang.Class ClassNotFoundException Since the class A is found class.forName() returns the instanc...

Java MCQ's on Internal Working of HashMaps

This test contains 10 questions based on Internal Working of the HashMap . Each question answered correctly awards you 1 point and each incorrect answer has a penalty of -0.25 points, no points are deducted for unattempted answers. Select Test Type Timed Test UnTimed Test You Get +1 for each correct answer and -0.25 for each incorrect answer Time Left - minutes : seconds How are key values stored in a hashmap ? as Entry object as a LinkedList as an ArrayList None of these A All the objects in the form of a key-value pair are stored as an Entry object. What is the initial size of a HashMap when we create an object of a HashMap ? 0 8 16 None of these C When we create a HashMap like "HashMap map = new HashMap ();" , it creates an empty hashmap of initial size of 16...

What are static variables in Java ?

Points To Remember Static variable are the variables at the class level. Static variables are not attached to the objects. Static variable cannot be declared inside any method of a class. Static variables are assigned the memory only once during the execution of the class. Static variables can be called directly from static context or by class reference. Definition Static variables are the class level variable that are allocated the memory only once and they cannot be declared inside any method of a class or inside a static block or static method but can be declared only  outside all methods and inside a class. A static variable is called by using the reference of the class and not the object. Example So lets assume an easy example where we have a class Test, with two variables, " a " non-static variable and the other " b " a static variable. Then variable "a" will be specific to the objects of the class Test and variable "b" will be common for a...