Journey to Java: Episode 4 “Java Basics and Methods.”
Keywords
There are over 50 Java keywords that have reserved meanings in the language. This is important to know as you cannot name variables the dame as a reserved keyword.
Some Java Keywords used so far in this blog series:
- boolean
- byte
- char
- class
- double
- float
- if
- int
- long
- short
- public
- static
- void
// you cannot do something like this
int int = 3;
int void = 25;
Control Flow and If Statement Review
valid if statement syntax review
int highestNum = 10;
int newNum = 11;if (newNum > highestNum){
System.out.println("you are higher than the highest number");
System.out.println("congrats");
}
If there is only one statement following the if you can omit the curly brackets.
int highestNum = 10;
int newNum = 11;if (newNum > highestNum)
System.out.println("you are higher than the highest number");
So far the if statements we have been using have only had actions take place when a condition is true. We can account for the other occurrences by using the else Java keyword.
int highestNum = 10;
int newNum = 2;if (newNum > highestNum){
System.out.println("you are higher than the highest number");
} else {
System.out.println("you were not higher than highest number");
}
This code will check to see if the first expression following the if is true. Since n is not greater than 10 it goes to the else. The statement following the else will be executed.
You can also use else if to make another condition.
int highestNum = 10;
int average = 5;
int newNum = 6;if (newNum > highestNum){
System.out.println("you are higher than the highest number");
} else if (newNum > average && newNum < highestNum) {
System.out.println("not bad");
} else {
System.out.println("this number is pretty low");
}
With else if it gives us another chance to check a condition like the if statement. We can only do one if block but we can use multiple else if blocks.
Methods
Methods are chunks of code that can be written and called for multiple uses. We need have been using the main method this entire time. We write a method just outside the main method to avoid having a method inside a method. The method we write also needs to stay inside the class we are working in.
public class Practice { public static void main(String[] args) { }
//we want to write a new method here. Outside the main method but //still inside the Practice class}
The syntax for writing a method will need to include the same keywords as writing the main method depending on the situation. We will be using public, static, and void. I will be learning more about these keywords as I proceed further in my course and self studies.
public class Practice {
public static void main(String[] args) {
int firstNum = 1;
int secondNum = 2; }
public static void ourFirstMethodAddsNumbers() {
int total = firstNum + secondNum;
System.out.println(total) }}
Here we built out first method ourFirstMethodAddsNumbers(). the parenthesis on the end are going to be required for parameters later. now that we have written out the correct syntax and logic for our first method we need to be able to call it. When we run our Java program the main method will be run and we need to simply call the name of the method when we are ready for it’s contents.
A new issue arises. Our first method has no idea what firstNum or secondNum are because of scope. we want to find a way to have some data in the main method, our addition method, then plug the numbers in appropriately. This is done with parameters. To use parameters in Java will add the variables we want to plug into the method call into the parenthesis we set up.
public static void main(String[] args) { int firstNum = 1; int secondNum = 2; ourFirstMethodAddsNumbers(firstNum,secondNum);}
In the method we need to receive this information, declare the data type and give it a new name to be operated with inside the method.
public class Practice { public static void main(String[] args) { int firstNum = 1; int secondNum = 2; ourFirstMethodAddsNumbers(firstNum,secondNum); } public static void ourFirstMethodAddsNumbers(int one, int two) { int total = one + two; System.out.println(total); }}
Returning From a Method
If we want our method to send its final solution back to what originally called it we need to use its return value. The void keyword we have been using so far has ensured that our methods don’t return anything. The return value is void. If we want to return a value we need to change the void keyword to the data type of the content you want to return.
Right now ourFirstMethodAddsNumbers() is printing out it’s solution but we want to return the solution so we can continue to use the value in our main method. We will use the return word to return what we need.
public class Practice { public static void main(String[] args) { int firstNum = 1; int secondNum = 2; ourFirstMethodAddsNumbers(firstNum,secondNum); } public static int ourFirstMethodAddsNumbers(int one, int two) { int total = one + two; return total; }}
Now that we are returning we can test to see if our main method will receive this information.
public class Practice { public static void main(String[] args) { int firstNum = 1; int secondNum = 2; System.out.println(
ourFirstMethodAddsNumbers(firstNum,secondNum)
);} public static int ourFirstMethodAddsNumbers(int one, int two) { int total = one + two; return total; }}
Thankfully, we can also save the result of a method to a variable.
public class Practice { public static void main(String[] args) { int firstNum = 1; int secondNum = 2;
int calculation =
ourFirstMethodAddsNumbers(firstNum,secondNum);
System.out.println(calculation); } public static int ourFirstMethodAddsNumbers(int one, int two) { int total = one + two; return total; }}
Final Thoughts
Java’s methods are once again similar to what I am familiar with from other programming languages. There is a pattern of the only thing being different is declaring data types which is taking some getting used to. You need to declare the data type when setting a variable, defining parameters, and even returning a value. It is just going to take practice and repetition for me to include all of these data types. So far after learning a little about Java basics I am enjoying the language and finding it a pleasure to work with. I am excited to get to the point where I can start to make projects rather than simple code solutions. Thanks for reading!