[Go to site: main page, start]

0% found this document useful (0 votes)
10 views5 pages

Java Methods: A Comprehensive Guide

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

Java Methods: A Comprehensive Guide

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

Java Methods Tutorial Programs

Introduction
Methods in Java help us write modular, reusable, and organized code. Instead of repeating code,
we put it inside a method and call it whenever needed.

Problem Statement
Without methods, code gets repetitive. With methods, we can reuse logic easily.
// Without method (repeated code)
public class WithoutMethod {
public static void main(String[] args) {
int a = 10, b = 20;
[Link]("Sum = " + (a + b));

int x = 5, y = 15;
[Link]("Sum = " + (x + y));
}
}

// With method (reusable)


public class WithMethod {
static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
[Link]("Sum = " + sum(10, 20));
[Link]("Sum = " + sum(5, 15));
}
}

Method Syntax
General form of a method: returnType methodName(parameters) { body }
public class MethodSyntax {
static void displayMessage() {
[Link]("This is a simple method.");
}
public static void main(String[] args) {
displayMessage();
}
}

Program: Sum of Two Numbers


A simple method that returns the sum of two integers.
public class SumExample {
static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
[Link]("Sum = " + sum(10, 20));
}
}

Program: Greetings
Method with no return type (void) that just prints a greeting.
public class Greeting {
static void greet() {
[Link]("Hello! Welcome to Java.");
}
public static void main(String[] args) {
greet();
}
}

Returning Values
Methods can return values using the return statement.
public class ReturnValue {
static int getNumber() {
return 42;
}
public static void main(String[] args) {
int num = getNumber();
[Link]("Returned: " + num);
}
}

Returning a String
Methods can also return strings.
public class ReturnString {
static String greetUser(String name) {
return "Hello " + name + "!";
}
public static void main(String[] args) {
[Link](greetUser("Abhishek"));
}
}

Parameters (Integer Function)


Passing integers into a method.
public class SquareExample {
static int square(int x) {
return x * x;
}
public static void main(String[] args) {
[Link]("Square = " + square(5));
}
}

Parameters (String Function)


Passing strings into a method.
public class PrintName {
static void printName(String name) {
[Link]("Your name is: " + name);
}
public static void main(String[] args) {
printName("Abhishek");
}
}

Program: Swap Two Numbers


Demonstrates pass-by-value in Java. Original variables remain unchanged.
public class SwapExample {
static void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
[Link]("Inside method: a=" + a + ", b=" + b);
}
public static void main(String[] args) {
int x = 10, y = 20;
swap(x, y);
[Link]("Outside method: x=" + x + ", y=" + y);
}
}

Program: Pass Value


Shows that primitive values are passed as copies.
public class PassValue {
static void changeValue(int x) {
x = 99;
[Link]("Inside method: x=" + x);
}
public static void main(String[] args) {
int a = 10;
changeValue(a);
[Link]("Outside method: a=" + a);
}
}

Program: Change Value in Array


Arrays are objects, so modifications affect the original array.
public class ChangeArray {
static void change(int[] arr) {
arr[0] = 99;
}
public static void main(String[] args) {
int[] nums = {10, 20, 30};
change(nums);
[Link]("After change: " + nums[0]);
}
}

Method Scope
Variables inside a method are only accessible within that method.
public class MethodScope {
static void show() {
int x = 10;
[Link]("x = " + x);
}
public static void main(String[] args) {
show();
// [Link](x); // Error: x not visible here
}
}

Block Scope
Variables declared inside a block { } exist only inside that block.
public class BlockScope {
public static void main(String[] args) {
if (true) {
int a = 5;
[Link]("Inside block: a=" + a);
}
// [Link](a); // Error: a not visible here
}
}
Loop Scope
Variables inside a loop exist only within that loop.
public class LoopScope {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
[Link]("i=" + i);
}
// [Link](i); // Error: i not visible here
}
}

Shadowing
Local variable with the same name as class variable shadows it.
public class ShadowingExample {
static int x = 50;
public static void main(String[] args) {
[Link]("Class variable x=" + x);
int x = 20; // shadows class variable
[Link]("Local variable x=" + x);
}
}

Variable Arguments (Varargs)


Method that can take multiple arguments.
public class VarargsExample {
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) total += n;
return total;
}
public static void main(String[] args) {
[Link]("Sum = " + sum(10, 20, 30, 40));
}
}

Method Overloading
Same method name, different parameter types.
public class OverloadingExample {
static int sum(int a, int b) {
return a + b;
}
static double sum(double a, double b) {
return a + b;
}
public static void main(String[] args) {
[Link](sum(10, 20));
[Link](sum(10.5, 20.5));
}
}

Q1: Prime Number


Check if a number is prime.
public class PrimeCheck {
static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i <= [Link](n); i++) {
if (n % i == 0) return false;
}
return true;
}
public static void main(String[] args) {
[Link](isPrime(29) ? "Prime" : "Not Prime");
}
}

Q2: Armstrong Number


Check if a number is an Armstrong number.
public class ArmstrongCheck {
static boolean isArmstrong(int n) {
int sum = 0, temp = n;
while (n > 0) {
int digit = n % 10;
sum += digit * digit * digit;
n /= 10;
}
return sum == temp;
}
public static void main(String[] args) {
[Link](isArmstrong(153) ? "Armstrong" : "Not Armstrong");
}
}

