Java FAQ



What are exceptions, and when should I use them?

Exception handling is an important feature of Java. Exceptions indicate unusual error conditions that occur during the execution of an application or applet. When you call an object method, and an "exceptional" event occurs (such as being unable to access a file or network resource), the method can stop execution, and "throw" an exception. This means that it passes an object (the exception), back to the calling code. That code can then handle the event, and deal with unusual conditions.


How do I create a new instance of an object?

To create a new instance of an object, we use the "new" keyword. This keyword creates a new instance of an object, which we can then assign to a variable, or invoke methods.



How do I compare two strings?

To correctly compare two strings, we must use the .equals method(). This method is inherited from java.lang.Object, and can be used to compare any two strings.
 


How I can I return a null value from an object constructor?

You don't return any value whatsoever from an object constructor. The object has already been created - in the constructor you're just initializing the object's state. So there isn't any way to return a null value.



How can I create an event handling mechanism in Java?

The most common mechanism for this is the callback, where one class calls the method of another to notify it of an action or event. The class to be notified defines methods that will respond to specific events, such as when a mouse is clicked, dragged, or released. The AWT makes heavy use of this, with Listener interfaces.



How can I append an existing file in Java?

Appending data to a file is a fairly common task. You'll be surprised to know that there was no support for file appending in JDK1.02, and developers supporting that platform are forced to re-write the entire file to a temporary file, and then overwrite the original. As most users support either JDK1.1 or the Java 2 platform, you'll probably be able to use the FileOutputStream constructor to append data: 
 


What is multithreading?

Multithreading allows two parts of the same program to run concurrently.



What is the difference between an applet and an application?

In simple terms, an applet runs under the control of a browser, whereas an application runs stand-alone, with the support of a virtual machine. As such, an applet is subjected to more stringent security restrictions in terms of file and network access, whereas an application can have free reign over these resources



How can I load a particular HTML page from within an applet?

Applets are executed within a web browser, and there's an easy way to load show a specific URL.
Obtain a reference to the applet context
Call the showDocument method, which takes as a parameter a URL object.


 
What is the use of interface?

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations.
 


Why can't my applet read or write to files?

Applets execute under the control of a web browser. Netscape and Internet Explorer impose a security restriction, that prohibits access to the local filesystem by applets. While this may cause frustration for developers, this is an important security feature for the end-user. Without it, applets would be free to modify the contents of a user's hard-drive, or to read its contents and send this information back over a network.



How can I change the gray background of an applet?

Applets use a default background of gray, which isn't very visually appealing, and very infrequently matches the background of the web page on which it is loaded. So unless you repaint the background yourself in the paint() method of your applet, you'll want to change its background as soon as the applet loads.The best place to do it will be in your init() method. This means the applet will change color once it has finished loading. To change background color, you need to invoke the setBackground(Color) method. It accepts as a parameter any valid Color.



How can I create my own GUI components?

Custom graphical components can be created by producing a class that inherits from java.awt.Canvas. Your component should override the paint method, just like an applet does, to provide the graphical features of the component.



What is 64-bit Java?

A 64-bit version of Java has been available to Solaris SPARC users since the 1.4.0 release of J2SE.  A 64-bit capable J2SE is an implementation of the Java SDK (and the JRE along with it) that runs in the 64-bit environment of a 64-bit OS on a 64-bit processor. You can think of this environment as being just another platform to which we've ported the SDK.  The primary advantage of running Java in a 64-bit environment is the larger address space. This allows for a much larger Java heap size and an increased maximum number of Java Threads, which is needed for certain kinds of large or long-running applications.



Why is java not fully objective oriented?

Java is not fully object oriented because it doesn't implement primitive datatypes as objects. Also it doesn't support multiple inheritance of classes(though alternatives are provided i.e,interfaces).
 


Why does Java not support multiple inheritance?

The fact is that Java separates implementation inheritance from interface inheritance. Interface inheritance deals with the "contract" or "agreement " which a class should adhere & on the other hand implementation inheritance deals with the reuse of code and the contract which a class adheres to.



What is the Difference between Vector and ArrayList?

Vector is synchronized whereas arraylist is not.



What is the difference between method overloading and method overriding?

when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass.When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method's parameters. However, this match need not always be exact.



What is polymorphism?

Polymorphism means "many form". A object or data can behave differently at different situation depend upon how we use that object or data. A good example of polymorphism is method overriding.
 


