AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Java Methods


Java Tutorial » Java Methods

A method is a block of code that performs a specific task.

Methods are used to perform certain actions, known as functions.

Suppose we have to create a program to draw a square and color it. You can create two methods to solve this problem:

  1. the method of drawing the square,
  2. square coloring method

Syntax:

returnType my_method_Name() {
// method body
}


Create a Method, example


A method is declared within a class and defined with name of the method, followed by "()" (parentheses without quotes)

Java programs provides some pre-defined methods, like System.out.println(), but you can create your own methods to perform actions

Example: method inside Main
public class Main {
static void my_Method() {
// code java to be executed
} }


Calling Method example

In the next example, we have declared a method named multiplication() and to use this method, we need to call it.

Example:
public class Main {

// create a method
public int multiplication(int x, int y) {
int multiplication = x * y;
// return value
return multiplication;
}

public static void main(String[] args) {

int my_num1 = 12;
int my_num2 = 18;

// create an object of Main
Main obj = new Main();
// calling method
int result = obj.multiplication(my_num1, my_num2);
System.out.println("The result of multiplying the two numbers is: " + result);
}
}
Output:
The result of multiplying the two numbers is: 216


Java Method Return Type

A Java method may or may not return a value to the function call. We use the return statement to return any value.

Example:
int add_Numbers() {
...
return sum;
}

Here, we are returning the variable sum. Since the return type of the function is int. The sum variable should be of int type. Otherwise, it will generate an error.

Example:
class Main {

// create a method
public static int square(int num) {

// return statement
return num * num;

} public static void main(String[] args) {
int result;

// call the method
// store returned value to result
result = square(10);

System.out.println("Squared value of 10 is: " + result);
}
}

Output: Squared value of 10 is: 100




java methods examples, exercises, string, in interface, and parameters, witv same name, types, exercises pdf, list, for financial engineering pdf
Java Methods - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...