Skip to main content

Posts

Showing posts from July, 2009

What happens if you do not provide a constructor?

Java does not actually require an explicit constructor in the class description. If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit "Pet(){}". If a class includes one or more explicit constructors like "public Pet(int id)" or "Pet(){}" etc, the java compiler does not create the default constructor "Pet(){}".  

What is the difference between constructors and other regular methods? What happens if you do not provide a constructor? Can you call one constructor from another? How do you call the superclass’s constructor?

Constructors: Constructors must have the same name as the class name and cannot return a value. The constructors are called only once per creation of an object while regular methods can be called many times.     E.g. for a Pet.class public Pet() {} // constructor   Regular Methods: Regular methods can have any name and can be called any number of times. E.g. for a Pet.class.   public void Pet(){} // regular method has a void return type.   Note: method name is shown starting with an uppercase to differentiate a constructor from a regular method. Better naming convention is to have a meaningful name starting with a lowercase like:   public void createPet (){} // regular method has a void return type  

Explain static vs. dynamic class loading?

loading Dynamic class loading Static Class Loading Classes are statically loaded with Java's "new" operator. class MyClass { public static void main(String args[]) { Car c = new Car (); } }   A NoClassDefFoundException is thrown if a class is referenced with Java's " new" operator (i.e. static loading) but the runtime system cannot find the   referenced class.     Dynamic loading   Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically. Class.forName (String className ); //static method which returns a Class The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Je

Explain Java class loaders? If you have a class in a package, what do you need to do to run it? Explain dynamic class loading?

Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that is already running in the JVM. So, how is the very first class loaded? The very first class is especially loaded with the help of static main( ) method declared in your class. All the subsequently loaded classes are loaded by the classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let's look at non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of primordial class loader. Let us look at the class loaders created by the JVM. class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true

What is the difference between C++ and Java?

Both C++ and Java use similar syntax and are Object Oriented, but:   ô€‚ƒ Java does not support pointers. Pointers are inherently tricky to use and troublesome.   ô€‚ƒ Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance , which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.   ô€‚ƒ Java does not support destructors but adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release nonmemory resources like file handles, sockets, da

What is the main difference between the Java platform and the other software platforms?

Java platform is a software-only platform, which runs on top of other hardware-based platforms like UNIX , NT etc .   The Java platform has 2 components: ô€‚ƒ Java Virtual Machine ( JVM ) – 'JVM' is a software that can be ported onto various hardware platforms. Byte codes are the machine language of the JVM.   ô€‚ƒ Java Application Programming Interface ( Java API ) – set of classes written using the Java language and run on the JVM.  

Give a few reasons for using Java?

Java is a fun language. Let's look at some of the reasons:   ô€‚ƒ Built-in support for multi-threading, socket communication, and memory management (automatic garbage collection). ô€‚ƒ Object Oriented (OO). ô€‚ƒ Better portability than other languages across operating systems. ô€‚ƒ Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI, EJB etc) and network protocols (HTTP, JRMP etc) with the help of extensive standardized APIs (Application Programming Interfaces).  

Hibernate Interview Questions

Q. How will you configure Hibernate? Answer: The configuration files hibernate.cfg.xml (or hibernate.properties) and mapping files *.hbm.xml are used by the Configuration class to create (i.e. configure and bootstrap hibernate) the SessionFactory, which in turn creates the Session instances. Session instances are the primary interface for the persistence service. " hibernate.cfg.xml (alternatively can use hibernate.properties): These two files are used to configure the hibernate sevice (connection driver class, connection URL, connection username, connection password, dialect etc). If both files are present in the classpath then hibernate.cfg.xml file overrides the settings found in the hibernate.properties file. " Mapping files (*.hbm.xml): These files are used to map persistent objects to a relational database. It is the best practice to store each object in an individual mapping file (i.e mapping file per class) because storing large number of persistent classes