Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
37 views

Lecture 4 Methods in JAVA

Uploaded by

Khunsha farooq
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)
37 views

Lecture 4 Methods in JAVA

Uploaded by

Khunsha farooq
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
You are on page 1/ 16

JAVA Programming (CST015)

Lecture 4: Methods in JAVA

Department of Computer Science and Engineering


National Institute of Technology, Srinagar, Jammu and Kashmir
March 13, 2023
Methods in JAVA
• A method is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known
as functions.
• Why to use methods? To reuse code: define the code once, and use it
many times.
Creating a Method

• A method must be declared within a class.


• It is defined with the name of the method, followed by parentheses
().
• Java provides some pre-defined methods, such as
System.out.println(),
• but you can also create your own methods to perform certain actions:
Type of Methods
1. Predefined Method:
• In Java, predefined methods are the method that is already
defined in the Java class libraries.
•Predefined Methods are also called as built-in methods.
•these methods can be used just by calling them in the program at any
point.
•Some pre-defined methods are length(), equals(), compareTo(), sqrt(),
max(), etc.
•Each and every predefined method is defined inside a class. Such as
print() method is defined in the java.io.PrintStream class. It prints the
statement that we write inside the method.
2. User-defined Method:
• The method written by the user or programmer is known as a user-
defined method.
• These methods are created according to the requirement.
Built-in methods
• main(), length(), max() are the built-in methods.
• For example, max() method is already defined in Math class.
• length() class is defined in String Class.
Creating a User-defined Method

1. Method Declaration
2. Method Definition
3. Method Calling
Creating a Method:
Method Declaration and Definition
public class Main
{
static void myMethod() // Declaration and Definition
{
// code to be executed
}
}
• myMethod() is the name of the method
• static means that the method belongs to the Main class and not an object
of the Main class.
• void means that this method does not have a return value.
Call a Method

• To call a method in Java, write the method's name followed by two


parentheses () and a semicolon;
myMethod();
A method can also be called multiple times
Method Example
public class Main {

static void myMethod()


{
System.out.println("i am inside method");
}

public static void main(String[] args) {


myMethod();
}
}
Overloading the main method

Overloading of main() method can be done in java.


We can define any number of main() method in the class, but the
method signature must be different
Overloading of main() method
class OverloadMain
{
public static void main(int a) //overloaded main method
{
System.out.println(a);
}
public static void main(String args[])
{
System.out.println("main method incoked");
main(6);
}
}
Exercise:
• WAP using functions (methods) to find the sum of two numbers.
Output?
Output?
Output?
Output?

You might also like