The essential Java language shows a walk,
Autoboxing and unboxing, improved for loops, static imports, varargs and covariant return types
To satisfy the need for constant interfaces, avoiding the problems imposed by the use of these interfaces, Java 5 introduced static imports. This language feature can be used to import the static members of a class. It is implemented by the static import declaration whose syntax appears below:
import of static packages. name of the class (
staticmembername | *);
Static placement after import is separated from a regular import declaration. Its syntax is similar to the regular import declaration in terms of the default list of packet separation and subfolder names. A single static member name or all static member names (thanks to the asterisk) can be imported. Consider the following examples:
import java.lang.math static. *; // Import all static members of mathematics.
import java.lang.Math.PI static; // Import only the static constant PI.
static import java.lang.Math.cos; // Import only the static method cos ().
Once imported, static members can be specified without having to prefix them with the names of the classes. For example, after specifying the first or third static import, you could specify cos directly, as in double cosine = cos (angle);
To correct Listing 6 so that it does not rely more on Switchable implements, we can insert a static import, which Listing 7 shows.
Listing 7. Light.java (version 2)
foo package;
import static foo.Switchable. *;
public class Light
{
Private boolean state = OFF;
public void printState ()
{
System.out.printf ("state =% s% n", (state == OFF)? "OFF": "ON");
}
public void toggle ()
{
state = (status == DISABLED)? ENABLED DISABLED;
}
}
Listing 7 starts with a foo package; because you can not import static members of a type that is in the default package. This package name appears as part of the static foo.Setkey of subsequent import. *; static import.
There are two additional precautions in relation to static imports:
When two static imports import the same named member, the compiler reports an error. For example, suppose that the physics of the package contains a Mathematics class that is identical to the math class of java.lang, insofar as it implements the same constant PI and trigonometric methods. When faced with the following piece of code, the compiler reports errors because it can not determine whether java.lang.math or physical. The PI constant of the Math is being accessed and the cos () method is being called:
static import java.lang.Math.cos;
import of static physics.Math.cos;
double angle = PI;
System.out.println (cos (angle));
Excessive use of static imports can make your code illegible and not maintained because it contaminates the space for the code name with all the static members that matter. Also, anyone who reads your code may have difficulty finding out what kind a static member comes in, especially when importing all the static member names of a class.
varargs
The variable argument function (varargs) allows you to pass a list of variable arguments to a method or a constructor without having to first frame these arguments in an array.
To use varargs, you must first declare the method or the constructor with ... after the name of the parameter type further to the right in the parameter list of the method / constructor. Consider the following example:
static void printNames (String ... names)
{
(name of the rope: names)
System.out.println (name);
}
In this example, printNames () declares a unique parameter of names. Notice the three points after the name of the String type. In addition, I use the enhanced loop to iterate over the variable number of string arguments passed to that method.
When invoking the method, specify a comma-separated list of arguments that match the type of parameter. For example, given the declaration of the previous method, you could invoke this method in the following way:
PrintNames ("Java", "JRuby", "Jython", "Scala");
Behind the scenes, varargs is implemented in terms of a matrix and ... it's syntax sugar that hides the implementation of the matrix. However, you can access the method matrix in the following way:
static void printNames2 (String ... names)
{
(int i = 0; i <names.length; i ++)
System.out.println (names [i]);
}
In addition, you can create and pass the matrix directly, although normally you have notga In the following example: printNames2 (new string [] {"Java", "JRuby", "Jython", "Scala"}); Covariant return types The new Java language feature 5 is the covariant return type, which is a return type of the method that, in the declaration of the superclass method, is the supertype of the return type in the declaration of the substitution method of the subclass. Consider Listing 8. List 8.
CovarDemo.java class paper
{@Sobrepor public String toString () {returns "paper instance";
}
}
}
} PaperFactory {Paper newInstance () {Return paper (); }}}
Class The card extends the paper
{@Override public String toString () {return "cardboard instance";
}
}
class CardboardFactory extends PaperFactory {@Override Cardboard newInstance () {returns New Cardboard (); }} public class CovarDemo {public static void main (String [] args)
{
PaperFactory pf = new PaperFactory ();
Paper paper = pf.newInstance (); System.out.println (paper);
CardboardFactory cf = new CardboardFactory ();
Cardboard Card = cf.newInstance (); System.out.println (cardboard);
}
}
List 8 shows the Paper, PaperFactory, Cardboard, CardboardFactory, and CovarDemo classes.
The key classes in this demo are PaperFactory and CardboardFactory.PaperFactory declares a new Instance () method that the PaperboardFactory replaces. Note that the return type changes paper in the PaperFactory superclass for the paper in the CardboardFactory subclass. The type of return is said covariant. Listing compilation 8 (javac CovarDemo.java) and run the application (java CovarDemo). You should observe the following output: instances of instance instance instances.
The conversion return types eliminate the explicit templates. For example, if the NewInstance () method of CardboardFactory has been declared with a return type of paper, you must specify the cardboard card = (Cardboard) cf.newInstance (); in the main method (). Types of conversion and generic returns.
The preliminary accounting operations are common to the covariance and generic return types, so the covariant return types were implemented as part of the generics and why they are not included in the list of Java language features 5. In conclusion , Java 5 improved the type security, mainly through generic, but also through type sulfur. It also improved developer productivity by introducing annotations, autobox and unboxing, the improved for loop, static imports, varargs and covariant return types. Near Java 101: The next generation series is a look at the productivity-oriented language resources introduced in Java 7.
Autoboxing and unboxing, improved for loops, static imports, varargs and covariant return types
To satisfy the need for constant interfaces, avoiding the problems imposed by the use of these interfaces, Java 5 introduced static imports. This language feature can be used to import the static members of a class. It is implemented by the static import declaration whose syntax appears below:
![]() |
Best Java Training Institutes in Chennai |
import of static packages. name of the class (
staticmembername | *);
Static placement after import is separated from a regular import declaration. Its syntax is similar to the regular import declaration in terms of the default list of packet separation and subfolder names. A single static member name or all static member names (thanks to the asterisk) can be imported. Consider the following examples:
import java.lang.math static. *; // Import all static members of mathematics.
import java.lang.Math.PI static; // Import only the static constant PI.
static import java.lang.Math.cos; // Import only the static method cos ().
Once imported, static members can be specified without having to prefix them with the names of the classes. For example, after specifying the first or third static import, you could specify cos directly, as in double cosine = cos (angle);
To correct Listing 6 so that it does not rely more on Switchable implements, we can insert a static import, which Listing 7 shows.
Listing 7. Light.java (version 2)
foo package;
import static foo.Switchable. *;
public class Light
{
Private boolean state = OFF;
public void printState ()
{
System.out.printf ("state =% s% n", (state == OFF)? "OFF": "ON");
}
public void toggle ()
{
state = (status == DISABLED)? ENABLED DISABLED;
}
}
Listing 7 starts with a foo package; because you can not import static members of a type that is in the default package. This package name appears as part of the static foo.Setkey of subsequent import. *; static import.
There are two additional precautions in relation to static imports:
When two static imports import the same named member, the compiler reports an error. For example, suppose that the physics of the package contains a Mathematics class that is identical to the math class of java.lang, insofar as it implements the same constant PI and trigonometric methods. When faced with the following piece of code, the compiler reports errors because it can not determine whether java.lang.math or physical. The PI constant of the Math is being accessed and the cos () method is being called:
static import java.lang.Math.cos;
import of static physics.Math.cos;
double angle = PI;
System.out.println (cos (angle));
Excessive use of static imports can make your code illegible and not maintained because it contaminates the space for the code name with all the static members that matter. Also, anyone who reads your code may have difficulty finding out what kind a static member comes in, especially when importing all the static member names of a class.
varargs
The variable argument function (varargs) allows you to pass a list of variable arguments to a method or a constructor without having to first frame these arguments in an array.
To use varargs, you must first declare the method or the constructor with ... after the name of the parameter type further to the right in the parameter list of the method / constructor. Consider the following example:
static void printNames (String ... names)
{
(name of the rope: names)
System.out.println (name);
}
In this example, printNames () declares a unique parameter of names. Notice the three points after the name of the String type. In addition, I use the enhanced loop to iterate over the variable number of string arguments passed to that method.
When invoking the method, specify a comma-separated list of arguments that match the type of parameter. For example, given the declaration of the previous method, you could invoke this method in the following way:
PrintNames ("Java", "JRuby", "Jython", "Scala");
Behind the scenes, varargs is implemented in terms of a matrix and ... it's syntax sugar that hides the implementation of the matrix. However, you can access the method matrix in the following way:
static void printNames2 (String ... names)
{
(int i = 0; i <names.length; i ++)
System.out.println (names [i]);
}
In addition, you can create and pass the matrix directly, although normally you have notga In the following example: printNames2 (new string [] {"Java", "JRuby", "Jython", "Scala"}); Covariant return types The new Java language feature 5 is the covariant return type, which is a return type of the method that, in the declaration of the superclass method, is the supertype of the return type in the declaration of the substitution method of the subclass. Consider Listing 8. List 8.
CovarDemo.java class paper
{@Sobrepor public String toString () {returns "paper instance";
}
}
}
} PaperFactory {Paper newInstance () {Return paper (); }}}
Class The card extends the paper
{@Override public String toString () {return "cardboard instance";
}
}
class CardboardFactory extends PaperFactory {@Override Cardboard newInstance () {returns New Cardboard (); }} public class CovarDemo {public static void main (String [] args)
{
PaperFactory pf = new PaperFactory ();
Paper paper = pf.newInstance (); System.out.println (paper);
CardboardFactory cf = new CardboardFactory ();
Cardboard Card = cf.newInstance (); System.out.println (cardboard);
}
}
List 8 shows the Paper, PaperFactory, Cardboard, CardboardFactory, and CovarDemo classes.
The key classes in this demo are PaperFactory and CardboardFactory.PaperFactory declares a new Instance () method that the PaperboardFactory replaces. Note that the return type changes paper in the PaperFactory superclass for the paper in the CardboardFactory subclass. The type of return is said covariant. Listing compilation 8 (javac CovarDemo.java) and run the application (java CovarDemo). You should observe the following output: instances of instance instance instances.
The conversion return types eliminate the explicit templates. For example, if the NewInstance () method of CardboardFactory has been declared with a return type of paper, you must specify the cardboard card = (Cardboard) cf.newInstance (); in the main method (). Types of conversion and generic returns.
![]() |
| No.1 Java Training Institutes in chennai |


Wish you good luck.Sure will refer my friends.
ReplyDeleteapple service center chennai
apple service center in chennai
iphone service center in chennai
I really liked your blog article.Really thank you! Really Cool.
ReplyDeleteMYSQL training
OBIEE online training
OBIEE training
Oracle 11g rac online training
Oracle 11g rac training
Oracle Access Manager online training
Oracle Access Manager training
Oracle ADF online training
Oracle ADF training
Oracle Apps funcational online training