Only one!
Q. Why we say, in c or Java, everything is pass-by-value?
A:
In c and Java, everything is pass-by-value. In c++ and Pascal, you can pass-by-value or pass-by-reference.When we code in C, you pass pointers to a method, the value of pointer (address) will never change, but the value the pointer points to will change. In language theory, C is a only-pass-by-value language. Actually, this is exactly Java does. Java Object reference is equivalent to C pointer, the difference is you cannot dereference it, which makes java safer, but less flexible.Since in C, you can dereference a pointer, you actually can prove the pointer is passed by value. A simple test can prove that, you dereference the pointer inside/outside the method call, you will get two different values, which proves inside the method, what we are using actually a copy of the pointer, not the pointer itself.That is exactly what pass-by-value means. You make a copy of the
parameter (argument), and use it inside of the methods. The value of the parameter (argument) will not be affected outside of the method.In Java, value of an object reference is the reference. That is the source of confusion. However, if you read it twice, it only makes sense. Doesn't it?
To the top Q. Can you provide some authoritative quotation to support the saying "In Java, everything is pass-by-value"?
A:
From The Java Programming Language, by James Gosling et al. 3rd edition (pg. 56):
quote:
Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.
From The Java Tutorial
quote:
Pass by Value
In Java methods, arguments are passed by value.
When invoked, the method receives the value of the variable passed in. When the argument is of primitive type, pass-by-value means that the method cannot change its value. When the argument is of reference type, pass-by-value means that the method cannot change the object reference, but can invoke the object's methods and modify the accessible variables within the object.
This is often the
source of confusion--a rogrammer writes a method that attempts to modify the value of one its arguments and the method doesn't work as expected. Let's look at such method and then investigate how to change it so that it does what the programmer originally intended.
Quiz question from Sun:
In Java programming, all method parameters are passed by value.
Answer: true In Java, everything is pass-by-value, unless you think Dr. James Gosling forgot how he invented Java, AND Sun does not know how Java works.If we all agree those are not the cases. Then the question should change to that how to understand the fact "In Java, everything is pass-by-value".If you go up-and-down a little, you will find a lot of help here.
To the top Q. What can I do in Java, if I want to use the C++ like pass-by-reference?
A:
Java and C languages simply do not support pass-by-reference. They only support pass-by-value.
C++ and Pascal languages support both pass-by-value and pass-by-reference.
Ada language supports probably three, value, reference, and pass-by-name. Fortran language only support one, pass-by-reference. As far as I could recall, I might be wrong on these two. Don't have time and desire to check them out anymore.
Those are language designers' choices. As a language user like you and me, we cannot do anything about it. What we can do is vote by our keyboard, select the language we like to code...
To the top Q. Is there any programming language, which use pass-by-reference only?
A:
The first, and still the most efficient high level programming language is FORTRAN. As far as I can recall, it is everything pass-by-reference. FORTRAN is not dead, it is still alive. Is it amazing?! In NASA, the space center of USA, the most used programming languages are still FORTRAN and Ada, it is them sending men on the moon.
To the top
Some Java utilities, tools Q. What is javap?
A:
A Java utility officially called Java Class File Disassembler. It can disassemble your class by type:
javap -c Test$1
You must have compiled class Test$1.class before using it. See an interesting use of javap disassembler example here.
However, javap is commonly used as a kind of profiler, which profiles your class. It generates something similar to c/c++ *.h file, if you use no option or options like -privateIf you were c/c++ programmer in your previous life , and missed the *.h file convenience, then you have a utility to do the job for you. Who says the life is not beautiful? See javap - The Java Class File Disassembler for more options and details.
To the top Q. Why some code is perfect fine according to Java Language Specification(JLS), but not compilable by javac?
"An instance initializer of a named class may not throw a checked exception unless that exception is explicitly declared in the throws clause of each constructor of its class and the class has at least one explicitly declared constructor." --from JLS2
A:
Some Java code is perfect fine according to Java Language specification(JLS), but not fine to jdk1.2.2 javac compiler, since javac is not strictly compliant with JLS. It sounds strange, but it is the fact.There is a compiler which is 100% compliant with JLS: jikes.exe from IBM. Jikes.exe is jdk version independent too. No matter which version of jdk you're using, as long as your give your jar files classpath correctly, it will compile
correctly for you. If you find jikes anything not compliant with JLS, you can report or fix it too, since it is open source. See the following link for more about jikes:
http://oss.software.ibm.com/developerworks/opensource/jikes/project/index.htmlThe following code is not compilable by javac, but perfectly compilable by jikes and runnable by java:
class SmpException extends Exception {}
public class InstInitTest { int k, l;
{
k = 10;
System.out.println("It's ok to JLS, but it isn't ok to javac compiler");
if (k > 0)
throw new SmpException();
l = k;
}
public InstInitTest() throws SmpException{}
public static void main(String[] args) { try {
new InstInitTest();
} catch (SmpException e){}
} }
Attention: Sun finally fixed this problem in j2sdk1.4!!!
To the top
Q. What are the differences between JVM (java virtual machine) and JRE (java