Q3: Print All 3-digit Armstrong Numbers


Prints all Armstrong numbers between 100 and 999.
public class ArmstrongNumbers {
static boolean isArmstrong(int n) {
int sum = 0, temp = n;
while (n > 0) {
int digit = n % 10;
sum += digit * digit * digit;
n /= 10;
}
return sum == temp;
}
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
if (isArmstrong(i)) {
[Link](i);
}
}
}
}

Outro
Summary: Methods help write modular code. We saw parameters, return types, scope, varargs,
overloading, and practice problems like Prime and Armstrong numbers.

Common questions

Powered by AI

Java uses pass-by-value for parameter passing, which means the method receives a copy of the argument. For primitive types, such as integers, changes to parameters within the method do not affect the original variable. For example, in `static void changeValue(int x) { x = 99; }`, calling `changeValue(a)` does not alter the variable `a` since the method uses a copy of `a` . However, for reference types like arrays, changes affect the original object. In `static void change(int[] arr) { arr[0] = 99; }`, modifying `arr` alters the original array because the reference itself is copied, not the array content .

In Java's pass-by-value strategy, the invoked method operates on a copy of the actual arguments, meaning any changes to provided parameter values within the method do not affect the originals. For instance, in a `swap` method: `static void swap(int a, int b) { int temp = a; a = b; b = temp; }`, swapping `x` and `y` won't affect the original variables outside of the method because a copy was swapped, not the originals . Conversely, in pass-by-reference (not in Java), the method manipulates the actual storage location or object, allowing for direct modifications outside its scope, thus effectively executing a swap. This highlights Java's difficulty implementing an in-place swap for primitives without additional wrappers or constructs.

Method overloading in Java allows multiple methods with the same name but different parameter lists within a class. This enables performing similar operations with different types or numbers of inputs. For example, you could have a `sum` method overloaded to handle both integers and doubles: `static int sum(int a, int b) { return a + b; }` and `static double sum(double a, double b) { return a + b; }`. These overloaded methods allow you to call `sum(10, 20)` for integers or `sum(10.5, 20.5)` for doubles, making the API more flexible for different data types .

Methods in Java help avoid code repetition by encapsulating reusable blocks of code that can be invoked whenever needed. For instance, consider the example of calculating the sum of two numbers. Without methods, you'd write the code separately each time: `int a = 10, b = 20; System.out.println("Sum = " + (a + b));` and then again for another set of numbers. With methods, you encapsulate the sum logic in a reusable `sum` method: `static int sum(int a, int b) { return a + b; }`. This allows for cleaner and more maintainable code, such as `System.out.println("Sum = " + sum(10, 20));` .

Utilizing return types in Java methods significantly enhances code modularity and efficiency by enabling methods to encapsulate logic and produce reusable output that can be directly utilized in further computations or conditions. Return types increase modularity by structuring programs into distinct units with specific outputs that can be combined easily, such as using `static int getNumber() { return 42; }` to return a value for further use. They also improve efficiency by eliminating the need to recompute values or logic across different parts of a program, thereby reducing redundancy .

To identify Armstrong numbers, compute the sum of the cubes of its digits and check if this sum equals the original number. For instance, in `static boolean isArmstrong(int n) { int sum = 0, temp = n; while (n > 0) { int digit = n % 10; sum += digit * digit * digit; n /= 10; } return sum == temp; }`, an Armstrong number like `153` satisfies this condition because `1^3 + 5^3 + 3^3 = 153`. These numbers are mathematically significant due to their unique property that relates individual digits' powers to aspect of number identity, often studied in number theory .

Variable and method scope in Java enhance encapsulation by limiting the accessibility of variables to specific sections of code, thus contributing to code safety and reducing the chance of unintended interactions. Variables declared within a method are not accessible outside of that method, as shown in `public class MethodScope { static void show() { int x = 10; } }`. Outside calls to `x` result in compile-time errors, ensuring `x` isn't mistakenly modified. Similarly, block scope confines variable access within control structure blocks `{ }`, like in `if` or `for` loops, which further prevents unwanted access .

Modular code structures, enabled by Java methods, streamline debugging and maintenance by isolating functionality into independent blocks. This separation allows developers to test and debug individual methods without affecting the overall application, facilitating pinpointed troubleshooting. Maintenance becomes simpler as updates to methods can be conducted without inadvertently impacting other program sections, enhancing reliability and readability. As each method encapsulates functionality, understanding and documenting code is more straightforward, fostering long-term software sustainability .

Variable arguments (varargs) in Java allow a method to accept zero or more arguments of a specified type, providing flexibility in passing different numbers of parameters without overloading multiple methods. This is helpful when the exact count of arguments is unknown or varies. A method like `static int sum(int... numbers) { int total = 0; for (int n : numbers) total += n; return total; }` can be called both as `sum(10, 20, 30)` and `sum(10, 20, 30, 40, 50)`, efficiently computing the sum across varying argument counts .

Local variable shadowing occurs when a local variable within a block or method has the same name as a variable outside the block or method, effectively "hiding" the outer variable. For example: `public class ShadowingExample { static int x = 50; public static void main(String[] args) { int x = 20; } }`. The local variable `x` inside `main` shadows the class variable `x`, so `System.out.println(x)` would print `20`, not `50`. Shadowing can lead to confusion or errors if the developer expects the outer variable to be accessed, highlighting the need for clear and distinct variable naming .

You might also like