Found 9326 Articles for Object Oriented Programming

How to process Arrays in Java?

Sharon Christine
Updated on 16-Jun-2020 10:10:14

1K+ Views

To process array elements, we often use either for loop or for each loop because all of the elements in an array are of the same type and the size of the array is known. Suppose we have an array of 5 elements we can print all the elements of this array as:ExampleLive Demopublic class ProcessingArrays {    public static void main(String args[]) {       int myArray[] = {22, 23, 25, 27, 30};       for(int i = 0; i

Difference between Method Overloading and Method Overriding in Java

Kiran Kumar Panigrahi
Updated on 13-Sep-2023 15:28:25

33K+ Views

Method Overloading in Java When a class has two or more methods by the same name but different parameters, at the time of calling based on the parameters passed respective method is called (or respective method body will be bonded with the calling line dynamically). This mechanism is known as method overloading. Example of Method Overloading If you observe the following example, here we have created a class called Sample and this class has two methods with the same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other ... Read More

What is the difference between method overloading and method hiding in Java?

varun
Updated on 30-Jul-2019 22:30:20

784 Views

method hiding − When super class and the sub class contains same methods including parameters, and if they are static and, when called, the super class method is hidden by the method of the sub class this is known as method hiding. Example Live Demo class Demo{ public static void demoMethod() { System.out.println("method of super class"); } } public class Sample extends Demo{ public static void demoMethod() { System.out.println("method of sub class"); } ... Read More

How to find Min/Max numbers in a java array?

Samual Sam
Updated on 07-Oct-2023 02:52:33

26K+ Views

You can find the minimum and maximum values of an array using for loops −ExampleLive Demopublic class MinAndMax {    public int max(int [] array) {       int max = 0;             for(int i=0; imax) {             max = array[i];          }       }       return max;    }    public int min(int [] array) {       int min = array[0];             for(int i=0; i

Is final method inherited in Java?

seetha
Updated on 30-Jul-2019 22:30:20

2K+ Views

No, we cannot override a final method in Java. The final modifier for finalizing the implementations of classes, methods, and variables. We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot modify a final method from a sub class. The main intention of making a method final would be that the content of the method should not be changed by any outsider. Example class Demo{ public final void demoMethod(){ System.out.println("Hello"); } } public class Sample extends Demo{ ... Read More

How to create and populate two-dimension Java array?

Sai Subramanyam
Updated on 16-Jun-2020 09:53:19

643 Views

A two-dimensional array in Java is represented as an array of one-dimensional arrays of the same type. Mostly, it is used to represent a table of values with rows and columns −Example Live Demopublic class Creating2DArray { public static void main(String args[]) { int[][] myArray = new int[3][3]; myArray[0][0] = 21; myArray[0][1] = 22; myArray[0][2] = 23; myArray[1][0] = 24; myArray[1][1] = 25; myArray[1][2] = 26; myArray[2][0] = 27; myArray[2][1] = 28; myArray[2][2] = 29; for(int i=0; i

How to create and populate Java array of Hash tables?

Lakshmi Srinivas
Updated on 16-Jun-2020 09:52:15

472 Views

One way to create an array of hash tables is to create Hashtable objects and to assign these to a Hashtable array using curly braces:ExampleLive Demoimport java.util.Hashtable; public class HashTableOfArrays {    public static void main(String args[]) {       Hashtable ht1 = new Hashtable();       ht1.put("Name", "Krishna");       ht1.put("Age", "28");       ht1.put("City", "Visakhapatnam");       ht1.put("Phone", "9848022338");             Hashtable ht2 = new Hashtable();       ht2.put("Name", "Raju");       ht2.put("Age", "30");       ht2.put("City", "Chennai");       ht2.put("Phone", "9848033228");     ... Read More

How to Convert a Java 8 Stream to an Array?

karthikeya Boyini
Updated on 19-Dec-2019 08:54:20

282 Views

To convert a stream to an Array in Java -Collect the stream to a list using the Collect interface and the Collectors class.Now convert the list to an array using the toArray() method.ExampleLive Demoimport java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class J8StreamToArray {    public static void main(String args[]) {       String[] myArray = { "JavaFX", "OpenCV", "WebGL", "HBase" };       Stream stream = Stream.of(myArray);       List list = stream.collect(Collectors.toList());       String[] str = list.toArray(new String[0]);       System.out.println(Arrays.toString(str));    } }Output[JavaFX, OpenCV, WebGL, HBase]

How to read data from scanner to an array in java?

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

10K+ Views

The Scanner class of the java.util package gives you methods like nextInt(), nextByte(), nextFloat() etc. to read data from keyboard. To read an element of an array uses these methods in a for loop:Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ReadingWithScanner {    public static void main(String args[]) {       Scanner s = new Scanner(System.in);       System.out.println("Enter the length of the array:");       int length = s.nextInt();       int [] myArray = new int[length];       System.out.println("Enter the elements of the array:");       for(int i=0; i

How to convert Java Array to Iterable?

V Jyothi
Updated on 16-Jun-2020 08:52:32

8K+ Views

To make an array iterable either you need to convert it to a stream or as a list using the asList() or stream() methods respectively. Then you can get an iterator for these objects using the iterator() method.ExampleLive Demoimport java.util.Arrays; import java.util.Iterator; public class ArrayToIterable {    public static void main(String args[]){       Integer[] myArray = {897, 56, 78, 90, 12, 123, 75};       Iterator iterator = Arrays.stream(myArray).iterator();       while(iterator.hasNext()) {          System.out.println(iterator.next());       }    } }Output897 56 78 90 12 123 75As mentioned above, you can also convert ... Read More

Advertisements