Thursday, 13th October 2011
Follow WikiJava on twitter now. @Wikijava

Message Passing

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

This article shows how to pass messages to methods in Java. This is a fundamental functionality in every language and it's absolutely necessary to know exactly how to pass values to methods, in order to be able to compute data properly.

All objects in Java are passed via reference, while all the primitive types are passed via value.

Contents

comments

In programming in general the expression "passing messages" may mean many very different concepts. For example it can mean to send a message to some kind of network server. It may mean to communicate with a soap service and many other concepts.

In this context by passing a message we mean to call a java method and pass to it some arguments (messages) that it will use for performing it's processing. We mean also retrieving from the method the result of the processing.

Passing a message to a method is called passing arguments or passing parameters, while retrieving a message containing the result of the processing is called retrieving the result value.

In this context the arguments and the result value are always either objects or primitive types. Wether one of the other it makes no difference, as these values can be used normally, just like any other object or primitive type.

It's important to remember that Objects in Java are always passed as reference, while primitive types are always passed as value.

This means that if you modify the object received as parameter in a method, the change will be visible (and affect) also the calling method, and any other piece of code working on it.


calling without parameters

Methods that don't require parameters can be defined like follows:

private void run() {

the previous method can be called with:

messages.run();

One more example:

// call with no parameters
salute();
private void salute() {
	System.out.println("Hello World");
}

callling with parameters

Methods with parameters can be declared as follows:

private void printString(String stringValue) {
	System.out.println("the string is : " + stringValue);
}

The method printString accepts as input parameter a String value, that can be used inside the method by referencing to the variable stringValue. The method doesn't return any value.

	// void method with one parameter
	printString(stringValue);

System.out.println is an example of Api Method that takes only one parameter.

System.out.println("The string is " + count + " characters long");

Note that "The string is " + count + " characters long" is in facts only one parameter, as count is substituted by it's value and the "+" concatenate the two strings and the int

Calling with parameters and return value

	// int method with one parameter
	int count = count(stringValue);
// int method without keeping the returned value
count(stringValue);
private int count(String stringValue) {
	int result = stringValue.length();
	return result;
}


calling a method with multiple values

Multiple parameters can just be separated by commas, like in the following example:

	count = countFrom(stringValue, intValue);

The definition goes in the same way:

private int countFrom(String stringValue, int intValue) {
	int result = stringValue.substring(intValue).length();
	return result;
}

One more example:

	String createdString = createString('a', 5);


private String createString(char c, int num) {
	String out = "";
 
	//...
 
	return out;
}

The code

package org.wikijava.basics;
 
public class PassingMessages {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
	PassingMessages messages = new PassingMessages();
 
	messages.run();
    }
 
    private void run() {
	// call with no parameters
	salute();
 
	String stringValue = "bladibla";
 
	// void method with one parameter
	printString(stringValue);
 
	// int method with one parameter
	int count = count(stringValue);
	System.out.println("The string is " + count + " characters long");
 
	// int method without keeping the returned value
	count(stringValue);
	System.out.println("The length was measured, but not kept");
 
	int intValue = 3;
	count = countFrom(stringValue, intValue);
	System.out.println("the substring contains " + count + " characters");
 
	String createdString = createString('a', 5);
	System.out.println("the string created is " + createdString);
 
    }
 
    private String createString(char c, int num) {
	String out = "";
 
	for (int i = 0; i < num; i++) {
	    out += c;
	}
 
	return out;
    }
 
    private int countFrom(String stringValue, int intValue) {
	int result = stringValue.substring(intValue).length();
	return result;
    }
 
    private int count(String stringValue) {
	int result = stringValue.length();
	return result;
    }
 
    private void printString(String stringValue) {
	System.out.println("the string is : " + stringValue);
 
    }
 
    private void salute() {
	System.out.println("Hello World");
    }
 
}

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.