Using Predefined Methods in Java
Predefined methods in Java are the methods that are already written, tested, and stored in the
Java Class Library. These methods are available for programmers to use directly without writing
their own code for common tasks. They make Java programming faster, easier, and more
reliable.
Java organizes its predefined methods inside different packages, such as [Link], [Link], and
[Link]. Each package contains classes, and each class contains several predefined methods. For
example, the Math class contains methods like pow(), sqrt(), abs(), max(), and min(), which
perform various mathematical calculations.
Using a predefined method is similar to using a function in algebra. A method has a name, it
takes parameters (inputs), performs some computation, and returns a value. For instance,
[Link](2, 3) calculates 2³ and returns 8.0, while [Link](25) returns the square root of 25.
Predefined methods save time because the programmer does not need to write code for
routine operations. They also reduce errors, since the methods are part of Java’s standard
library and are already optimized and thoroughly tested. Some predefined methods are called
using the class name (for example, [Link]()), while others require creating an object first. For
example, the Scanner class has predefined methods like nextInt(), nextLine(), and nextDouble()
for taking user input.
The package [Link] is automatically imported in every Java program, so predefined methods
like those in the Math and String classes can be used without writing an import statement. This
makes it even more convenient to work with methods like length(), charAt(), and toUpperCase()
in String processing.
In short, predefined methods are a powerful feature of Java that help in writing clean, short,
and efficient code by providing ready-made solutions for mathematical operations, input
handling, string manipulation, and much more.
User-Defined Methods in Java (Clear
Explanation)
Java provides many predefined methods, but it cannot include every method a programmer
might need. Different programs require different types of tasks, so Java allows programmers to
create their own methods. These are called user-defined methods. They help make programs
easier to manage, reduce repetition, and improve readability.
User-defined methods in Java are mainly divided into two categories:
Value-Returning Methods in Java
A value-returning method in Java is a method that performs some work and then gives back a
result to the main program. The result it returns can be an integer, decimal, text, or any other
data type. Because it returns a value, the method must have a return type like int, double,
boolean, or String.
Inside the method, a return statement is used to send the result back. Once the return
statement runs, the method stops working and the returned value can be stored in a variable or
used inside a calculation.
Value-returning methods usually take some parameters, which are the inputs the method
needs to do its job. For example, a method that calculates the square of a number needs one
parameter. These methods are very useful because they help us avoid writing the same code
again and again. They also make programs easier to read and understand.
In simple words, a value-returning method is like a machine: you give it some input, it does
some work, and it returns an answer. This answer can then be used anywhere in the program.
What is a Return Statement?
The return statement in Java is used to send a value back from a method to the part of the
program that called it. It is like giving the answer of a calculation or operation from the method
to the main program.
Where is it Used?
It is mainly used in value-returning methods, which are methods that produce a result.
It can also be used in void methods to stop execution early, but in void methods it does
not return a value.
3. Purpose of Return Statement
To return a result from a method.
To end the execution of the method immediately.
5. Rules of Return Statement
1. The type of the value returned must match the method’s return type.
o Example: if the method returns int, you must return an integer.
2. A method that has a return type must have at least one return statement.
3. After executing the return statement, the method stops immediately, and no further
code in the method is executed.
4. In a void method, return; can be used to exit the method early but it does not return a
value.
Key Points to Remember
5. The return statement must match the method’s return type.
6. Value-returning methods always return a value.
7. Void methods can use return; to exit early, but cannot return a value.
8. After the return statement, no further code in the method is executed.
9. It allows methods to send results back to the calling code for further use.
10.
Flow of Execution in Java
The flow of execution refers to the exact order in which Java runs the statements of a program.
Understanding this flow is important because it helps us know how the program behaves, how
methods interact, and how decisions and loops change the way a program runs.
Step
. A Java program has many classes and methods.
But the program always starts from the main() method — no matter where it is written.
2. Java begins execution from the first line inside main().
It runs the code line by line, from top to bottom.
3. Other methods do NOT run automatically.
They run only when you call them from the main method (or another method).
4. When a method is called:
Java jumps to that method.
Executes all statements inside that method.
5. After the method finishes:
Java returns to the line right after the method call.
**6. For value-returning methods:
When the method returns a value:
That value replaces the method call.
The program continues from the next statement.
7. Program ends when main() finishes executing.
Void Method
1. Definition
A void method is a method in Java that does not return any value to the part of
the program that called it.
The keyword void is used as the method’s return type to indicate that the method
performs some action but does not give back a result.
. Purpose of Void Methods
Void methods are used when:
You want the method to perform a task, not calculate and return a result.
The output is shown directly on the screen.
The method updates something or performs steps, but the caller does not
need a value.
Examples of tasks:
Printing text
Displaying results
Taking input
Showing messages
Performing procedures (steps)
How Void Methods Work
When a void method is called:
1. The program jumps from the calling statement to the first line inside the
method.
2. It executes all statements inside the method.
3. After finishing, the control returns to the line immediately after the method
call.
4. No value is returned to replace the method call, because the method
returns nothing.
Syntax of a Void Method
A void method is defined using the keyword void.
Example:
void display() {
[Link]("Welcome!");
}
Here:
void → return type
display → method name
{ } → method body
Calling a Void Method
You call it simply by its name:
display();
Since it does not return a value:
You cannot assign it to a variable
You cannot use it in an expression
Example of incorrect usage:
int x = display(); // Not allowed
6. Return Statement in Void Methods
A void method cannot return a value, but it can use the return statement without
a value to exit early.
Example:
void checkNumber(int n) {
if (n < 0) {
[Link]("Negative number");
return; // exits the method early
}
[Link]("Positive number");
}
Here, return; only stops the method, but does not return a value.
7. Why Use Void Methods?
Void methods are helpful when:
The method performs an action, not a calculation.
The result does not need to be sent back to the caller.
You want to reuse code for printing or showing messages.
You want to keep your program organized.
Examples:
Displaying menus
Printing tables
Showing results
Processing data and printing output