Describe What Happens When An Object Is Created in Java
Describe What Happens When An Object Is Created in Java
Describe What Happens When An Object Is Created in Java
Several things happen in a particular order to ensure the object is constructed properly:
1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the object and its
superclasses. Implementation-specific data includes pointers to class and method data.
2. The instance variables of the objects are initialized to their default values.
3. The constructor for the most derived class is invoked. The first thing a constructor does is call the constructor for its uppercase.
This process continues until the constructor for java.lang.Object is called, as java.lang.Object is the base class for all objects in
java.
4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the
body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived
class completes last.
In Java, you can create a String object as below : String str = "abc"; & String str = new String("abc"); Why can’t a button
object be created as : Button bt = "abc"? Why is it compulsory to create a button object as: Button bt = new Button
("abc"); Why this is not compulsory in String’s case?
Button bt1= "abc"; It is because "abc" is a literal string (something slightly different than a String object, by-the-way) and bt1 is a
Button object. That simple. The only object in Java that can be assigned a literal String is java.lang.String. Important to not that
you are NOT calling a java.lang.String constructor when you type String s = "abc"; For example String x = "abc"; String y =
"abc"; refer to the same object. While String x1 = new String("abc");
String x2 = new String("abc"); refer to two different objects.
interface CanFight {
void fight();
interface CanSwim {
void swim();
interface CanFly {
void fly();
class ActionCharacter {
public void fight() {}
class Hero extends ActionCharacter implements CanFight, CanSwim, CanFly {
public void swim() {}
public void fly() {}
}
You can even achieve a form of multiple inheritance where you can use the *functionality* of classes rather than just the interface:
interface A {
void methodA();
}
class AImpl implements A {
void methodA() { //do stuff }
}
interface B {
void methodB();
}
class BImpl implements B {
void methodB() { //do stuff }
}
class Multiple implements A, B {
private A a = new A();
private B b = new B();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}
This completely solves the traditional problems of multiple inheritance in C++ where name clashes occur between multiple base
classes. The coder of the derived class will have to explicitly resolve any clashes. Don’t you hate people who point out minor typos?
Everything in the previous example is correct, except you need to instantiate an AImpl and BImpl. So class Multiple would look
like this:
class Multiple implements A, B {
private A a = new AImpl();
private B b = new BImpl();
void methodA() { a.methodA(); }
void methodB() { b.methodB(); }
}
Java says "write once, run anywhere". What are some ways this isn’t quite true?
As long as all implementations of java are certified by sun as 100% pure java this promise of "Write once, Run everywhere" will
hold true. But as soon as various java core implementations start digressing from each other, this won’t be true anymore. A recent
example of a questionable business tactic is the surreptitious behavior and interface modification of some of Java’s core classes in
their own implementation of Java. Programmers who do not recognize these undocumented changes can build their applications
expecting them to run anywhere that Java can be found, only to discover that their code works only on Microsoft’s own Virtual
Machine, which is only available on Microsoft’s own operating systems.
What is the difference between a Vector and an Array. Discuss the advantages and disadvantages of both?
Vector can contain objects of different types whereas array can contain objects only of a single type.
- Vector can expand at run-time, while array length is fixed.
- Vector methods are synchronized while Array methods are not
What is RMI?
RMI stands for Remote Method Invocation. Traditional approaches to executing code on other machines across a network have
been confusing as well as tedious and error-prone to implement. The nicest way to think about this problem is that some object
happens to live on another machine, and that you can send a message to the remote object and get a result as if the object lived on
your local machine. This simplification is exactly what Java Remote Method Invocation (RMI) allows you to do. Above excerpt is
from "Thinking in java". For more information refer to any book on Java.
What does the keyword "synchronize" mean in java. When do you use it? What are the disadvantages of synchronization?
Synchronize is used when you want to make your methods thread safe. The disadvantage of synchronize is it will end up in
slowing down the program. Also if not handled properly it will end up in dead lock.
What gives java it’s "write once and run anywhere" nature?
Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is
not platorm specific and hence can be fed to any platform. After being fed to the JVM, which is specific to a particular operating
system, the code platform specific machine code is generated thus making java platform independent.
What are native methods? How do you use them?
Native methods are methods written in other languages like C, C++, or even assembly language. You can call native methods from
Java using JNI. Native methods are used when the implementation of a particular method is present in language other than Java
say C, C++. To use the native methods in java we use the keyword native
public native method_a(). This native keyword is signal to the java compiler that the implementation of this method is in a
language other than java. Native methods are used when we realize that it would take up a lot of rework to write that piece of
already existing code in other language to Java.
What is JDBC? Describe the steps needed to execute a SQL query using JDBC.
We can connect to databases from java using JDBC. It stands for Java DataBase Connectivity.
Here are the steps:
1. Register the jdbc driver with the driver manager
2. Establish jdbc connection
3. Execute an sql statement
4. Process the results
5. Close the connection
Before doing these do import java.sql.*
JDBC is java based API for accessing data from the relational databases. JDBC provides a set of classes and interfaces for doing
various database operations. The steps are:
Register/load the jdbc driver with the driver manager.
Establish the connection thru DriverManager.getConnection();
Fire a SQL thru conn.executeStatement();
Fetch the results in a result set
Process the results
Close statement/result set and connection object.
How many different types of JDBC drivers are present? Discuss them.
There are four JDBC driver types.
Type 1: JDBC-ODBC Bridge plus ODBC Driver:
The first type of JDBC driver is the JDBC-ODBC Bridge. It is a driver that provides JDBC access to databases through ODBC
drivers. The ODBC driver must be configured on the client for the bridge to work. This driver type is commonly used for
prototyping or when there is no JDBC driver available for a particular DBMS.
Type 2: Native-API partly-Java Driver:
The Native to API driver converts JDBC commands to DBMS-specific native calls. This is much like the restriction of Type 1
drivers. The client must have some binary code loaded on its machine. These drivers do have an advantage over Type 1 drivers
because they interface directly with the database.
Type 3: JDBC-Net Pure Java Driver:
The JDBC-Net drivers are a three-tier solution. This type of driver translates JDBC calls into a database-independent network
protocol that is sent to a middleware server. This server then translates this DBMS-independent protocol into a DBMS-specific
protocol, which is sent
to a particular database. The results are then routed back through the middleware server and sent back to the client. This type of
solution makes it possible to implement a pure Java client. It also makes it possible to swap databases without affecting the client.
Type 4: Native-Protocol Pure Java Driver
These are pure Java drivers that communicate directly with the vendor’s database. They do this by converting JDBC commands
directly into the database engine’s native protocol. This driver has no additional translation or middleware layer, which improves
performance tremendously.
What does the "static" keyword mean in front of a variable? A method? A class? Curly braces {}?
static variable
- means a class level variable
static method:
-does not have "this". It is not allowed to access the not static members of the class.
can be invoked enev before a single instance of a class is created.
eg: main
static class:
no such thing.
static free floating block:
is executed at the time the class is loaded. There can be multiple such blocks. This may be useful to load native libraries when using
native methods.
eg:
native void doThis(){
static{
System.loadLibrary("myLibrary.lib");
}
What does the "final" keyword mean in front of a variable? A method? A class?
FINAL for a variable : value is constant
FINAL for a method : cannot be overridden
FINAL for a class : cannot be derived
A final variable cannot be reassigned,
but it is not constant. For instance,
final StringBuffer x = new StringBuffer()
x.append("hello");
is valid. X cannot have a new value in it,but nothing stops operations on the object
that it refers, including destructive operations. Also, a final method cannot be overridden
or hidden by new access specifications.This means that the compiler can choose
to in-line the invocation of such a method.(I don’t know if any compiler actually does
this, but it’s true in theory.) The best example of a final class is
String, which defines a class that cannot be derived.