Interview Questions On Java,Java EE


Enter your email address:



Add to Technorati Favorites
Google PageRank Checker
Showing posts with label Java 5. Show all posts
Showing posts with label Java 5. Show all posts

Tuesday, May 26, 2009

Interview Questions on Java 5 - Part II

- What are Generic types and how it helps developers?
Generic types are now part of J2SE 5.0. At the first instance one will see them in action is the Collections API. The Collections API provides common functionality like LinkedLists, ArrayLists and HashMaps that can be used by more than one Java type. The next example uses the 1.4.2 libraries and the default javac compile mode.

ArrayList list = new ArrayList();
list.add(0, new Long(10));
int total = ((Integer)list.get(0)).intValue();

The cast to Integer on the last line is an example of the typecasting issues that generic types aim to prevent. The issue is that the 1.4.2 Collection API uses the Object class to store the Collection objects, which means that it cannot pick up type mismatches at compile time. The first notification of a problem is a ClassCastException at runtime.

The same example with the generified Collections library is written as follows:

ArrayList list = new ArrayList();
list.add(0, new Integer(42));
int total = list.get(0).intValue();


The user of a generified API has to simply declare the type used at compile type using the <> notation. No casts are needed and in this example trying to add a String object to an Integer typed collection would be caught at compile time.

Hence Generic types enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time.
- What are Annotations and how are they used?

Annotations have no direct impact on the execution of the code.They provide additional data about the program which is not part of the program itself.

Annotations are used for:

-Preferred over comments as documentation of a program is made quite flexible.
-Program's declarations of classes, fields, methods, and other program elements.
-Can be used by the compiler to detect errors or suppress warnings.
-Compiler-time and deployment-time processing where annotation information can be processed to generate code, XML files, and so forth.
—Some annotations are available to be examined at runtime.

The annotation appears first, often (by convention) on its own line, and may include elements with named or unnamed values:

@Author(
name = "Victor Hugo",
date = "9/20/2009"
)
class SampleClass() { }

or

@SuppressWarnings(value = "unchecked")
void sampleMethod() { }
If there is just one element named "value," then the name may be omitted, as in:
@SuppressWarnings("unchecked")
void sampleMethod() { }
Also, if an annotation has no elements, the parentheses may be omitted, as in:
@Override
void mySuperMethod() { }


- What is Enhanced for-loop feature?

It is a popular feature introduced with in Java SE 5.0.
There is always a backward compatibility with old style of for looping. The changed was of for-loop in Java 5 look something like the following:


The newer form:
for (String name : nameArray) {
System.out.println("Name: " + name);
}


- What is static import feature in Java 5?
- Enables imported static members to be referred as if class declares them within to be used. So no more need of qualifying static class name to access the methods defined in the class. Here is an example as given below:


import static java.lang.Math.*;

public class SampleStaticImport
{
public static void main( String args[] )
{
System.out.println(sqrt(625.0 ) );
System.out.println(sin( 90.0 ) );
}
}



- What are Varargs?

The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple ... notation for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf.They are most fondly used in core APIs including reflection, message formatting, and the new printf facility in Java 5. Read an article here on varargs for more details.
Continue reading...

Tuesday, April 14, 2009

Interview Questions on Java 5

- What are the new features in Java 5 as compared to its earlier versions?

Java 2 Platform Standard Edition 5.0 was a major feature release. The following features were introduced in 5.0 since previous major release and that was 1.4.0.

Summary:
- Performance Enhancements
a.Garbage collection performance enhancements
b.Introduction to StringBuilder
c.Java 2D technology
d.Image I/O - Performance and memory

- Language Specific Features:
a.Generics
b.Enhanced for Loop
c.Autoboxing/Unboxing
d.Typesafe Enums
e.Varargs
f.Static Import
g.Metadata (Annotations)

The list is long which you can catch in detail at following URL:

Java 5 New Features

- What is Autoboxing/Unboxing and what are its advantages?

The conversion from primitive types to wrapper types objects is automated. This facility eleminates pain of manual conversion from primitive to wrapper types.
Boxing converts primitive values to objects of corresponding wrapper types.
Unboxing converts objects of wrapper types to values of corresponding primitive types.

Assignment Conversions on boolean and numeric types.
boolean boolValue = true;
byte b = 2;
short s = 2;
char c ='2';
int i = 2;

// Boxing
Boolean boolReference = boolValue;
Byte bReference = (byte) 4;
Short sReference = (short) 4;
// both byte and short type casting is to be done as int cannot be assigned to Byte and Short respectively
Character cReference = '4';
Integer iReference = 4;

// Unboxing
boolean boolValue1 = boolReference;
byte b1 = bReference;
short s1 = sReference;
char c1 = cReference;
int i1 = iReference;


The usage of autoboxing/unboxing can be found:
- In the if statement, condition can be Boolean.
- In the switch statement, the switch expression can be Character, Byte, Short or Integer.
- In the while, do-while and for statements, the condition can be Boolean.

More questions in my next post on Java 5.
Continue reading...
 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker