Found 4338 Articles for Java 8

When to use vararg methods in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

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

What are vararg methods in Java?

Lakshmi Srinivas
Updated on 18-Feb-2020 10:02:14

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

What are variadic functions in Java?

Monica Mona
Updated on 16-Jun-2020 06:19:30

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

What are annotations in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:20

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

What is the difference between compositions and aggregations in Java?

Sharon Christine
Updated on 30-Jul-2019 22:30:20

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.

How to execute a static block without main method in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20

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

How to print new line in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

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

How do I declare and initialize an array in Java?

karthikeya Boyini
Updated on 19-Feb-2020 10:06:11

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}

How to declare an Array Variables in Java?

Sharon Christine
Updated on 19-Feb-2020 10:04:43

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}

What is a composition in Java?

Lakshmi Srinivas
Updated on 30-Jul-2019 22:30:20

202 Views

The 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.

Advertisements