what happens to return value java not used
Methods are how we communicate with objects. When we invoke or call a method we are asking the object to acquit out a task. We can say methods implement the behaviour of objects. For each method we need to give a name, we demand to define its input parameters and we need to define its render blazon. We also need to set its visibility (private, protected or public). If the method throws a checked exception, that needs to be declared as well. It is called a method definition. The syntax of method definition is:
MyClass { ... public ReturnType methodName ( ParamOneType parameter1 , ParamTwoType parameter2 ) { ... return returnValue ; } ... } Nosotros tin can declare that the method does non render anything using the void Java keyword. For example:
| | Code department 3.67: Method without returned information. private void methodName ( String parameter1 , String parameter2 ) { ... return ; } |
When the method returns cypher, the return keyword at the terminate of the method is optional. When the execution menstruum reaches the return keyword, the method execution is stopped and the execution catamenia returns to the caller method. The return keyword tin can be used anywhere in the method as long equally there is a way to execute the instructions below:
| | Code section 3.68: return keyword location. private void aMethod ( int a , int b ) { int c = 0 ; if ( a > 0 ) { c = a ; render ; } int c = c + b ; return ; int c = c * ii ; } |
In the code section 3.68, the return keyword at line five is well placed considering the instructions below can be reached when a is negative or equal to 0. However, the render keyword at line 8 is badly placed because the instructions below can't be reached.
Examination your noesis
Question three.9: Consider the following code:
| | Question 3.9: Compiler error. private int myMethod ( int a , int b , boolean c ) { b = b + two ; if ( a > 0 ) { a = a + b ; return a ; } else { a = 0 ; } } |
The code above will return a compiler error. Why?
Respond
| | Answer 3.9: Compiler mistake. private int myMethod ( int a , int b , boolean c ) { b = b + 2 ; if ( a > 0 ) { a = a + b ; return a ; } else { a = 0 ; } } |
The method is supposed to return a int but when a is negative or equal to 0, it returns zip.
Parameter passing [edit | edit source]
Nosotros can pass any primitive data types or reference data type to a method.
Primitive type parameter [edit | edit source]
The primitive types are passed in by value. It means that every bit soon equally the primitive blazon is passed in, there is no more link between the value inside the method and the source variable:
| | Lawmaking department 3.69: A method modifying a variable. private void modifyValue ( int number ) { number += 1 ; } |
Every bit you can encounter in lawmaking section three.70, the modifyValue() method has not modified the value of i.
Reference type parameter [edit | edit source]
The object references are passed by value. Information technology ways that:
- There is no more link betwixt the reference inside the method and the source reference,
- The source object itself and the object itself inside the method are still the same.
You must understand the difference between the reference of an object and the object itself. An object reference is the link between a variable proper name and an case of object:
Object object ⇔ new Object()
An object reference is a pointer, an accost to the object instance.
The object itself is the value of its attributes within the object instance:
| object.firstName | ⇒ | "James" |
| object.lastName | ⇒ | "Gosling" |
| object.birthDay | ⇒ | "May 19" |
Take a wait at the example above:
| | Code department 3.71: A method modifying an object. private void modifyObject ( FirstClass anObject ) { anObject . setName ( "Susan" ); } |
The name has changed because the method has inverse the object itself and not the reference. Now take a wait at the other example:
| | Lawmaking section three.73: A method modifying an object reference. private void modifyObject ( FirstClass anObject ) { anObject = new FirstClass (); anObject . setName ( "Susan" ); } |
The name has not inverse because the method has inverse the reference and not the object itself. The behavior is the same every bit if the method was in-lined and the parameters were assigned to new variable names:
Variable argument listing [edit | edit source]
Coffee SE 5.0 added syntactic support for methods with variable argument list, which simplifies the typesafe usage of methods requiring a variable number of arguments. Less formally, these parameters are called varargs[1]. The blazon of a variable parameter must exist followed with ..., and Coffee volition box all the arguments into an assortment:
| | Lawmaking department 3.76: A method using vararg parameters. public void drawPolygon ( Point ... points ) { //… } |
When calling the method, a programmer tin can simply separate the points by commas, without having to explicitly create an array of Indicate objects. Within the method, the points can be referenced as points[0], points[ane], etc. If no points are passed, the array has a length of zero.
A method can have both normal parameters and a variable parameter but the variable parameter must always be the last parameter. For example, if the programmer is required to use a minimum number of parameters, those parameters can be specified before the variable argument:
| | Lawmaking section three.77: Variable arguments. // A polygon needs at least iii points. public void drawPolygon ( Point p1 , Betoken p2 , Point p3 , Indicate ... otherPoints ) { //… } |
Return parameter [edit | edit source]
A method may return a value (which can be a primitive type or an object reference). If the method does not return a value we use the void Java keyword.
Still, a method can return simply ane value so what if you want to return more ane value from a method? Yous can pass in an object reference to the method, and let the method modify the object properties so the modified values can be considered as an output value from the method. Yous tin also create an Object assortment inside the method, assign the return values and return the array to the caller. Even so, this gives a problem if you want to mix primitive data types and object references as the output values from the method.
There is a better approach, define a special return object with the needed return values. Create that object inside the method, assign the values and return the reference to this object. This special object is "leap" to this method and used simply for returning values, so exercise not apply a public class. The best way is to use a nested class, see example beneath:
| | Code listing iii.12: Multiple returned variables. public course MyObject { ... /** Nested object is for return values from getPersonInfoById method */ private static course ReturnObject { private int age ; individual String name ; public void setAge ( int age ) { this . age = age ; } public int getAge () { return age ; } public void setName ( Cord name ) { name = name ; } public Cord getName () { render proper noun ; } } // Finish of nested class definition /** Method using the nested form to return values */ public ReturnObject getPersonInfoById ( int id ) { int age ; String name ; ... // Get the name and age based on the ID from the database ... ReturnObject result = new ReturnObject (); result . setAge ( age ); consequence . setName ( name ); return outcome ; } } |
In the above example the getPersonInfoById method returns an object reference that contains both values of the name and the age. See beneath how you may utilize that object:
| | Lawmaking section 3.78: Retrieving the values. MyObject object = new MyObject (); MyObject . ReturnObject person = object . getPersonInfoById ( 102 ); Organisation . out . println ( "Person Proper name=" + person . getName ()); Organisation . out . println ( "Person Age =" + person . getAge ()); |
Examination your noesis
Question 3.10: Consider the post-obit code:
| | Question iii.10: Compiler error. private int myMethod ( int a , int b , String c ) { if ( a > 0 ) { c = "" ; return c ; } int b = b + 2 ; return b ; } |
The lawmaking above will return a compiler error. Why?
Answer
| | Answer 3.ten: Compiler error. private int myMethod ( int a , int b , String c ) { if ( a > 0 ) { c = "" ; return c ; } int b = b + ii ; render b ; } |
The method is supposed to return a int but at line 4, information technology returns c, which is a String.
Special method, the constructor [edit | edit source]
The constructor is a special method called automatically when an object is created with the new keyword. Constructor does non accept a return value and its name is the same as the course name. Each class must have a constructor. If we do non define one, the compiler volition create a default so called empty constructor automatically.
| | Code listing iii.13: Automatically created constructor. public course MyClass { /** * MyClass Empty Constructor */ public MyClass () { } } |
Static methods [edit | edit source]
A static method is a method that tin be called without an object instance. It can be called on the form directly. For case, the valueOf(String) method of the Integer class is a static method:
| | Code section 3.79: Static method. Integer i = Integer . valueOf ( "10" ); |
The static keyword makes attributes instance-agnostic. This means that you cannot reference a static aspect of a single object (because such a specific object attribute doesn't exist). Instead, only one instance of a static attribute exists, whether there is i object in the JVM or 1 hundred. Hither is an case of using a static aspect in a static method:
| | Code section 3.80: Static attribute. private static int count = 0 ; public static int getNewInteger () { return count ++ ; } |
You lot can notice that when yous use System.out.println(), out is a static attribute of the Organisation class. A static aspect is related to a class, not to whatsoever object instance. This is how Java achieves one universal output stream that we tin use to print output. Here is a more complex apply case:
Test your knowledge
Question 3.xi: Visit the Oracle JavaDoc of the class java.lang.Integer.
How many static fields does this class have?
Answer
4.
-
int MAX_VALUE, -
int MIN_VALUE, -
int SIZEand -
Form<Integer> Blazon.
- To learn how to overload and override a method, see Overloading Methods and Constructors.
Source: https://en.wikibooks.org/wiki/Java_Programming/Methods
0 Response to "what happens to return value java not used"
Post a Comment