Thursday, December 4, 2014

Hey, I have fruits now can you tell me how to make the Fruit Salad !!!

Sorry for them, who came here seeing the article heading. Sorry bro, I am not making any salad here, I am writing article on Java Programming.

You told me about the words, sorry keywords used in Java program but no clue about how to use them. Would you please like to give me a hint of how to use them ?
Remember from my previous post, I had written that, in any program, there are two parts, one is data and another is instruction to what to do with the data.
We are aware of the data part and we have also a little knowledge of the Java dictionary. Now its time to learn, how to build blocks of programming.

Analogy: You have chosen the fruits, bought some peeper and black salt to use in your salad. Now if you don't know, how to proceed, probably you will end up doing nothing.

That is the case in Java programs too. You get your variables, store them in memory but don't know what to do with them, you will probably going to end up doing nothing.

Hey, enough boring lectures. Can you deep dive or I surf the internet instead of reading this article ?
Sorry to exaggerate it a little. Now we will see how stuffs are organised to get the work done.

In Java, we work on variables in blocks known as METHODS. Method is the place where all the instructions are written and algorithms are executed.

Methods has a special syntax to follow. The minimal syntax is as follows,

<return type> <method name>(){}

Following is an example of a method.
 class MethodCheck {  
      void method() {  

If method is just return type and method name and two curly braces, what is this class doing here ?
In Object Oriented Programming, anything you want to do, must be done in objects. So, if you are writing the simplest program, you have to declare a class for it.

The first rule, isn't it ?
Yes, this is the first rule of OOP.

OK, I got it, what's next ?
Now, you can see, that within the class, there is a method, it has void keyword. this means that this method is going to return nothing and method is the method name.

Wait, wait, wait. I have two questions.
Please move on.

What am I supposed to do with a method that returns nothing ?
Well, its upto your choice, how you are going to implement everything in your code and according to your design, methods can return anything you want it to return, I am absolutely nobody to tell you what you should do in your application. I used it just for learning purpose and nothing else. When we'll explore more, we'll see what other stuffs can be returned.

My next question, your method name is method. Does Java allow that ?
Yes, as long as it is not a keyword and a valid identifier, Java does not have any problem with the name. It may be anything you want within the valid premises. If you want to refresh your knowledge on identifier, you can check this. This article gives you a brief idea of naming your identifiers. Again remember, I am absolutely nobody to tell you what you should do in your application.

Well, keep on. What else ?
Now, you have return type and method name in place. Next is (), this is a standard notation you have to keep in mind. These braces can be left empty or it can have something in it.

Something means ? 
Good question.
It can have anything you want to use in the method. We'll talk about it later. For now, just concentrate on the empty braces.

OK, carry on.
Now, you see two curly braces. Well, this is the place where every instruction goes. Check the first application. You can relate things now. You have the class declaration, then the method declaration and there is something within (). Then in the curly brace pair, there is an instruction, which basically prints Hello World in console.

I understand the part of method and class but what about other stuffs ?
Yes, this program has many other stuffs, that is still not clear to you. Just forget them for now and concentrate on the method and instructions. We'll see what other stuffs are and how you can use them in your own application.

So, a basic application can be in the following form,

 class MethodCheck {  
      void method() {  
           System.out.print("Hi there, Learning Java");  

Word of Caution: You can not run this program as we did here.

Why ?
Cause, it lacks something, that is required to run your application.

OK, what next ?
Next, we are going to learn some keywords. Hold on !!!

Prev     Next
Palash Kanti Kundu

No comments:

Post a Comment