Ini review lagi tentang Java! Check this out.

Question: Does Java pass by reference or pass by value?

Answer:
Before answering the question, let define which are pass-by-reference and pass-by-value means.

Pass-by-value
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter’s value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.

Pass-by-reference
The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.

Source: http://javadude.com/articles/passbyvalue.htm

But, fact about Java explains that:

Truth #1: Everything in Java is passed by value. Objects, however, are never passed at all.
That needs some explanation – after all, if we can’t pass objects, how can we do any work? The answer is that we pass references to objects. That sounds like it’s getting dangerously close to the myth, until you look at truth #2:

Truth #2: The values of variables are always primitives or references, never objects.
This is probably the single most important point in learning Java properly. It’s amazing how far you can actually get without knowing it, in fact – but vast numbers of things suddenly make sense when you grasp it.

Source: http://www.yoda.arachsys.com/java/passing.html

According to these two facts, we can conclude that:

Java does manipulate objects by reference, and all object variables are references. However, Java doesn’t pass method arguments by reference; it passes them by value.
Source: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

To give clearer answer, let examine this example. In this example I create three classes below:

1. Class ReferenceTest is the blueprint of an object which we use as example of swapped object.

public class ReferenceTest {
int myRef;
public ReferenceTest(int myInput){

this.myRef = myInput;

}

public int getMyRef(){
return myRef;

}

public void setMyRef(int myInput){
this.myRef = myInput;

}
}

2. Class SwapTest which contain method to do swapping test.
public class SwapTest {
public static void swaping(ReferenceTest rt1, ReferenceTest rt2){

System.out.println();
System.out.println(“=======================================”);

System.out.println(“This is what happened in swap process:”);

System.out.println(“Before swapping: “);

System.out.println(“Value of firstRt: ” + rt1.getMyRef());

System.out.println(“Value of secondRt: ” + rt2.getMyRef());

ReferenceTest tempRt;
tempRt = rt1;

rt1 = rt2;
rt2 = tempRt;

System.out.println();
System.out.println(“After swapping: “);

System.out.println(“Value of firstRt: ” + rt1.getMyRef());

System.out.println(“Value of secondRt: ” + rt2.getMyRef());

System.out.println(“=======================================”);

}

}

3. Class MainTest is the main class.
public class MainTest {
public static void main(String[] args) {

ReferenceTest firstRt;

ReferenceTest secondRt;

firstRt = new ReferenceTest(5);

secondRt = new ReferenceTest(10);

System.out.println(“Before swapping: “);

System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef()); SwapTest.swaping(firstRt, secondRt);
System.out.println();

System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef());
}

}

Result of the application is as shown below:

Before swapping:
Value of firstRt: 5

Value of secondRt: 10

=======================================
This is what happened in swap process:
Before swapping:
Value of rt1: 5
Value of rt2: 10

After swapping:
Value of rt1: 10
Value of rt2: 5

=======================================

After swapping:
Value of firstRt: 5
Value of secondRt: 10

By the first, application will create two instances of ReferenceTest class which are firstRt and secondRt objects. Java will create two Objects which type is ReferenceTest and two variables whose value is a pointer to those two objects.

Figure1. Java will create two objects and two variables.

So, when we attempt to execute these statements in main method:
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef());

Java will give values of ReferenceTest objects on which those two variables pointed. The result of these statements is:
Before swapping:
Value of firstRt: 5
Value of secondRt: 10

When we pass these two variables (firstRt and secondRt) into swaping method, Java will create two variables to collect those two variables which were passed.

Figure2. Java will create two variables in swaping method.

So, when we attempt to execute these statements:
System.out.println(“Before swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());

These statements will give same result as statements in Main method, since these two variables contain value of pointer which is pointed into same objects which also pointed by firstRt and secondRt in Main method. The result of these statements is:
Before swapping:
Value of rt1: 5
Value of rt2: 10

When we swap rt1 and rt2, means we swap pointer value contained by them. Even though they were swapped, actually firstRt and secondRt are not swapped (since Java passes the reference by value).

So, when we attempt to execute these statements in swaping method after swapping process:
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + rt1.getMyRef());
System.out.println(“Value of secondRt: ” + rt2.getMyRef());

This will give output as shown below:
After swapping:
Value of rt1: 10
Value of rt2: 5

Figure3. After swap process, rt1 and rt2 is swapped while firstRt and secondRt isn’t.

Otherwise, when we execute these statements in Main method:
System.out.println(“After swapping: “);
System.out.println(“Value of firstRt: ” + firstRt.getMyRef());
System.out.println(“Value of secondRt: ” + secondRt.getMyRef())
;
This will give output as shown below:
After swapping:
Value of firstRt: 5
Value of secondRt: 10


  • Share/Bookmark