What is the difference between C and Java?

Java is not like C but the major difference between Java and C is that Java an object Oriented language. It has a mechanism to define classes and objects.
 


What is garbage collection?

Garbage collection is one of the most important feature of Java. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory.
 


What is the use of finalize() method?

Sometimes an object will need to perform some action when it is destroyed. For example if an object is holding some non-Java resource such as a file handle, then these resources should be freed before an object is destroyed.



What is recursion?

Recursion is the process of calling a function itself. 



How can we assign a constant value to a variable? 

Using final keyword we can assign a constant value to a variable. Final variable must initialize, when it is declared.
 


What is a package?

Packages are containers for classes that are used to keep the class name space compartmentalized. For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other classes named List stored elsewhere.
 


How to find length of a string in Java?

The length of a string is the number of characters that it contains. To obtain this value call the length() method.
 


What is the use of  throws clause?

If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can guard themselves against that exception. This is acheived by including a throws clause in the method's declaration. A throws clause lists the types of exceptions that a method might throw. 
 


What is an applet?

Applet is a small window based program, that run on an HTML page. To run applet we need a web browser.
 


What is BufferedReader?

BufferedReader is an abstract class. One of its subclass is InputStreamReader which convert bytes to characters.
 


What is an exception in terms of Java?

A Java exception is an object that describes an exceptional condition that has occured in a piece of code. When an exception condition arises, an object representing that exception is created and thrown in the method that caused error.
 


What is the difference between equals() and == in Java?

The equals method compares the characters inside a String object. The == operator compares two object references to see wheather they refer to the same instance.



What is the use of  implements clause?

Once an interface has been defined, one or more classes can implement that interface.To implement an interface, include the implements clause in a class definition, and then create the methods defined by the interface. 



What's the difference between a member variable, and a local variable?

A member variable is a variable that belongs to an object, whereas a local variable belongs to the current scope.



What is an abstract class, and when should it be used?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.



What does the keyword 'static' mean?

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.
 


What is the difference between public, private, and protected keywords?

We use these keywords to specify access levels for member variables, or for member functions (methods).Public variables, are variables that are visible to all classes.Private variables, are variables that are visible only to the class to which they belong.Protected variables, are variables that are visible only to the class to which they belong, and any subclasses.
 


What does the 'extends' keyword mean?

It is used in a class declaration to specify the superclass.



How do I run the garbage collector to free memory and delete unused objects?

you can request the garbage collector to perform work, by invoking the System.gc() method. Once working, you may experience a short pause while the garbage collector does its work. That means you should do it at an appropriate time, such as before a big task or when the GUI is idle and waiting for input.



How do I convert between an int and a char?

You can convert an int and achar by casting.
eg: int  a = 65;
char myChar = (char) a;



How can I throw an exception without declaring it in a 'throws' clause?

Sometimes it is useful to throw exceptions, without explicitly declaring them in the throws clause of a method's signature. Any exceptions that extends java.lang.RuntimeException don't need to be declared. This means that it is safe to throw subclasses (like NullPointException), or to create your own.
 


What is the difference between HashMap and HashTable

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2. The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.
 


What is Synchronization?

Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value.
 


What is the difference between JDK and JRE?

The "JDK" is the Java Development Kit. i.e., the JDK is bundle of software that you can use to develop Java based software. The "JRE" is the Java Runtime Environment. i.e., the JRE is an implementation of the Java Virtual Machine which actually executes Java programs.
 


What is the difference between a constructor and a method?
 
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.
 


What is an Iterator?

Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.
 


What are the major advantages of using class?

1. Class is responsible for the validity of the data. 
2. Implementation details can be hidden.
3. Class can be reused.



How Java ensures portability?

Java ensures portability in two ways. First, Java compiler generates bytecode instructions that can be implemented on any machine. Secondly, the size of primitive data types are machine independant. 
 


What is literals in Java?

Literals in Java are sequence of characters(digits,letters,and other characters) that represent constant values to be stored in variables.
 


What is the use of  'this' keyword?

The 'this' keyword can be used inside any method to refer to the current object. In other words 'this' is always reference to the object on which the method was invoked.
 


TekTipsDownload
GateExam
Academic Projects
TekTipsExperts



 
Site optimized for IE7, 1280 X 768 and above. Copyright © 2010 - 2018 KTS InfoTech
Site Developed Using KTS WebCloud