• +1-617-874-1011 (US)
  • +44-117-230-1145 (UK)
Live Chat
Follow Us:

Java Methods Homework Help

Java Methods:

In java method is a collection of statements it is used to perform some operation. Depends on user needs they create own method with argument or without argument and with return type or without return type.

Method creation:

Syntax:

Modifier returntype methodname( argumentlist)

{

Body of the content

}

Example:

Public static void add( int a, int b )
{
Int c;
c= a+b;
}
  • Public, static – modifiers
  • Void – return type (nothing return)
  • Add – method name
  • Int a, int b – argument list

Method calling:

There are two ways to call the method that is with return type or without return type.

Syntax;

If it have return type then

Datatype variable = methodname(arguments);

If no return type means

Methodname(arguments);

Passing parameters by value:

Example:

In this example using pass by value method will executed.

public class sample
 {
 public static void main(String[] args)
 {
 int x = 20;
 int y = 15;
int z;
 // Invoke the method
 Z=add(x, y);
 System.out.println("addition value is :" + z);
 }
 public static int add(int x, int y)
 {
 // add x with y
int a;
a=x+y;
return a;
 }
}

Output

Addition value is : 35