Baru aja nambah page counter. Biar tau aja rate dari blog ini.


  • Share/Bookmark

Temanku! Kau memang menyebalkan! Di atas langit masih ada langit! Mara bisa abuku, munyine jeg tegeh sajan. Biasain aja. Kalo tau ya kasi tau, kalo ga tau bilang ga tau. Ga usah dilebih-lebihkan ato dikurang-kurangkan. Kalo ada ilmu share-lah dengan yang lain. Jangan pelit2. Jangan juga terlalu banyak bicara. Ingatlah! Tong kosong, selalu nyaring bunyinya!

Anyway, parameter passing in Java – by reference or by value?


  • Share/Bookmark

Weiz, setelah sekian lama akhirnya ngepost lagi! Ini tugas buat di kantor. Sebenarnya banyak sekali opini yang mengulas di forum2 di internet. Ok, here it is!

QUESTION:

Why Java is not a 100% Object Oriented Programming Language?

ANSWER:

Before we answer the question, we should have a specific point of view of the meaning of Object Oriented Programming Language. There are actually so many opinions which tell there is no any specific term can be used to define it. This is a sample of them:

Source: http://forum.java.sun.com/thread.jspa?threadID=5186578&start=15&tstart=0

On “purity” as a measure of object-orientedness:

However, this definition is problematic in that it implies that “purity” is a valid concept. Yet, there is no accepted definition of the term “pure OO language”. Despite years of discussion and argument on the net, and in other circles, I have never seen anyone present a definition of a “pure OOPL” that met with general agreement by others.
– Robert Martin, founder, CEO, and president of Object Mentor Inc., software industry leader.

On the value of using “purity” as a measure of a programming language:

I will not go into the discussion about “purity” beyond mentioning that I think that a general purpose programming language ought to and can support more than one programming style (“paradigm).
– Bjarne Stroustrup, designer and implementer of the C++ programming language, etc.

On the alleged “purity continuum”:

OO language experts divide OOPLs into two categories, hybrid languages and pure OO languages. Hybrid languages are based on some non-OO model that has been enhanced with OO concepts. C++ (a superset of C), Ada 95, and CLOS (an object-enhanced version of LISP) are hybrid languages. Pure OO languages are based entirely on OO principles; Smalltalk, Eiffel, Java, and Simula are pure OO languages.

So, in this assignment I will assume that OOP Language:
Source: http://forum.java.sun.com/thread.jspa?threadID=5186578&start=15&tstart=0
Purist view about a 100% OOP language would be “Everything is an object”.

According to this assumption, there are several reasons why Java is not a 100% Object Oriented Programming Language:

In Java, the primitives have been excused from being objects for simplicity and to improve performance.
This is makes sense, since a primitives data types is not an object and couldn’t be defined as an object, neither a class. It is clear enough, because primitive data don’t have any ability/characteristic such as inheritance or polymorphism. So, according to this explanation we can say that Java can be defined as not fully OOP supported language, since it’s allowed a non object-oriented thing to exist.

Operator overloading is not possible in java.
Except “+” operator which can be use in two different operations (addition operations and concatenations of string), there is no possibilities in Java to do an operator-overloading. Since, this is a kind of polymorphism ability (specific for operators) we also could say that Java isn’t 100% OOP Language.

Multiple-inheritance through classes is not possible.
As same as the explanations of operator overloading, multiple inheritance is also an (ad-hoc) abilities. We could also say that Java isn’t 100% OOP Language according to this point of view.

Availability of static methods and variables.
Since this ability is exist in Java, which allow developers to invoke a method without instantiating any object is like breaking rules of encapsulation. We could also say that Java isn’t 100% OOP Language according to this point of view.

References:


  • Share/Bookmark

Di manapun dirimu aku ga peduli.
Kapanpun kutemui aku ga akan kasihani.
Dari detik kau melakukan hal itu sampai hari ini
Kau deklarasikan dirimu sebagai musuh abadiku

Aku akan usahakan, dirimu mati di tanganku
Saat ini ku tak tau kapan
Kuberi waktu kau nikmati hidupmu
Tapi ingatlah, aku akan menjadi pencabut nyawamu

Berdoalah agar Tuhan menyadarkanku
Karena aku tak akan pernah pedulikan yang lain

Ingatlah…
Aku akan selalu menjadi musuh abadimu
Kapanpun,
Semoga aku bisa mengingatmu di kehidupan mendatang
karena saat itu pun kau tetap musuh abadiku

Lawan aku pengecut!!!


  • Share/Bookmark
© 2010 IMS' Blog Suffusion WordPress theme by Sayontan Sinha