Pass by value in Java [duplicate] - java

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Does Java pass by reference?
Is Java pass by reference?
Hey,
Is it true that Java passes everything by value, and we can't pass something by reference ?
Thanks.

Good God, it can't be asked again. Java is pass by value - always, in all cases. That's it.
Here's a reference that quotes James Gosling, who should be authoritative enough for anyone:
From the authors of Java: "There is
exactly one parameter passing mode in
Java - pass by value - and that helps
keep things simple." The Java
Programming Language, 2nd ed. by Ken
Arnold and James Gosling, section
2.6.1, page 40, 3rd paragraph.

Java passes value of references of the objects you are passing and simple value for primitive types.
See following discussion on this:
Is Java pass by reference?

Java passes primitives by value by default, and all types have their object references passed by value. See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.1 for details.

Related

Is Java using pointer to access Array by index? [duplicate]

This question already has answers here:
How can I use pointers in Java?
(16 answers)
Closed 2 years ago.
I am curious. is java using Pointer like C or C++ to access array by index? when use C languange, x[a] can be converted to *(x + a). What about Java languange? its same with c and c++ or it just use Sequential search to access the element?
If it is an array of objects, it's essentially an array of pointers that reference those objects, this is not the case for primitive values however. Unlike c++, you cannot do pointer arithmetic in Java.
OK, i found an answer from this https://softwareengineering.stackexchange.com/a/105919
In Java, plain pointer arithmetics (referencing and dereferencing) don't exist anymore. However pointers exist. They call them references, but it doesn't change what it is. And array access still is exactly the same thing: Look at the address, add the index and use that memory location. However in Java, it will check whether or not that index is within the bounds of the array you originally allocated. If not, it will throw an exception.

About int.class [duplicate]

This question already has answers here:
What does int.class mean
(3 answers)
Closed 6 years ago.
i have been experimenting with java trying to logically understanding it ...
but i dont understand why this makes sense in java
int.class
i mean int is not a class.it's just a primitive type;so as far as i can understand it contains a number and nothing more.. let alone a "class" element inside it ...
what is happening in the background ?How is this possible?
please be as more as more elaborate as you can.
thank you in advance.
P.S. i read some of the other answers to similarly asked questions ..but i didn't understand.
Variables of the primitive types hold primitive values, and variables of the reference type hold reference values. Reference values refer to objects, but are not objects themselves. Primitive values, by contrast, do not refer to anything. They are the actual data themselves.

Primitive vs Object type in Java [duplicate]

This question already has answers here:
Why do people still use primitive types in Java?
(21 answers)
Closed 9 years ago.
This question came to my mind because I have read somewhere that Java is not a pure Object oriented language since it is using primitives (which are not objects). I can agree with that. Now my problem is why we are using primitives/wrappers while we already have Object in same type?
As an example if we consider Integer, It has same value limit as int other than object behavior. why still Java use primitives under these condition?
As my opinion, if Java only use Object type Autoboxing and Unboxing no need. Also there is no primitive for String by the way.
One reason is due to memory usage. Primitives, such as int, float etc. require less memory allocations (I think 4 bytes) in comparison to Objects which are at the very least 8 bytes. Please see the following reference:
In addition, a lot of arithmetic (numeric) is completed with the use of primitives rather than their Object equivalents and this is another reason why they are quite critical in the Java language.

I don't understand this pass by value and pass by reference concept in java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between passing by reference vs. passing by value?
Is Java pass by reference?
I don't understand this pass by value and pass by reference concept in java. Can anyone explain me in lame words... after reading many articles, i am still not understanding it.
Java uses only pass by value. That's it.
http://stackoverflow.com/search?q=[java]+pass+by+value
I can attempt a layman's explanation of the general principle, but I don't use Java much so there may be some wrinkles.
OK so when you pass a variable into a function/ subroutine/ method, you have these 2 choices.
Pass by value: your variable will be copied and 2 variables will exist independently, one inside the function scope and one in the calling scope. The former will cease to exist once the function completes, so the latter variable will not change. This is also known as 'pass by copy'.
Pass by reference: the variable is not copied. All that is passed is a reference to the location of the original variable (in the calling scope). So, if the called function modifies the variable, it will persist even after the function returns to the calling scope.
Pass by reference is generally more efficient, especially for large variables. However functional design principles say that you should avoid using reference values for returning the result of a function.
Let me know if any clarification needed!

Why does Java have NullPointerException instead of NullReferenceException? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is a Null Pointer Exception?
Java: Why aren't NullPointerExceptions called NullReferenceExceptions ?
Our of idle curiosity, does anyone know why the null reference exception in Java was called NullPointerException?
It seems counterintuitive that in a new language that officially has no pointers a choice would be made to use this name when using a null reference.
If anyone can point me to an authoritative explanation, that would be appreciated.
The statement that Java "officially has no pointers" is simply false. Just because you can't do arithmetic on it doesn't mean it's not a pointer. C is not the final authority for programming terminology.
The only people who get hung up over it are either C/C++ fans who want to disparage Java for its lack of power over the bare metal, or (unlikely, nowadays) marketing people who want to sell Java as a simpler, safer alternative to managers who've had bad experiences with C development.
From the Java language specification:
The reference values (often just
references) are pointers to these
objects, and a special null reference,
which refers to no object.

Categories