0% found this document useful (0 votes)
18 views9 pages

Java Method Overloading and Arrays

Uploaded by

Neha.Kale K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views9 pages

Java Method Overloading and Arrays

Uploaded by

Neha.Kale K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Method Overloading

With method overloading, multiple methods can have the same name
with different parameters:
int myMethod(int x)
float myMethod(float x)
double myMethod(double x, double y)
static int plusMethodInt(int x, int y) {
return x + y;
}

static double plusMethodDouble(double x, double y) {


return x + y;
}

public static void main(String[] args) {


int myNum1 = plusMethodInt(8, 5);
double myNum2 = plusMethodDouble(4.3, 6.26);
[Link]("int: " + myNum1);
[Link]("double: " + myNum2);
}
static int plusMethod(int x, int y) {
return x + y;
}

static double plusMethod(double x, double y) {


return x + y;
}

public static void main(String[] args) {


int myNum1 = plusMethod(8, 5);
double myNum2 = plusMethod(4.3, 6.26);
[Link]("int: " + myNum1);
[Link]("double: " + myNum2);
}
Array
Create an ArrayList object called cars that will store strings:
import [Link]; // import the ArrayList class
ArrayList<String> cars = new ArrayList<String>(); // Create an ArrayList object
import [Link];
import [Link]; // Import the Collections class
public class Main {
public static void main(String[] args) {ArrayList<Integer> myNumbers = new ArrayList<Integer>();
[Link](33);
[Link](15);
[Link](20);
[Link](34);
[Link](8);
[Link](12);
[Link](myNumbers); // Sort myNumbers
for (int i : myNumbers) {
[Link](i);
}
}
}
// Java Program to find maximum in arr[]
// Driver Class
class Test
{
// array declared
static int arr[] = {10, 324, 45, 90, 9808};

// Method to find maximum in arr[]


static int largest()
{
int i;
// Initialize maximum element
int max = arr[0];
// Traverse array elements from second and
// compare every element with current max
for (i = 1; i < [Link]; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
// Driver method
public static void main(String[] args)
{
[Link]("Largest in given array is " + largest());
}
}
// Java Program to find the if the arrays are equal
import [Link];
public class CheckArraysEqual {
public static void main(String[] args)
{
// Initializing the first array
int a[] = { 30, 25, 40 };

// Initializing the second array


int b[] = { 30, 25, 40 };
// store the result
// [Link](a, b) function is used to check
// whether two arrays are equal or not
boolean result = [Link](a, b);

// condition to check whether the


// result is true or false
if (result == true) {
// Print the result
[Link]("Two arrays are equal");
}
else {
// Print the result
[Link]("Two arrays are not equal");
}
}
}

You might also like