Found 33676 Articles for Programming

Difference between declaring a variable before or in a Java loop.

Giri Raju
Updated on 30-Jul-2019 22:30:21

125 Views

Performance wise, there is hardly any difference. But it is good to keep a variable local to the scope it is used. So declaring a variable inside Java loop is generally preferred.

How to convert List to int[] in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:21

955 Views

You can simply iterate through list and fill the array as shown below −import java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List list = new ArrayList();       list.add(new Integer(1));       list.add(new Integer(2));       list.add(new Integer(3));       list.add(new Integer(4));       int[] array = new int[list.size()];       for(int i=0;i

How to pass a function as a parameter in Java

Prabhas
Updated on 17-Jun-2020 11:36:16

5K+ Views

Yes. From Java 8 onwards, we can do so using method references.Method references help to point to methods by their names. A method reference is described using "::" symbol. A method reference can be used to point the following types of methods −Static methodsInstance methodsConstructors using new operator (TreeSet::new)Method Reference ExampleCreate the following Java program using any editor of your choice in, say, C:\> JAVA.Java8Tester.java Live Demo import java.util.List; import java.util.ArrayList; public class Java8Tester {    public static void main(String args[]) {       List names = new ArrayList(); names.add("Mahesh");       names.add("Suresh");       names.add("Ramesh");       ... Read More

How to return 2 values from a Java method

seetha
Updated on 17-Jun-2020 11:32:59

783 Views

A method can give multiple values if we pass an object to the method and then modifies its values. See the example below −Examplepublic class Tester {    public static void main(String[] args) {       Model model = new Model();       model.data1 = 1;       model.data2 = 2;       System.out.println(model.data1 + ", " + model.data2);       changeValues(model);       System.out.println(model.data1 + ", " + model.data2);    }    public static void changeValues(Model model) {       model.data1 = 100;       model.data2 = 200;    } } class Model {    int data1;    int data2; }Output1, 2 100, 200

Why can't static method be abstract in Java?

radhakrishna
Updated on 30-Jul-2019 22:30:21

2K+ Views

A static method belongs to class not to object instance thus it cannot be overridden or implemented in a child class. So there is no use of making a static method as abstract.

Is java pass by reference or pass by value?

varun
Updated on 24-Feb-2020 12:22:39

893 Views

Call by Value means calling a method with a parameter as value. Through this, the argument value is passed to the parameter.While Call by Reference means calling a method with a parameter as reference. Through this, the argument reference is passed to the parameter.In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in call by reference, the the modification done to the parameter passed are persistent and changes are reflected in the caller's scope.But Java uses only call by value. It creates a copy of references and pass them as ... Read More

Breaking out of nested loop in java

usharani
Updated on 24-Feb-2020 12:17:34

437 Views

Yes, break statement can be used in nested for loops. It works on the for loop in which it is applied. See the example below.Examplepublic class Tester {    public static void main(String[] args) {       for(int i = 0; i< 2; i++) {          for(int j = 0; j < 2; j++){             if(i == 1) {                break;             }             System.out.println("i = " + i+",j = " + j);          }       }    } }

How to empty an array in Java

Rahul Sharma
Updated on 24-Feb-2020 11:10:47

5K+ Views

Use List.clear() method to empty an array.Exampleimport java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List list = new ArrayList();       list.add("A");       list.add("B");       list.add("C");       list.add("D");       list.add("E");       list.add("F");       list.add("G");       list.add("H");       System.out.println("Original List " + list);       list.clear();       System.out.println("Cleared List " + list);    } }OutputOriginal List [A, B, C, D, E, F, G, H] Cleared List []

How to create an array of Object in Java

Johar Ali
Updated on 07-Mar-2025 17:39:38

847 Views

In this article, we will learn to create an array of objects in Java. An array of Object classes can be created that can accept any type of object. During the operation on such an array, instanceof operator can be used. Different Approaches The following are the two different approaches to creating an array of objects in Java − Using an Object[] Array Using a Class-Specific Object Array Using an Object[] Array Java provides a built-in Object class, which is the superclass of all classes. This means an array ... Read More

How to check whether element is in the array in Java?

Ali
Ali
Updated on 24-Feb-2020 10:55:45

139 Views

Following example uses Contains method to search a String in the Array.Exampleimport java.util.ArrayList; public class Main {    public static void main(String[] args) {    ArrayList objArray = new ArrayList();    ArrayList objArray2 = new ArrayList();    objArray2.add(0, "common1"); objArray2.add(1, "common2");   objArray2.add(2, "notcommon"); objArray2.add(3, "notcommon1");    objArray.add(0, "common1"); objArray.add(1, "common2");    System.out.println("Array elements of array1"+objArray);    System.out.println("Array elements of array2"+objArray2);    System.out.println("Array 1 contains String common2?? " +objArray.contains("common1"));    System.out.println("Array 2 contains Array1?? " +objArray2.contains(objArray) );     }  }OutputThe above code sample will produce the following result.Array elements of array1[common1, common2] Array elements of array2[common1, common2, notcommon, notcommon1] Array ... Read More

Advertisements