Message PassingFrom WikiJavabuy 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.
commentsIn 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 parametersMethods 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 parametersMethods with parameters can be declared as follows: private void printString(String stringValue) { System.out.println("the string is : " + stringValue); } The method // void method with one parameter printString(stringValue);
System.out.println("The string is " + count + " characters long"); Note that 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 valuesMultiple 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 codepackage 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"); } } |
