Previously, we saw the simple method. This method took no input and returned no output. It just printed a String.
In reality, methods usually take one (or more) inputs.
These inputs are called "parameters".
Now let's take the simple method, and create a simple2() method that takes a String parameter, called name. We can now use the "name" input inside the method.
Now, when we run this we'll get this output:
You can now see that the method can be called anywhere in the code, and you can pass whatever String to it.
The methods we saw so far did not return anything. But we can make the methods return whatever datatypes we want.
Let's do a simple sum method, which takes 2 numbers as input, and returns their sum as their output:
When we run this, we get this output:
Here is another example, this time let's return a String. We'll pass first name and last name, and get back their full name.
When we run this, the output is:
Seriously, there is nothing more complex than this on methods, parameters and return types!
Methods in Java
A method is just a block of code that be called anytime. It helps in keeping the code clean.
Anytime you have some code that will be called repeatedly, go ahead and create a method. You can then call this from any part of the code.
Let me show you a simple method. In the previous example, I showed main() method. We'll call this simple method from the main() method.
Note: the "void" here means this method returns nothing!
Running this code will print this line:
I am a simple method.
It's actually that simple, folks!
Java's Platform Independence
In a previous blog, I mentioned that Java can written once and run anywhere. This is called "being platform independent".
I came across a really nice and short video that explains how Java accomplishes this, using what is called "bytecode". Please watch this.
In Java, the entry point to the program is a function called main. It has the following syntax:
So, now what does each of this mean? We don't need to go into details, but at a high level:
public - This function/method is available for Operating system to see
static - We don't need an "instance" of this class. Don't get it? It's okay, we'll visit this later
void - This method returns nothing
String args - This is basically a String array. You can optionally pass some arguments in an array.
Now, if we want to print a line, the syntax is:
Now, putting this all together, here is the full program.
That's it, folks!
You have your first Java program done. Congrats!!
A very brief introduction to Java
Java is another programming language. But calling it as another language does not do it justice. I'll call this boldly:
JAVA IS A VERY POWERFUL PROGRAMMING LANGUAGE
You will discover the power of Java language as you go. In my opinion, here are the things that make Java unique:
1. It is Object Oriented.
2. Once it is compiled, it can run anywhere. I can write code on my Windows laptop and compile it. Then the compiled code can on Unix/Linux or Windows
3. It is very English-like. So it is easy to learn.
4. There are a lot of editors that support Java.
Java has been used to develop applications in major Technology companies in Silicon Valley.
We will go over everything about Java in steps. Stay tuned.