Upload
aparnajanwalkar
View
215
Download
0
Embed Size (px)
Citation preview
8/12/2019 6.Newfeatures of Java
1/9
8/12/2019 6.Newfeatures of Java
2/9
For each loop
The for-each style of for is an enhanced for loop.
The general form of the for-each version of the for is shown here
for(type itr-var : collection) statement-block
Here, type specifies the type and itr-var specifies the name of an
iteration variable that will receive the elements from a
collection, one at a time, from beginning to end.
It can be used for iterating multidimensional arrays.
8/12/2019 6.Newfeatures of Java
3/9
Here is an entire program that demonstrates the for-each version of the
for just described:
// Use a for-each style for loop.
class ForEach {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
for(int x : nums) {
System.out.println("Value is: " + x);sum += x;
}
System.out.println("Summation: " + sum);
}
}
8/12/2019 6.Newfeatures of Java
4/9
In cases where the maximum number of potential arguments was larger, or
unknowable,a second approach was used in which the arguments were put into a
array, and then the array was passed to the method. This approach is illustrated b
the following program:
class PassArray {
static void vaTest(int v[]) {
System.out.print("Number of args: " + v.length +" Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[])
{ int n1[] = { 10 };//variable length arguments
int n2[] = { 1, 2, 3 };
vaTest(n1);
vaTest(n2); } }
8/12/2019 6.Newfeatures of Java
5/9
Variable Length Arguments
Sometimes we need creation of methods That take a variable num
of arguments. This feature is called varargsand it is short forvariable-length arguments. A method that takes a variable numb
arguments is called a variable-arity method, or simply a varargs
method.
Situations that require that a variable number of arguments be pas
to a method are not unusual.For example, a method that opens an Internet connection might ta
user name, password, filename, protocol, and so on, but supply
defaults if some of this information is not provided. In this situa
it would be convenient to pass only the arguments to which the
defaults did not apply.
8/12/2019 6.Newfeatures of Java
6/9
The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follow
return_type method_name(data_type... variableName){}
EX:
classVarargsExample1{staticvoiddisplay(String... values){
System.out.println("display method invoked ");
for(String s:values){
System.out.println(s);
}
publicstaticvoidmain(String args[]){
display();//zero argument
display("my","name","is","varargs");//four arguments
}
}
8/12/2019 6.Newfeatures of Java
7/9
Rules:While using the varargs, you must follow some rules otherwise progracode 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.
EX:
classVarargsExample3{
staticvoiddisplay(intnum, String... values){
System.out.println("number is "+num);for(String s:values){
System.out.println(s);
}
}
8/12/2019 6.Newfeatures of Java
8/9
Static import The static import feature of Java 5 facilitate the java programmer to access any st
member of a class directly. There is no need to qualify it by the class name.
Adv: Less coding is required if you have access any static member of a class often
Disadv: If you overuse the static import feature, it makes the program unreadable
unmaintainable.
Example:
importstaticjava.lang.System.*;
classStaticImportExample{publicstaticvoidmain(String args[]){
out.println("Hello");//Now no need of System.out
out.println("Java");
}
}
8/12/2019 6.Newfeatures of Java
9/9
Thank You