Q1) What is an immutable class?
Ans) Immutable class is a class which once created, it’s contents can not be changed. Immutable objects are the objects whose state can not be changed once constructed. e.g. String class
|
Q2) How to create an immutable class?
Ans) To create an immutable class following steps should be followed:
E.g.
public final class FinalPersonClass {
private final String name;
private final int age; public FinalPersonClass(final String name, final int age) { super(); this.name = name; this.age = age; } public int getAge() { return age; } public String getName() { return name; } } |
Q3) Immutable objects are automatically thread-safe –true/false?
Ans) True. Since the state of the immutable objects can not be changed once they are created they are automatically synchronized/thread-safe.
|
Q4) Which classes in java are immutable?
Ans) All wrapper classes in java.lang are immutable –
String, Integer, Boolean, Character, Byte, Short, Long, Float, Double, BigDecimal, BigInteger |
Q5) What are the advantages of immutability?
Ans) The advantages are:
1) Immutable objects are automatically thread-safe, the overhead caused due to use of synchronisation is avoided. 2) Once created the state of the immutable object can not be changed so there is no possibility of them getting into an inconsistent state. 3) The references to the immutable objects can be easily shared or cached without having to copy or clone them as there state can not be changed ever after construction. 4) The best use of the immutable objects is as the keys of a map. |
Tuesday, 23 September 2014
Write a JAVA program to implement a Queue using user defined Exception Handling (also make use of throw, throws)
import java.io.*;
class myException extends Exception
{ myException()
{ System.out.println("Error:Password too short");
}
myException(int n)
{ System.out.println("Error:Only adults can join");
}
}
class user_exception
{ public static void main(String s[])throws IOException,myException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{ System.out.print("Enter user name : ");
String n=br.readLine();
System.out.print("Enter your password : ");
String m=br.readLine();
if(m.length() <6)
throw new myException();
System.out.print("Enter your age : ");
int o=Integer.parseInt(br.readLine());
if(o<18)
throw new myException(o);
}
catch(Exception e)
{ }
}
}
output:
class myException extends Exception
{ myException()
{ System.out.println("Error:Password too short");
}
myException(int n)
{ System.out.println("Error:Only adults can join");
}
}
class user_exception
{ public static void main(String s[])throws IOException,myException
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{ System.out.print("Enter user name : ");
String n=br.readLine();
System.out.print("Enter your password : ");
String m=br.readLine();
if(m.length() <6)
throw new myException();
System.out.print("Enter your age : ");
int o=Integer.parseInt(br.readLine());
if(o<18)
throw new myException(o);
}
catch(Exception e)
{ }
}
}
output:
Friday, 12 September 2014
Diffs
what is difference between servlet and cgi?
CGI :
CGI :
- Written in C, C++, Visual Basic and Perl
- Difficult to maintain, non-scalable, non-manageable
- Prone to security problems of programming language
- Resource intensive and inefficient
- Platform and application-specific
- Every time a new process begins each time a request is made to CGI program.
- A CGI program (and probably also an extensive runtime system or interpreter) needs to be loaded and started for each CGI request.
- CGI handles separate request by separate instances.
- Written in Java
- Powerful, reliable, and efficient
- Improves scalability, reusability (component based)
- Leverages built-in security of Java programming language
- Platform independent and portable
- A Servlet does not run in a separate process. This removes the overhead of creating a new process for each request.
- A Servlet stays in memory between requests.
- There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Subscribe to:
Posts (Atom)