23-12-2009, 06:18 AM | #21 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
Do I need to import java.lang package any time? Why?
|
23-12-2009, 06:18 AM | #22 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
Can I import same package/class twice? Will the JVM load the package twice at runtime
One can import the same package or same class multiple times. Neither compiler nor JVM complains anything about it. And the JVM will internally load the class only once no matter how many times you import the same class.
|
23-12-2009, 06:20 AM | #23 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
Does importing a package imports the sub packages as well?
Does importing com.abhi.* also import com.abhi.pgr.*?
No you will have to import the sub packages explicitly. Importing com.abhi.* will import classes in the package abhi only. It will not import any class in any of its sub packages. |
23-12-2009, 06:20 AM | #24 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
Explain the usage of Java packages.
A Java package is a naming context for classes and interfaces. A package is used to create a separate name space for groups of classes and interfaces.
Packages are also used to organize related classes and interfaces into a single API unit and to control accessibility to these classes and interfaces. For example: The Java API is grouped into libraries of related classes and interfaces; these libraries are known as package. |
23-12-2009, 06:21 AM | #25 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
Are the imports checked for validity at compile time?
e.g. will the code containing an import such as java.lang.abhi compile?
Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying, cannot resolve symbol. |
23-12-2009, 06:24 AM | #26 |
Member
Join Date: Dec 2009
Posts: 33
Rep Power: 0 |
What is the difference between String and StringBuffer?
String objects are immutable whereas StringBuffer objects are not. StringBuffer unlike Strings support growable and modifiable strings.
|
25-12-2009, 07:01 AM | #27 |
Member
Join Date: Dec 2009
Posts: 64
Rep Power: 15 |
What is the default value of an object reference declared as an instance variable?
null unless we define it explicitly.
|
25-12-2009, 07:01 AM | #28 |
Member
Join Date: Dec 2009
Posts: 64
Rep Power: 15 |
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, invoked using the new operator. It has the same name as the class and has no return type. They are only called once, whereas member functions can be called many times. 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. Constructor will be automatically invoked when an object is created whereas method has to be called explicitly.
super.method(); is used to call a super class method from a sub class. To call a constructor of the super class, we use the super(); statement as the first line of the subclass constructor. |
25-12-2009, 07:03 AM | #29 |
Member
Join Date: Dec 2009
Posts: 64
Rep Power: 15 |
Does Java support multiple inheritance?
Java does support multiple inheritance via interface implementation.
|
25-12-2009, 07:03 AM | #30 |
Member
Join Date: Dec 2009
Posts: 64
Rep Power: 15 |
Where and how can you use a private constructor?
Private constructor can be used if you do not want any other class to instantiate the class. This concept is generally used in Singleton Design Pattern. The instantiation of such classes is done from a static public method.
|
Bookmarks |
Tags |
java interview question |
|
|