Java Programming Tutorial Index

Miscellaneous

Java is one of the popular general-purpose programming languages which is concurrent,  have classes & objects and also provide codes to set up in modules so that some codes can be re-used and specifically intended to let your applications "write once, run anywhere" (WORA) feature.



Methods in Java

Java methods are an assortment of statements clustered together for performing an operation. When you call any pre-defined function such as toString(), a series of logical stepwise set of codes run in the background which is already stored and made ready in the library. In other words, methods in Java are containers in which you can put, for different operations on different data (which are variables) to carry out a specific task in your Java code. Also, you can group Java operations providing a name which will be the name of the method.

It is to be noted that Java methods must have to be located within a Java class. Java methods are similar to what you call functions and procedures in languages like JavaScript, C, Pascal, etc.). A Java method is a single or a collection of Java statement(s) performing some action or tasks on some data (variables) which may or may not return any end result.

Types of Methods

Methods can be of two broad categories. These are:

  • Standard Library Methods
  • User-defined Methods

These classifications are being made based on whether the Java method is defined by the programmer or available and pre-existing in Java's standard library or additional libraries.

Some examples of Standard libraries are:

  • print() method comes under java.io.PrintSteam which prints the string that is written within the quotation.
  • sqrt() is a method of Math class which returns the square root of that specific number.

The syntax for Creating methods in Java

Syntax:

modifier returnType NameofMethod(Parameter List) {
 // method body
}

Example:

public static int funie(int g, int d) {
 // body of the method
}

Here, public static are modifiers where: public sets the visibility of the method, statics used so that this method is shared among all instances of the class it belongs to; int specifies the type of value method will return, i.e. the return type; funie is the name of method used here, g and d is the formal parameters' list of type integer.

Calling Methods in Java

Methods won't be going to perform anything until and unless you call them on a specific part of your program to do some action. In other words, to use method(s), programmers should call them by the method name. Two approaches are there to call a method. One, where a method returns a value and the other where methods do not return anything (i.e., no return value). Here are two examples shows two different ways of calling methods:

  • Ex: System.out.println("Hello, Methods, I do not return anything");
  • int output = fun(2, 6);

Example:

public class Example {
 public static void main(String argu[]) {
  int val1 = 62;
  int val2 = 8;
  int res = fun(val1, val2);
  System.out.println("Result is: " + res);
 }

 public static int fun(int g1, int g2) {
  int ans;
  ans = g1 + g2;
  return ans;
 }
}

Example:

Result is: 70

Passing Parameters

Parameters can be passed in two ways: by value or by reference. In this section, you will study about this:

Passing Parameters by Value

In the above example, you have seen the arguments having values are passed the unction. These arguments must have to be in the same sequence as their respective parameters defined in method specification. Passing parameters using value(s) means calling a method with the help of parameter.

Example:

public void swapping(int s1, int s2) {
 int temp = s1;
 s1 = s2;
 s2 = temp;
}

Alternatively other than pass by value, you can use pass by reference which represents the aliasing of data and changes done in aliased value gets reflected your original value. In case of passing by reference, any changes done on a value within different scope gets preserved even when the scope is exited.

Example:

public class AnotherExample {
 public void printOut(StringBuffer str2) {
  str2.append("Reference");
  System.out.println(str2); // prints passBy reference
 }
 public static void main(String argu[]) {
  AnotherExample ae = new AnotherExample();
  StringBuffer str1 = new StringBuffer("passBy ");
  ae.printOut(str1);
  System.out.println(str1); // prints passBy reference
 }
}

Value of reference-object in str1 is passed to str2. Now, the reference value of str1 and str2 refers to the same object.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram