Posted by: ahmedashfaque | February 18, 2015

Why Java is so popular? part 4


We have been discussing about reasons for popularity of Java programming language over part 1, part 2 and part 3. We will continue this discussion today.

Let us see a small piece of Java code.

private void cal_salary () {

int sal = 2500;

int month = 2;

int total_sal = sal*month;

system.out.println(“total salary for 2 months is:” + total_sal)

}

If you run this piece of code then you will get output as “total salary for 2 months is 5000”. Now let us see some declarations and constructions of the source code.

When we declared private void method name (); we are stating that this method is a private method and it is not returning any value. The word “private” is a modifier and it denotes accessibility of a method. A method can be accessible from within a class or from outside from another class.

A modifier for a method or a variable is very important for Java programming. This in fact goes into designing your program. If you want a method to be used for a calculation and this type of calculation is not used anywhere in your program then you will declare it as private.

However it is possible that this kind of computation is needed at many other places. In that case you will declare it as “public”. In that case it can be accessed from anywhere in your code. For example suppose you have put this method inside a class named “my_class” and you have declared your method as public then you can access this method from within another class.

public void cal_salary (){

}

So our method is public now. Now if I am in class “any_class” and want to access this method then I will use my_class.cal_salary().

The referencing mechanism of pieces of code in Java is so good that you can reuse your code, over and over. Code reuse is a powerful concept. There are 2 benefits of code reuse. First, you do not need to write pieces of code many times if they are supposed to do same kind of computation. The second benefit is that chances of error in code is minimized as the more code you write, the more chances of error in code. So less is better.

There are some more benefits of code reuse. Your debugging time will be lot less as you will be debugging your code only once instead of doing it many times for all the duplicated code at many places. Maintaining your code will be easier because if any change has to be done then it will be done in just one place instead of making changes all over the many pieces of code scattered over your entire code base.

Advertisements

Responses

  1. […] far we have seen many features of Java programming language in part 1, part 2, part 3, and part 4. Let us continue the discussion in this […]

  2. […] far in part 1, part 2, part 3, part 4, part 5 and part 6 we have covered many topics on Java. In this post we will see […]

  3. […] far in part 1, part 2, part 3, part 4, part 5, part 6 and part 7 we have covered many topics on Java. In this post we will see […]

  4. […] far in part 1, part 2, part 3, part 4, part 5, part 6, part 7 and part 8 we have covered many topics on Java. In this post we will see […]

  5. […] far in part 1, part 2, part 3, part 4, part 5, part 6, part 7, part 8 and part 9 we have covered many topics on Java. In this post we […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: