Recent Articles / Archives

JavaSE 7 Certification

April 28, 2012 · Awareness · No Comments Yet

Java Passes references by value

How does Java pass arguments into a method? The short answer is “by value,” but the details are subtle. Recall that in C, C++, and other languages you can choose to pass either a “value” or to “pass a pointer to a value”. The first option is called “pass-by-value.” The second option is called “pass-by-reference.” In Java, primitive variables are always passed by value. In other words, a copy of the variable is passed into the method. In other words, if you make a change to the variable within the method, then that change is not visible anywhere else. For example, suppose we have


double d = 2.0;

changeMe(d);

System.out.println(d);     //prints 2.0

The printed value will be 2.0 no matter what happens inside the method changeMe.

public void changeMe(double d)

{

//this has no effect on d outside of this method!

d = 345.0;

}

On the other hand, Java objects appear to be passed by reference, but it’s a ruse. All Java objects are pointers (we just don’t have to use the annoying pointer symbol * when we create them), so the pointer gets passed by value (oooh, subtle). For example, suppose I create an object called Car that has methods called setSpeed() and getSpeed().

Car ferrari = new Car();

ferrari.setSpeed(65.0);

changeParameters(ferrari);

System.out.println(“The speed = “+ferrari.getSpeed());

So what gets printed out? That depends on what happens inside the changeParameters method. Suppose

public void changeParameters(Car c)

{

//changes the car’s speed outside this method!

c.setSpeed(200.0);

}

Then the code prints out 200.0. Why? Because the ferrari was a pointer to the object Car. The ferrari got passed by value into the method. So a copy of the pointer got passed into the method. But a copy of a pointer still points to the same object, the ferrari! So changes to the ferrari in the method are felt outside the method as well.

Here is one more subtlety. Suppose we have

public void changeCar(Car c)

{

Car chevy = new Car();

//does not affect c outside this method

c = chevy;

}

And now we call


Car ferrari = new Car();

changeCar(ferrari);

Is the original ferrari object now a chevy (outside of the method)? No! Inside of changeCar we changed the original pointer to be a new pointer. But the original pointer was passed by value, so changing the pointer within the method has no impact on the original pointer outside of the method.

Phew!

For those who doubt J, try the following code. Recall that arrays are pointers in Java. First let’s show that either a pointer or a copy of the pointer must be passed into a method. We’ll resolve which one in a minute. (The next example shows that it is actually a copy.)

public class Example

{

public static void main(String[] args)

{

//”test” is a pointer.

int[] test = {1, 2, 3, 4};

//The original hex value of the pointer

System.out.println(test);

//Let’s pass test’s pointer into a method.

//The method changes some array values.

Example e = new Example();

e.modifyArray(test);

//This prints out “9 2 3 4”, not “1 2 3 4”

for(int i=0; i

{

System.out.print(test[i]+”  “);

}

}

public void modifyArray(int[] a)

{

a[0] = 9;

//Because “a” is a pointer to the array

//called “test”, this changes the first

//element of “test”.

}

}

See? We passed in a pointer to the array, and then used that pointer to modify an element in the array. (But it was really a copy of the pointer! See below.)

The following code shows that a copy of the array’s pointer is passed into a method.


public class Example

{

public static void main(String[] args)

{

//”test” is a pointer.

int[] test = {1, 2, 3, 4};

//The original hex value of the pointer

System.out.println(“test’s pointer: “+test);

//Let’s pass test’s pointer into a method.

//The method attempts to change test’s //pointer.

Example e = new Example();

e.modifyPointer(test);

//The method failed to change the pointer!

System.out.println(“test’s pointer: “+);

//This prints out “1 2 3 4”, not “9 9 9 9”

for(int i=0; i

{

System.out.print(test[i]+” “);

}

}

public void modifyPointer(int[] a)

{

//“b” is a new pointer.

int[] b = {9, 9, 9, 9};

//The hex value of b’s pointer

System.out.println(“b’s pointer: “+b);

//”a” now points to the same thing as “b”.

a = b;

//The hex value of a’s pointer

System.out.println(“a’s pointer: “+a);

//But this affects nothing outside the

//method. “test” does not point to “b”!

//Only “a” points to “b”.

}

}

If the actual pointer had been passed into the method, then “test” should have been changed to point to “b”. That didn’t happen!

source : http://academic.regis.edu/dbahr/GeneralPages/IntroToProgramming/JavaPassByValue.htm

Installer for Java Desktop App

Java is a superb language. I have loved it ever since I wrote “Hello World” in it. My love for it increased even more when I put up a dialog of username and password using JDialog. Later I found Java has a big ecosystem on its own. Java SE, the core, Swing, Applet, Servlet, JSP, JSF, JMS, JavaMail, EJB and it continues. Each topic mentioned in the previous sentence can have a 500+ pages book.

I have had the experience of desktop development, delivered applications as a JAR to clients, but still one thing that I would love to have is “Installer for Java Desktop Application”. Yes I found lots of them, among them Izpack along with launch4J is a wonderful combination. NSIS, JSmooth, EXEWrapper and all others are wonderful on their own but they all have a high learning curve. For understanding NSIS, one need to understand the NSIS’s style of scripts and for Izpack you need to know it’s XML structure and compilation issues. Despite the availability of the Installers and all being open source I am not comfortable with those installers. Is it because they have a some learning curve or Is it because the installers like things should have been provided by the language itself. Offcourse if I will be needing one, I should design one but question on my mind (Like the one facebook asks :D me everytime ) is shouldn’t Oracle (previously SUN) have provided these things by default. What I found is beginners are having hell lots of problems with that. There are no good tutorials/blogs and official posts for any Installer related things from the company on it’s own. Why is that? In my opinion, introducing those things and having good articles/tutorials on these things would lure the beginners into Java world. These are the things that are not very convincing and because of these difficulties, many adapt the other. Why would a beginner trouble himself moving into Java if s/he can get easily with Visual Studio  (Though there is Netbeans a java alternative for Visual Studio, I am a big fan of it ever since it’s 4.0 release) for example.

I wonder how Java/JavaSE7 installer itself is created. If you have the answer to this please write it in comments, I would love to see it. In my opinion, in an installer, basically there should be basic things like

  • Serial Key Generation technique for unique installation.
  • Prevention to the application if it’s copied to another device (it should be done by the application itself).
  • Providing Demo version for lets say we can open 50 times the application for Demo purpose, after reaching this, we should ask the serial key and continue with full feature.
  • Embedded DB installation, for making commercial application.
  • And the validation for the user inputs during installation.

in an installer.

Netbeans Platform a very powerful rich client platform for JavaSE based on swing is a cool thing. I am going through it, but even though, it creates installers, there seems no tutorials/blogs for creating custom installer through Netbeans Platform 7.1.  I would love to see this article if there are any (If you know, please share). Will let you know if I found any special during my research and study by utilizing my free time. Thank you for now. :)

January 17, 2012 · Java · Comments (5)
Next Page »