Affichage des articles dont le libellé est java. Afficher tous les articles
Affichage des articles dont le libellé est java. Afficher tous les articles

vendredi 6 avril 2012

Swapping numbers in java, pass-by-value VS pass-by-reference

I never thought about it, but I just discovered it is not possible to write a method to swap numbers in Java since Java is strictly pass-by-value, not pass by reference.
public void swapNumbers(int x, int y)
{
    int temp = x;
     x = y;
     y = temp;
}
If we call this function to try to swap numbers, the numbers won't be changed.
There is an example of swapping numbers in this link: http://www.roseindia.net/java/beginners/swapping.shtml, but it's confusing for beginners.

public class Swapping{
  static void swap(int i,int j){
  int temp=i;
  i=j;
  j=temp;
  System.out.println("After swapping i = " + i + " j = " + j);
  }
  public static void main(String[] args){
  int i=1;
  int j=2;
 
  System.out.prinln("Before swapping i="+i+" j="+j);
  
swap(i,j);
 
  
}
}


This example doesn't mention that after we call the method swap, the numbers will return to their original values after this call.
The same operation is really simple in C++ which allows us to pass values by reference

void swapNumbers(double& a, double& b)
{
    double temp = a;
    a = b;
    b = temp;
}
 
Now, when we call this function, a and b will be swapped.*

double number1(5.5), number2(7.0)
swapNumbers(number1, number2);
cout << "First number : << number1 << endl;
cout << " Second number :" << number2 << endl;

The output will be
First number : 7.0
Second number 5.5;

A reference is merely an alias, another name for the same variable. Hence, making operations on a variable will also change the variable value.

real life example for passing argument by reference in java

I read this in one of the comments for this answer in SO http://stackoverflow.com/a/40523/612920

My attempt at a good way to visualize object passing: Imagine a balloon.
Calling a function is like tieing a second string to the balloon and handing the line to the function.
parameter = new Balloon(); will cut that string and create a new balloon (but this has no effect on the original balloon).
parameter.pop(); will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed.

dimanche 18 décembre 2011

Getting started with the javadoc

If you are using eclipse, working with javadoc is pretty easy. It's not that complicated using the command line either, personally I'm an addict to the command line, but I didn't bother too much with generating the javadoc using it since I tried already and I failed miserably. I had a problem when trying to set the classpath, since there was a lot of dependencies to include. I should have written an ant script, but I was too lazy. All programmers are :). So, I just chose the easy way for once, selecting project > generate javadoc... You have then to go through the usual straightforward process, choosing some options and then next, next.

java heap space configuration


And don't forget to configure the JVM heap space, especially when you have a big project with a lot of comments in it. This way, the javadoc will be generated a lot faster, and you won't be at the risk of having an error with heap space.
My advice is, don't forget to document well you code. A good code is a self documenting code. Compare:

int x;
String y;

and

int hitPoints;
String playerName

A lot better, isn't it?
And if you have a self documenting code like the last one, when you put comments to explain it, if you are not going to add some value to the explanation, it's better to leave it as it is.



/** calcualte the number of the likes */ 
public int calculateNumberOfLikes()

That's what I would call a waste of time. Who'd do that anyway, well, more people than you think, not all people have learnt to program the way programming should really be.




/** This function calculates the number of the likes of a friend.
 * 
 * This function calls an FQL query which determines the pages a friend likes,
 * and calculates the number of times he liked an item in that page
 *
 * @author someone 
 */ 
public int calculateNumberOfLikes()

This is just a little example to show the forms a javadoc comment has. The first sentece is a summary of what the function does. Then a more detailed description, then some javadoc-specific tags about the purpose of the function, we can add html tags to this description in order to format it the way we want. There is a lot of tags to know about, which can give us a lot of information about a function, a class or a variable. So, it's good to have the habit of well documenting our code, cause we are not the only ones that work in our code, and we can make others win a lot of time by making it easier for them to understand our code. And in case of a solo programmer working on his hobby project, you'll eventually end up coming back to the code you wrote after some months, and then, there is no telling how much of your code you will remember and how much time you will waste because of a poor documented code.

jeudi 10 novembre 2011

Let's talk about finalize in java

So, what is finalize, how do we use it?
Well, the answer is simple, we don't. To be more precise, most of the time we don't.
The finalize method gets called when an object is garbage collected to clean any mess caused by that object. All java object have that method, since it is implemented on the Object class. And as you are already aware of, it's the mother class of all other java classes. If the object which was just garbage collected, was using system resources, the finalize method will take care of releasing them. An example of these resources could be an open file. So be careful when you try to use it, it won't clean your java objects, it will only take care of non-java resources.

The finalize method shouldn't be used since it's not a very reliable method, since there is no guarantee that the object will be garbage collected during the lifetime of an application, in addition to the fact that it takes care only of non-java resources. But if you want to use it, here's how to proceed.

You have to declare your method this way:

protected void finalize () throws throwable
Now, inside the method:

Since the method is error-prone, it should contain the famous try-catch-finally statement


try
{
    //you do your personalized cleaning operations here
} finally {
    //then you have to call the parent finalize method
    super.finalize()
}
 
Now you have your finalize() method ready for action, just don't forget, don't rely on it too much, because maybe it will be called, and maybe it won't.