Does it matter in what order catch statements for FileNotFoundException and IOException are written?
Yes, it does. The child exceptions classes must always be caught first and the "Exception" class should be caught last
How do you serialize an object to a file?
Imagine you want to save the state of one or more objects. If Java didn't have serialization, you'd have to use one of the I/O classes to write out the state of the instance variables of all the objects you want to save. The worst part would be trying to reconstruct new objects that were virtually identical to the objects you were trying to save. You'd need your own protocol for the way in which you wrote and restored the state of each object, or you could end up setting variables with the wrong values. For example, imagine you stored an object that has instance variables for height and weight. At the time you save the state of the object, you could write out the height and weight as two ints in a file, but the order in which you write them is crucial. It would be all too easy to re-create the object but mix up the height and weight values-using the saved height as the value for the new object's weight and vice versa.
The purpose of Serialization is to help us achieve whatever complicated scenario we just witnessed in an easier manner.
Serializing Objects
What does it really mean to save an object? If the instance variables are all primitive types, it's pretty straightforward. But what if the instance variables are themselves references to objects? What gets saved? Clearly in Java it wouldn't make any sense to save the actual value of a reference variable, because the value of a Java reference has meaning only within the context of a single instance of a JVM. In other words, if you tried to restore the object in another instance of the JVM, even running on the same computer on which the object was originally serialized, the reference would be useless.
But what about the object that the reference refers to? Look at this class:
class Car {
private Engine theEngine;
private int CarSize;
public Car(Engine Engine, int size) {
theEngine = Engine;
CarSize = size;
}
public Engine getEngine() { return theEngine; }
}
class Engine {
private int EngineSize;
public Engine(int size) { EngineSize = size; }
public int getEngineSize() { return EngineSize; }
}
Now make a Car... First, you make a Engine for the Car:
Engine c = new Engine(3);
Then make a new Car, passing it the Engine:
Car d = new Car(c, 8);
Now what happens if you save the Car? If the goal is to save and then restore a Car, and the restored Car is an exact duplicate of the Car that was saved, then the Car needs a Engine that is an exact duplicate of the Car's Engine at the time the Car was saved. That means both the Car and the Engine should be saved.
And what if the Engine itself had references to other objects-like perhaps a Color object? This gets quite complicated very quickly. If it were up to the programmer to know the internal structure of each object the Car referred to, so that the programmer could be sure to save all the state of all those objects. That would be a nightmare with even the simplest of objects.
Fortunately, the Java serialization mechanism takes care of all of this. When you serialize an object, Java serialization takes care of saving that object's entire "object graph." That means a deep copy of everything the saved object needs to be restored. For example, if you serialize a Car object, the Engine will be serialized automatically. And if the Engine class contained a reference to another object, THAT object would also be serialized, and so on. And the only object you have to worry about saving and restoring is the Car. The other objects required to fully reconstruct that Car are saved (and restored) automatically through serialization.
Remember, you do have to make a conscious choice to create objects that are serializable, by implementing the Serializable interface. If we want to save Car objects, for example, we'll have to modify the Car class as follows:
class Car implements Serializable {
// the rest of the code as before
// Serializable has no methods to implement
}
And now we can save the Car with the following code:
import java.io.*;
public class SerializeCar {
public static void main(String[] args) {
Engine c = new Engine(3);
Car d = new Car(c, 8);
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
} Catch (Exception e) { e.printStackTrace(); }
}
}
But when we run this code we get a runtime exception something like this
java.io.NotSerializableException: Engine
What did we forget? The Engine class must ALSO be Serializable. If we modify the Engine class and make it serializable, then there's no problem:
class Engine implements Serializable {
// same
}
Here's the complete code:
import java.io.*;
public class SerializeCar {
public static void main(String[] args) {
Engine c = new Engine(3);
Car d = new Car(c, 5);
System.out.println("before: Engine size is "
+ d.getEngine().getEngineSize());
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(d);
os.close();
} Catch (Exception e) { e.printStackTrace(); }
try {
FileInputStream fis = new FileInputStream("testSer.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
d = (Car) ois.readObject();
ois.close();
} Catch (Exception e) { e.printStackTrace(); }
System.out.println("after: Engine size is "
+ d.getEngine().getEngineSize());
}
}
class Car implements Serializable {
private Engine theEngine;
private int CarSize;
public Car(Engine Engine, int size) {
theEngine = Engine;
CarSize = size;
}
public Engine getEngine() { return theEngine; }
}
class Engine implements Serializable {
private int EngineSize;
public Engine(int size) { EngineSize = size; }
public int getEngineSize() { return EngineSize; }
}
This produces the output:
before: Engine size is 3
after: Engine size is 3
Here's a code snippet that solves that problem.
if((total%5==0) (total%4==0)) {
count += 1;
} else {
count += 2;
}
What is the advantage of hibernate in j2ee?
Hibernate is capable of mapping all Object Oriented relationships (such as inheritance,interfaces, associations, composition, ternary associations) between objects. Hibernate follows both Bottom-up (existing database to OO model) and Top-down (OOModel to design the database schema) approaches: asanthine s
its easy just extract if its jar or else use j2me to crack the activation using net beans ide or eclipse
What is the fundamental difference between while loops and numeric for loops?
There is no such difference, for and while loops are convertible:
in: for (exp1; exp2; exp3) stmt;
out: { exp1; while (exp2) { stmt; exp3; }}
in: while (exp) stmt;
out: for (; exp; ) stmt;
How do you use Java Native Interface to access Delphi code from Java classes?
The Delphi code would need to be compiled into a DLL, and the DLL is then called from java using the JNI. See http://home.pacifier.com/~mmead/jni/delphi/JavaToDPR/ to get started.
What is the simularities of parameter and arguments?
Arguments are fields that are given to a method when it is called, while parameters are the name for the received variables. For example:
public static void main(String[] args){
int arg1 = 0;
boolean arg2 = false;
String arg3 = "Some string";
function_a(arg1, arg2, arg3);
// These variables are called arguments because they are being passed
// to the function.
}
public static void function_a(int par1, boolean par2, String par3){
// These variables are called parameters because they are being received
// when the function is called.
}
I hope this helps.
Objects are constructed. You can't make a new object without invoking a constructor. In fact, you can't make a new object without invoking not just the constructor of the object's actual class type, but also the constructor of each of its superclasses including the Object class itself! Constructors are the code that runs whenever you use the keyword new. The constructor typically contains he initialization code that you want to run when someone is instantiating an object of a class that you are coding.
Do void methods have parameters?
A void method is just like any other method; it may or may not have parameters.
Can you assigned value of int to a ordinary pointer?
Yes, with type-cast (but I don't see why you should):
char *ptr = (char *)300;
If you have two public classes in a java file what will be the error and how will you remove it?
Hmm, let me see, it is technically perfectly legal to have 2 classes in a single file, as long as one class is an inner class of the other: i.e. public class Stevo{ public class Fun{}}. But if you want two separate classes independent of each other the compiler would scream at you about class is public and should be declare in a file named [className].java. To fix this simply follow the compiler's instructions (shocking! the compiler actually was clear about something) and create a new file with the given name. But sometimes an inner class would be helpful, i.e. an iterator for the Abstract Data Type you are implementing.
How can you convert words to numbers?
Previous answer No is a cop-out in my opinion.
Provided words are of strict format e.g. All numbers are separated by a space, and only ZERO to NINE in words is supported, a sentence can be split into an array then the following performed for each digit.
CASE : ZERO THEN 0
CASE : ONE THEN 1
CASE : TWO THEN 2
CASE : THREE THEN 3
CASE : FOUR THEN 4
CASE : FIVE THEN 5
CASE : SIX THEN 6
CASE : SEVEN THEN 7
CASE: EIGHT THEN 8
CASE : NINE THEN 9
A new string is built from the array digits of digits
E.q. "FIVE FIVE" becomes "55"
Of course the algorithm could be expanded to cope with "TEN", "TWENTY" and so on but I wish the coder luck.
As I have added such a good answer it is only right I should be permitted a link to my excellent music website at http://www.ejay-international.com
What are the keywords in Java?
Here's a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
abstract
assert***
boolean
break
byte
case
catch
char
class
const*
continue
default
do
double
else
enum****
extends
final
finally
float
for
goto*
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp**
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
* not used
** added in 1.2
*** added in 1.4
**** added in 5.0
Algorithm to arrange three numbers in ascending order using else if ladder?
step 1:start
step 2:input the values of a,b,c
step 3:if(a>b&&a>c) max=a
step 4:else if(b>c) max=b
step 5:else max=c
step 6:output:max
step 7:stop
How do you write the semicolon symbol in a pseudocode logic program delaing with classes?
Any of these:
PRINT semicolon
EMIT ;
WRITE ";"
etc.
An ActionListener is exactly what it sounds like. It's an interface used by other classes to listen for an action event. The simplest example of this is on a Button. Normally when you add a Button to a Component nothing will happen when you press it. You need to use a button.addActionListener(actionListener) call to make it listen for button clicks.
How do you clear a StringBuffer?
Set length of buffer to zero to clear a StringBufferinstance.
Example:
StringBuffer buf = new StringBuffer();
// ... use buffer
buf.setLength(0);
// buffer now has zero length
GUI stands for Graphical User Interface
This refers to the front end screens using which we access a system. For example if you logon to your online banking account, that website can be referred to as the GUI. It is the interface using which you are accessing the bank website.