Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

JAVA Methods

JAVA Methods

Table of Contents

  • JAVA Methods
    • Calling a Method
    • Returning Multiple values
    • Return Object from JAVA Methods
  • Parameter Vs. Argument in a Method
    • Example of call-by-value

JAVA Methods

JAVA Methods

A function defined in another programming language is analogous to a Java methods. A method explains how an object behaves. A method is made up of several statements that work together to complete an action.

For example, if we have a class Human, then this class should have methods like eating(), walking(), talking() etc, which describes the behavior of the object. Declaring method is similar to function. See the syntax to declare the JAVA Methods.


return-type methodName(parameter-list)
{
 //body of method
}

return-type refers to the type of value returned by the method.

methodName is a valid meaningful name that represent name of a method.

parameter-list represents list of parameters accepted by this method.

Method may have an optional return statement that is used to return value to the caller function.


Example of a JAVA Methods:

Lets understand the method by simple example that takes a parameter and returns a string value.

public String getName(String st)
{
 String name="onlineexamguide";
 name=name+st;
 return name;
}

Modifier : Modifier are access type of method. We will discuss it in detail later.

Return Type : A method may return value. Data type of value return by a method is declare in method heading.

Method name : Actual name of the method.

Parameter : Value passed to a method.

Method body : collection of statement that defines what method does.


Calling a Method

To carry out the functionality built into it, methods are called. By using the method’s name, we can invoke it and assign the result to a variable.

String val = GetName(".com")

It will return a value onlineexamguide.com after appending the argument passed during method call.


Returning Multiple values

Using an array in Java, we may have a method return many results. We put all the values we want to return into an array, which we then send back to the calling method. When creating an array, we must specify the return-type as an array. Here is an illustration.

Example:

So Below is an example in which we return an array that holds multiple values.


class MethodDemo2{ 
    static int[] total(int a, int b) 
    { 
int[] s = new int[2]; 
s[0] = a + b; 
s[1] = a - b; 
        return s; 
    } 

    public static void main(String[] args) 
    { 
int[] s = total(200, 70); 
System.out.println("Addition = " + s[0]); 
System.out.println("Subtraction = " + s[1]); 
    } 
}
  

Return Object from JAVA Methods

In some circumstances, it could be necessary to return a class object to the calling function. So In this scenario, the method definition must include the class name. The example that follows shows how to obtain an object from a method call. It can also be used to retrieve data collecting.

Example:

So In this example, we created a method get() that returns object of Demo class.


class Demo{
int a; 
    double b; 
int c; 
Demo(int m, double d, int a) 
    { 
        a = m; 
        b = d; 
        c = a; 
    } 
} 
class MethodDemo4{ 
    static Demo get(int x, int y) 
    { 
        return new Demo(x * y, (double)x / y, (x + y)); 
    } 
    public static void main(String[] args) 
    { 
        Demo ans = get(25, 5); 
System.out.println("Multiplication = " + ans.a); 
System.out.println("Division = " + ans.b); 
System.out.println("Addition = " + ans.c); 
    } 
}
  

Parameter Vs. Argument in a Method

It’s crucial to understand the distinction between the terms parameter and argument while discussing methods. A method-defined variable called a parameter receives a value when the method is called. Parameters never have a scope outside of the method; they are always local to the method. When a method is called, a value is supplied to it as the while argument.

You can understand it by the below image that explain parameter and argument using a program example.


call-by-value and call-by-reference

There are two ways to pass an argument to a method

  1. call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments.
  2. call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the argument value.

NOTE :However there is no concept of call-by-reference in Java. Java supports only call by value.


Example of call-by-value

So Lets see an example in which we are passing argument to a method and modifying its value.

public class Test
{
    public void callByValue(int x)
    {
        x=100;
    }
    public static void main(String[] args)
    {
       int x=50;
        Test t = new Test();
        t.callByValue(x);	//function call
        System.out.println(x);
    }

}

50

So See, in the above example, value passed to the method does not change even after modified in the method. So It shows that changes made to the value was local and argument was passed as call-by-value.

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.