Professional Documents
Culture Documents
is there
anything else?
What is serialVersionUID and why is it important? Please show an example
where missing serialVersionUID will cause a problem.
The serialization runtime associates with each serializable class a version number,
called a serialVersionUID, which is used during deserialization to verify that the sender
and receiver of a serialized object have loaded classes for that object that are
compatible with respect to serialization. If the receiver has loaded a class for the object
that has a different serialVersionUID than that of the corresponding sender's class, then
deserialization will result in anInvalidClassException. A serializable class can declare its
own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must
be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization
runtime will calculate a default serialVersionUID value for that class based on various
aspects of the class, as described in the Java(TM) Object Serialization Specification.
However, it is strongly recommended that all serializable classes explicitly declare
serialVersionUID values, since the default serialVersionUID computation is highly
sensitive to class details that may vary depending on compiler implementations, and can
thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to
guarantee a consistent serialVersionUID value across different java compiler
implementations, a serializable class must declare an explicit serialVersionUID value. It
is also strongly advised that explicit serialVersionUID declarations use the private
modifier where possible, since such declarations apply only to the immediately declaring
class--serialVersionUID fields are not useful as inherited members.
What is the use of the finally block? Is finally block in Java guaranteed to
be called? When finally block is NOT called?
The finally block always executes when the try block exits. This ensures that the
finally block is executed even if an unexpected exception occurs. But finally is
useful for more than just exception handling it allows having cleanup code
accidentally bypassed by a return, continue, or break. Putting cleanup code in a
finally block is always a good practice, even when no exceptions are anticipated.
If the JVM exits while the try or catch code is being executed, then the finally
block may not execute. Likewise, if the thread executing the try or catch code is
interrupted or killed, the finally block may not execute even though the application
as a whole continues.
Why there are two Date classes; one in java.util package and another in
java.sql?
A java.util.Date represents date and time of day, a java.sql.Date only represents a date. The
complement of java.sql.Date is java.sql.Time, which only represents a time of day.
The java.sql.Date is a subclass (an extension) of java.util.Date. So, what changed in
java.sql.Date:
-- toString() generates a different string representation: yyyy-mm-dd
-- a static valueOf(String) methods to create a Date from a String with above representation
-- the getters and setter for hours, minutes and seconds are deprecated
The java.sql.Date class is used with JDBC and it was intended to not have a time part, that
is, hours, minutes, seconds, and milliseconds should be zero but this is not enforced by
the class.
Question : What will happen if you put return statement or System.exit () on try
or catch block ? Will finally block execute?
This is a very popular tricky Java question and its tricky because many programmer
think that no matter what, but finally block will always execute. This question
challenge that misconcept by putting return statement in try or catch block or
calling System.exit from try or catch block. Answer of this tricky question in Java
is that finally block will execute even if you put returnstatement in try block or
catch block but finally block won't run if you call System.exit form try or catch.
class CricketTeam{
String name; //this is the complete name(inclusive of first and last name)
}
1>
2>
3>
4>
5>
6>
Sachin Tendulkar
Gautam Gambhir
Ricky Ponting
Shahid Afridi
Kevin Pieterson
MS Dhoni
I want to sort the above Cricket Players name by their last name
only.Suugestions/code provided would be appreciated.
2> what are the advantages of using enhanced for loop against iterator in
java.what are the advantages of using enhanced for loop in java and why was it
introduced in java at the first place when the iterator could do the job.?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
Implement a Comparator which parses the last name out of player names and
compares these, then sort the collection of cricket players using that comparator.
A simplistic example (without error handling, and assuming all players have exactly
one first name and no middle name):
class CricketTeamComparator implements Comparator<CricketTeam> {
@Override
public int compare(CricketTeam o1, CricketTeam o2) {
String lastName1 = o1.name.split(" ")[1];
String lastName2 = o2.name.split(" ")[1];
return lastName1.compareTo(lastName2);
}
}
...
List<CricketTeam> team = new ArrayList<CricketTeam>();
...
Collections.sort(team, new CricketTeamComparator());
it is cleaner, more concise and safer (e.g. avoids specific subtle bugs in multiple
embedded loops, when the iterator is accidentally incremented too often by
calling next() too many times).
Code sample (from Effective Java 2nd Edition, Item 46: Prefer for-each loops to
traditional for loops):
// Can you spot the bug?
enum Suit { CLUB, DIAMOND, HEART, SPADE }
enum Rank { ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
19.
20.
21.
22.
23.
24.
25.
List<CricketTeam> team;
Collections.sort(team, new Comparator<CricketTeam>) {
int compare(CricketTeam o1, CricketTeam o2) {
return o1.name.split(" ")[1].compareTo(o2.name.split(" ")[1])
}
}
Precompilation and DB-side caching of the SQL statement leads to overall faster
execution and the ability to reuse the same SQL statement in batches.
The Prepared Statement is a slightly more powerful version of a Statement, and should
always be at least as quick and easy to handle as a Statement.
The Prepared Statement may be parametrized
Most relational databases handles a JDBC / SQL query in four steps:
1.Parse the incoming SQL query
2. Compile the SQL query
3. Plan/optimize the data acquisition path
4. Execute the optimized query / acquire and return data
A Statement will always proceed through the four steps above for each SQL query sent
to the database. A Prepared Statement pre-executes steps (1) - (3) in the execution
process above. Thus, when creating a Prepared Statement some pre-optimization is
performed immediately. The effect is to lessen the load on the database engine at
execution time.
Some of the benefits of PreparedStatement over Statement are:
1.
2.
3.
4.
5.
finally - handles exception. The finally block is optional and provides a mechanism to clean up
regardless of what happens within the try block (except System.exit(0) call). Use the finally block
to close files or to release other system resources like database connections, statements etc.
finalize() - method belongs to Object class. The method that is invoked while doing the garbage
collection of the object. It could be used for allowing it to clean up its state. Good use cases will
be to free connection pools , deallocate resources etc.
What was the most exciting project for you in your 10 years of
career? What were your responsibilities in that project?