DEV Community

Varudhammal Abinaya
Varudhammal Abinaya

Posted on

Methods & Array

Hey everyone! In today’s post, I’m sharing what I learned about methods and arrays from my notes . Let’s dive in! 🚀

🧩 What are Methods?

A method is a reusable block of code that performs a specific task. It helps make your code modular and readable.

✅ Method Declaration:

public static void greet() {
    System.out.println("Hello!");
}
public static void main(String[] args) {
    greet(); // Method call
}
Enter fullscreen mode Exit fullscreen mode
  • public static void – standard structure for defining a method.
  • Reusability – call greet() whenever needed.

🛠️ Methods with Arguments

You can pass data to methods using parameters.

public static void add(int a, int b) {
    System.out.println(a + b);
}
public static void main(String[] args) {
    add(5, 3); // Outputs 8
}
Enter fullscreen mode Exit fullscreen mode

🔁 Return Types in Methods

  • void – no return value
  • int, boolean, String, etc. – return types
  • Use the return statement to return a value from a method.
public static int square(int x) {
    return x * x;
}
Enter fullscreen mode Exit fullscreen mode

📌 Pass by Value

Java uses pass-by-value, which means a copy of the variable is passed to the method.


🔄 Method Overloading

Method overloading lets you define multiple methods with the same name but different parameters.

public static int multiply(int a, int b) {
    return a * b;
}

public static double multiply(double a, double b) {
    return a * b;
}
Enter fullscreen mode Exit fullscreen mode

📍Java will choose the correct method based on:

  • Number of parameters
  • Data types
  • Order of parameters

🔢 Example: Fibonacci Check

Check if a number is part of the Fibonacci sequence:

public static boolean isFibonacci(int n) {
    int a = 0, b = 1;
    while (a <= n) {
        if (a == n) return true;
        int next = a + b;
        a = b;
        b = next;
    }
    return false;
}
Enter fullscreen mode Exit fullscreen mode

📦 Arrays in Java

An array is a collection of elements of the same data type.

Declaring an Array:

int[] numbers = new int[5]; // default values: 0, 0, 0, 0, 0
Enter fullscreen mode Exit fullscreen mode

Assigning Values:

numbers[0] = 10;
numbers[1] = 20;
Enter fullscreen mode Exit fullscreen mode

Or directly:

int[] nums = {1, 2, 3, 4, 5};
Enter fullscreen mode Exit fullscreen mode

Accessing Array Elements:

System.out.println(nums[2]); // Outputs 3
Enter fullscreen mode Exit fullscreen mode

Array Length:

System.out.println(nums.length); // Outputs 5
Enter fullscreen mode Exit fullscreen mode

For-Each Loop:

for (int num : nums) {
    System.out.println(num);
}
Enter fullscreen mode Exit fullscreen mode

🧪 Default Values in Arrays

When you create an array without assigning values, Java sets default values:

Data Type Default Value
int 0
float 0.0
char '\u0000'
boolean false
String null

🔁 Arrays in Methods (Example: Intersection)

You can pass arrays to methods too! Here's a simple array intersection:

public static void printIntersection(int[] arr1, int[] arr2) {
    for (int i : arr1) {
        for (int j : arr2) {
            if (i == j) {
                System.out.print(i + " ");
                break;
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Input:

int[] arr1 = {6, 2, 3, 7, 8};
int[] arr2 = {2, 3, 4, 3, 8, 9, 10};
Enter fullscreen mode Exit fullscreen mode

Output:

2 3 8 
Enter fullscreen mode Exit fullscreen mode

🧠 Final Thoughts

Today’s post covered two foundational Java concepts — methods and arrays. Understanding how they work and interact is crucial for writing clean, efficient code.

Next, I’ll be exploring what i learned . If you're also learning Java, let me know your favorite concept or where you struggled the most! Let's grow together! 🌱
Happy Learning!!!

Top comments (0)