- Back to Home »
- Core Java »
- New Features in Java
New Features in Java
There are many new features that have been added in java. There are major enhancement made in Java5, Java6 and Java7 like auto-boxing, generics, var-args, java annotations, enum, premain method etc.
J2SE 4 Features
The important feature of J2SE 4 is assertions. It is used for testing.
Assertion:
Assertion is a statement in java. It can be used to test your assumptions about the program.
While executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError. It is mainly used for testing purpose.
Advantage of Assertion:
It provides an effective way to detect and correct programming errors.
Syntax of using Assertion:
There are two ways to use assertion. First way is:
and second way is:
assert expression1 : expression2;
Where not to use Assertion:
There are some situations where assertion should be avoid to use. They are:
- According to Sun Specification, assertion should not be used to check arguments in the public methods because it should result in appropriate runtime exception e.g. IllegalArgumentException, NullPointerException etc.
- Do not use assertion, if you don't want any error in any situation.
J2SE 5 Features
The important features of J2SE 5 are generics and assertions. Others are auto-boxing, enum, var-args, static import, for-each loop (enhanced for loop etc.
- For-each loop (Java 5)
- Varargs (Java 5)
- Static Import (Java 5)
- Autoboxing and Unboxing (Java 5)
- Enum (Java 5)
- Covariant Return Type (Java 5)
- Annotation (Java 5)
- Generics (Java 5)
For-each loop (Advanced or Enhanced For loop):
The for-each loop introduced in Java5. It is mainly used to traverse array or collection elements. The advantage of for-each loop is that it eliminates the possibility of bugs and makes the code more readable.
Advantage of for-each loop:
- It makes the code more readable.
- It elimnates the possibility of programming errors.
Syntax of for-each loop:
Variable Argument (Varargs):
The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don't know how many argument we will have to pass in the method, varargs is the better approach.
Advantage of Varargs:
We don't have to provide overloaded methods so less code.
Syntax of varargs:
The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:
Rules for varargs:
While using the varargs, you must follow some rules otherwise program code won't compile. The rules are as follows:
- There can be only one variable argument in the method.
- Variable argument (varargs) must be the last argument.
Static Import:
The static import feature of Java 5 facilitate the java programmer to access any static member of a class directly. There is no need to qualify it by the class name.
Advantage of static import:
- Less coding is required if you have access any static member of a class oftenly.
Disadvantage of static import:
- If you overuse the static import feature, it makes the program unreadable and unmaintainable.
What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package qualification whereas the static import feature allows to access the static members of a class without the class qualification. The import provides accessibility to classes and interface whereas static import provides accessibility to static members of the class.
Autoboxing and Unboxing:
The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code.
Advantage of Autoboxing and Unboxing:
No need of conversion between primitives and Wrappers manually so less coding is required. |
Autoboxing and Unboxing with comparison operators
Autoboxing can be performed with comparison operators.Autoboxing and Unboxing with method overloading
|
Generics in Java
The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects.
Before generics, we can store any type of objects in collection i.e. non-generic. Now generics, forces the java programmer to store specific type of objects.
Advantage of Java Generics
There are mainly 3 advantages of generics. They are as follows:
1) Type-safety : We can hold only a single type of objects in generics. It doesn’t allow to store other objects.
2) Type casting is not required: There is no need to typecast the object.
Before Generics, we need to type cast.
After Generics, we don't need to typecast the object.
3) Compile-Time Checking: It is checked at compile time so problem will not occur at runtime. The good programming strategy says it is far better to handle the problem at compile time than runtime.
Syntax to use generic collection
Simple example to use Generics
Generic class
A class that can refer to any type is known as generic class. Here, we are using T type parameter to create the generic class of specific type.
Let’s see the simple example to create and use the generic class.
Creating generic class:
The T type indicates that it can refer to any type (like String, Integer, Employee etc.). The type you specify for the class, will be used to store and retrieve the data.
Using generic class:
Let’s see the code to use the generic class.
Output:2
Understanding Type Parameters
The type parameters naming conventions are important to learn generics thoroughly. The commonly type parameters are as follows:
- T - Type
- E - Element
- K - Key
- N - Number
- V - Value
- S,U,V etc. - 2nd, 3rd, 4th types
Generic Method
Like generic class, we can create generic method that can accept any type of argument.
Let’s see a simple example of java generic method to print array elements. We are using here E to denote the element.
Covariant Return Type
The covariant return type specifies that the return type may vary in the same direction as the subclass.
Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type
JavaSE 6 Features
The important feature of JavaSE 6 is premain method (also known as instrumentation).
- Instrumentation (premain method) (Java 6)
JavaSE 7 Features
The important features of JavaSE 7 are try with resource, catching multiple exceptions etc.
- String in switch statement (Java 7)
- Binary Literals (Java 7)
- The try-with-resources (Java 7)
- Caching Multiple Exceptions by single catch (Java 7)
- Underscores in Numeric Literals (Java 7)
Java training in chandigarh
ReplyDeleteGreat wordpress blog here.. It's hard to find quality writing like yours these days. I really appreciate people like you! take care
Java training in chandigarh
ReplyDeleteI love your blog.. very nice colors & theme. Did you create this website yourself? Plz reply back as I'm looking to create my own blog and would like to know where u got this from. thanks