Found 34472 Articles for Programming

How to pass Arrays to Methods in Java?

Samual Sam
Updated on 02-Sep-2023 13:52:53

56K+ Views

You can pass arrays to a method just like normal variables. When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.Suppose we have two methods min() and max() which accepts an array and these methods calculates the minimum and maximum values of the given array respectively:Example Live Demoimport java.util.Scanner; public class ArraysToMethod {    public int max(int [] array) {       int max = 0;       for(int i=0; imax) { ... Read More

How to create array of strings in Java?

Sai Subramanyam
Updated on 19-Dec-2019 10:13:26

2K+ Views

In Java, you can create an array just like an object using the new keyword. The syntax of creating an array in Java using new keyword −type[] reference = new type[10];Where, type is the data type of the elements of the array.reference is the reference that holds the array.And, if you want to populate the array by assigning values to all the elements one by one using the index −reference [0] = value1; reference [1] = value2;You can declare an array of Strings using the new keyword as &mius;String[] str = new String[5]; And then, you can populate the string ... Read More

How to return an array from a method in Java?

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

5K+ Views

We can return an array in Java from a method in Java. Here we have a method createArray() from which we create an array dynamically by taking values from the user and return the created array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class ReturningAnArray {    public int[] createArray() {       Scanner sc = new Scanner(System.in);       System.out.println("Enter the size of the array that is to be created:: ");       int size = sc.nextInt();       int[] myArray = new int[size];       System.out.println("Enter the elements of the array ::");       for(int i=0; i

How to use for each loop through an array in Java?

Syed Javed
Updated on 12-Mar-2020 10:20:25

249 Views

JDK 1.5 introduced a new for loop known as foreach loop or enhanced for loop, which enables you to traverse the complete array sequentially without using an index variable.ExampleLive Demopublic class ArrayUsingForEach {    public static void main(String[] args) {       double[] myList = {1.9, 2.9, 3.4, 3.5};       for (double element: myList) {          System.out.println(element);       }    } }Output1.9 2.9 3.4 3.5

What is Is-a relationship in Java?

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

969 Views

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.Examplepublic class Animal { } public class Mammal extends Animal { } public class Reptile extends Animal { } public class Dog extends Mammal { }Now, based on the above example, in Object-Oriented terms, the following are true −Animal is the superclass of Mammal class.Animal is the superclass of Reptile class.Mammal and Reptile are subclasses of Animal class.Dog is the subclass of both Mammal and Animal classes.Example Live Democlass Animal { } class Mammal extends Animal ... Read More

How to loop through an array in Java?

karthikeya Boyini
Updated on 26-Feb-2020 10:12:36

416 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

What is the difference between compile time polymorphism and runtime polymorphism in java?

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

932 Views

If we perform (achieve) method overriding and method overloading using instance methods, it is run time (dynamic) polymorphism. In dynamic polymorphism the binding between the method call an the method body happens at the time of execution and, this binding is known as dynamic binding or late binding. Example Live Demo class SuperClass{ public static void sample(){ System.out.println("Method of the super class"); } } public class RuntimePolymorphism extends SuperClass { public static void sample(){ System.out.println("Method of the ... Read More

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

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

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

815 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

27K+ 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

Advertisements