banner image
Sedang Dalam Perbaikan

Java Methods(Functions)



Java is one of the most used languages in the world. Java is everywhere..So I thought to write on specific areas in Java language. Rather than going from start as usual, I'm going to touch some areas and do some experiments on them. As a start, I will do this article on Java functions/methods. Methods is a very common topic in any programming language. They are very important in our coding since they are reusable. Mainly I discuss two types of methods.
  1. Basic Methods
  2. Para-metered Methods  

First I need to introduce the structure of a java method.



In the above picture I have included a method with parameters. It's easy to understand the components in a method.

We always need to remember there's a main method in every java program. When we need to call a method we do it through this main method. How to call the above function?
My file name is Demo.java. So I should declare a class for the program with the name same as the file name. Then main method will execute  to get the result.

In the main method, you can see the method has been called and assigned to an integer called mySum. Then mySum is printed in the next line.

Basic Methods

This means the methods that not having parameters. They do not take parameters into the method and only prints something. These methods do not return anything as the method shown in the above picture. Return type of a this kind of a method is void

Example:
Imagine that I need to print my name using a method, to the console. That means this method return nothing but prints my name. Then the printing statement is executed which is included in the printName method. 

Basic void method


This method can be implemented in another way. The above method is not having a return value. But we can do it returning a value also. Then the return type of the method is changed from void to int/double/float/long. Remember One thing! When you change the return type of the method from void to any other type, you must return a value which has the same return type that the method has

Basic int method



Methods with parameters

You may have seen some methods/functions that takes parameters. I'm going to talk about these methods. This kind of a method can take one or more parameters and give the result according to the relationships built using the parameters, within the method body. Para-metered-methods always have a return type. It may be int or double or any primitive data type. Here I will use methods with return type int for my ease.


class Demo{
// method definition
public static int findTotal(int x, int y) {
int total = x + y;
return total;
}
public static void main(String args[]){
// method call and print
System.out.println("Total : " + findTotal(2,3) );
}
}

Examples:

1. Find the sum of two integers entered by keyboard

import java.util.*;
class Demo{
//Method Declaration
public static int findTotal(int x, int y){
int total=x+y;
return total;
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Input number 1 : ");
int num1=input.nextInt();
System.out.print("Input number 2 : ");
int num2=input.nextInt();
//Call method
int total=findTotal(num1,num2);
System.out.println(num1+" + "+num2+" = "+total);
}
}

2. Find the number of digits in a number entered by keyboard

import java.util.*;
class Demo{
//Method Declaration
public static void printDigitsCount(){
Scanner input=new Scanner(System.in);

System.out.print("Input an integer : ");
int num=input.nextInt();

//find the digits
int digitCount=0;
do{
num=num/10;
digitCount++;
}while(num!=0);
System.out.println("No of digits : "+digitCount);

}
public static void main(String args[]){
//Call method
printDigitsCount();
}
}

3. Find the absolute value of a number entered by keyboard

class Demo{
//Method Declaration
public static void printAbs(int num){
int abs;
if(num<0 :="" abs="" args="" bsolute="" code="" else="" is="" main="" num="" of="" printabs="" public="" static="" system.out.println="" tring="" value="" void="">

4. Find the factorials of a number series

class Demo{
//Method Declaration
public static void printFactorial(int num){
int fact=1;
for(int i=num;i>0;i--){
fact*=i;
}
System.out.println(num+" ! : "+fact);
}
public static void main(String args[]){
for(int i=0; i<10 code="" i="" printfactorial="">


This is the end of Java methods tutorials. We will meet with another Java tutorial later!
Good Bye!






Java Methods(Functions) Java Methods(Functions) Reviewed by DAL on February 26, 2018 Rating: 5

No comments:

Powered by Blogger.