- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4338 Articles for Java 8
125 Views
Whenever, you want to pass different number of arguments each time you call a method you should use vararg methods. This example creates sumvarargs() method which takes variable no of int numbers as an argument and returns the sum of these arguments as an output. Example Live Demo public class Main { static int sumvarargs(int... intArrays) { int sum, i; sum = 0; for(i = 0; i< intArrays.length; i++) { ... Read More
162 Views
In Java methods, parameters accept arguments with three dots. These are known as variable arguments.sample(int args …){}If they are used you can pass a different number of arguments each time you call these methods.Examplepublic class Sample { void demoMethod(String... args) { for (String arg: args) { System.out.println(arg); } } public static void main(String args[] ) { new Sample().demoMethod("ram", "rahim", "robert"); new Sample().demoMethod("krishna", "kasyap") } }Outputram rahim robert krishna kasyap
776 Views
Methods which uses variable arguments (varargs, arguments with three dots) are known as variadic functions.ExampleLive Demopublic class Sample { void demoMethod(String... args) { for (String arg: args) { System.out.println(arg); } } public static void main(String args[] ){ new Sample().demoMethod("ram", "rahim", "robert"); new Sample().demoMethod("krishna", "kasyap"); } }Outputram rahim robert krishna kasyap
518 Views
Annotations are a tag (metadata) which provides info about a program. Annotations in Java Start with the symbol ‘@’. They are used by the compiler to detect errors. Software tools to generate code. They are used to show attributes of an element: e.g. @Deprecated, @Override. Annotation are used to describe the purpose of an element of the framework, e.g. @Entity, @TestCase, @WebService Annotations describe the behaviour of an element: @Statefull, @Transaction. Example Live Demo class Sample{ public void display(){ System.out.println(" "); } } public ... Read More
153 Views
In Aggregation relationship among classes by which a class (object) can be made up of any combination of objects of other classes. It allows objects to be placed directly within the body of other classes.A composition is also a type of aggregation where the relationship is restrictive i.e. If two objects are in composition, the composed object will not exist without the other.
1K+ Views
VM first looks for the main method (at least the latest versions) and then, starts executing the program including static block. Therefore, you cannot execute a static block without main method. Example public class Sample { static { System.out.println("Hello how are you"); } } Since the above program doesn’t have a main method, If you compile and execute it you will get an error message. C:\Sample>javac StaticBlockExample.java C:\Sample>java StaticBlockExample Error: Main method not found in class StaticBlockExample, please define the main method as: public static ... Read More
2K+ Views
The java.io.PrintStream.println() method prints an array of characters and then terminate the line. This method behaves as though it invokes print(char[]) and then println(). Using this method you can print the data on the console. import java.io.*; public class PrintStreamDemo { public static void main(String[] args) { char[] c = {'a', 'b', 'c'}; // create print stream object PrintStream ps = new PrintStream(System.out); // print an array and change line ps.println(c); ps.print("New Line"); // flush the stream ps.flush(); } } Output abc New Line
384 Views
You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}
295 Views
You can declare an array just like a variable −int myArray[];You can create an array just like an object using the new keyword −myArray = new int[5];You can initialize the array by assigning values to all the elements one by one using the index −myArray [0] = 101; myArray [1] = 102;You can access the array element using the index values −System.out.println("The first element of the array is: " + myArray [0]); System.out.println("The first element of the array is: " + myArray [1]); Alternatively, you can create and initialize an array using the flower braces ({ }): Int [] myArray = {10, 20, 30, 40, 50}