Until now, we have dealt with methods (=functions) of type void mostly. A void method is basically a block of code that is encapsuled in {} brackets and can be called by it's name, like this:
void functionName()
{
do something
}
Whenever you call the function in your program: funcionName(); - the code inside the function, here: do something, is executed. A void method (or function) can also have parameters passed to it, of course.
You will often use functions which are not of type void, but of type int, float, boolean or whatever instead. See the following example:
int addNumbers(int x, int y)
{
int z=x+y;
return z;
}
Here, you return the sum of the two passed integer numbers in a function, so you can call it like this: a=addNumbers(4,5); and get a=9 as a result. Instead of defining a new variable, you can just as well return the result of the calculation directly:
int addNumbers(int x, int y)
{
return x+y;
}
Here is another example:
boolean compareNumbers(int x, int y)
{
if(x<y)
return true;
else
return false;
}
This function returns true, if the first argument passed is less than the second one, and false otherwise.
Here's another one:
String testNumber(int x)
{
if(x<100)
return "The number is less than 100";
else
return "The number is greater or equal 100";
}
In the two last examples above, you can just as well omit the word else, because the last return is valid, if the function has not been exited before. Whenever a condition that ends with return is true, the function is exited, so a function can have several "returns".
Important: a function can not alter the value of simple variable types (int, float, double...) directly, instead it makes a copy of the passed variable (or of the variable outside the method), so the original value is not altered by the function! Only objects are passed by reference (and not by value). Strings and arrays are also objects in Java. For the simple data types there exist wrapper classes, for which class methods must be used to convert to and from the simple data types:
//a new object of the Integer class is created and initialized to 3
Integer a=new Integer(3);
//an int variable is declared and receives the value of a
int b=a.intValue();
//now b has the value 3
With this in mind, we can create a method that changes an Integer variable outside our method directly:
Integer a=new Integer(3);
void incrementNumber(Integer x)
{
int y=x.intValue();
//y is incremented
y++;
//the global variable a gets the new value, by initializing a new object
a=new Integer(y);
}
After calling this function, the value of a is now 4 instead of 3. The function has changed it by reference (by changing an alias of the original variable, that is connected with it). This might sound difficult to understand in the beginning, and you won't need to use it often. I just put it in here, to make this toppic complete.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.