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
}
-
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
}
🔁 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;
}
📌 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;
}
📍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;
}
📦 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
Assigning Values:
numbers[0] = 10;
numbers[1] = 20;
Or directly:
int[] nums = {1, 2, 3, 4, 5};
Accessing Array Elements:
System.out.println(nums[2]); // Outputs 3
Array Length:
System.out.println(nums.length); // Outputs 5
For-Each Loop:
for (int num : nums) {
System.out.println(num);
}
🧪 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;
}
}
}
}
Input:
int[] arr1 = {6, 2, 3, 7, 8};
int[] arr2 = {2, 3, 4, 3, 8, 9, 10};
Output:
2 3 8
🧠 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)