Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 2

Value methods

The value method is a method that returns a value.

The syntax of the value methods is similar to the void type methods:

 public static returnValueType myMethod( list_of_parameters ) {
   //statements;
   return value;
 }

Unlike void methods, in the return type methods :

  1. the method header contains the data type of the value (returnValueType) to be returned;
  2. the last statement of the method body contains a keyword return followed by the value to be returned.

Note: the return value of the method must correspond to the return value type defined in the method header!

Example of the return type method definition and call:

public class ThreeNumbers {
    public static double multiplyThreeNumbers(double a, double b, double c){
        return a*b*c;
    }

    public static void display(double a, double b, double c){
        System.out.println("Numbers: " + a + ", " + b + ", " + c);
    }

    public static void main(String[] args) {
        double x = 1.5;
        double y = 2.25;
        double z = 3;
        display(x, y, z);
        System.out.println("The result of multiplication: " + multiplyThreeNumbers(x, y, z));
    }
}

Self-assessment

Session 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment