Hello and welcome back again to yet another important concept in Java. Like i always say, every nitty gritty in java is and will always be relevant and important. Lets dive straight into the topic of today. Return… This
The return statement
Initially, when beginners begin getting their hands dirty with the Java syntax, they always create methods that look like this.
Looking at the above image, you can see that the method head(“oh yes, you just learnt that the top part of a method is called the head and also, the curly braces code that goes inside of it, is called the method body”), has a keyword known as void. This keyword, denotes that the method “thisIsAMethod()”, does not return any value after the method has been called and executed. What does this mean????
Now for a methods head to be indicated with the void keyword, it means that after the method has been executed, it will have nothing to offer the JVM(“Java Virtual Machine”) in memory. check out the following images and their method heads.
The first above snippet of code which has void in its head, only does nothing but print to screen and also a calculation. But when a method, has the return keyword, it therefore means that the method after its execution returns a value(“any value including all the primitive datatypes or objects”)back to the method caller and from there, can be stored in a variable and used for different actions(“Conditions if a boolean is returned”)… see image below.
The above snippet of code returns a value that gets stored in a variable and then can be used for any other operation.
And also, a method returning a boolean value can be used to test conditions.
Note: looking at the return statement of the “thisIsAmethod2”, you can see that we try to return a boolean value of either true or false whichever is the case. So try to use this way of returning boolean values, it will save you too many lines of code.
Why would i want to return a value ???…. This is a good question asked by beginners. Well you would obviously want to return a value in programming when you intend to use the returned value for further programming actions.
Hmmmmm. As a beginner in Java programming, if you have been into active coding, you obvoiusly should have started woking with classes and their objects. Simply put, the this keyword, is used to refer to an object of a class or the objects fields….. See pics below
On the picture to the left, you can see that the this keyword is being used to refer to the objects fields inside the constructor(“if you don’t know what a constructor is, then CLICK HERE”). Also the picture to the right shows how the this keyword is used to check if the current object of a class is equal to another object that has been passed into its equals method and finally in the bottom picture, the this is used in the constructor to also refer to the objects class fields.
Finally, you have learnt something new and important today. One word of advise from me to you is that to become more conversant with java programming, you need to keep on involving yourself in more and more and more java codes…… That’s all.
Right now, am off to learning new stuff. BYE