Not registered yet?
Register now! It is easy and done in 1 minute and gives you access to special discounts and much more!
C# makes distinction of those two. Does java do the same or differently?
Actually, everything in Java is passed by value, references being a special type of value, just like pointers are values in C. More about the semantics here:
C# on the other hand, does have real references a la C++, which are unrelated to reference values... Can it get any more confusing than that?
you can look at it like this: In Java everything is passed as a value but some objects may be mutable.
-1 No, not everything in Java is a value. Java Objects are not values; there is no way to pass an object as a value, they are not copied like values, etc. In C everything is a value, including pointers, and one can build reference semantics on top of that. In Java the reference semantics are built-in and it is not possible to access the underlying Objects as values the way one can access C structs as values.
@bames53 Sure, I've just edited my answer to make it clearer than we're talking about argument passing. Just because we don't have access to the actual number doesn't mean it doesn't exist.
"everything in Java is passed by value" Java objects are not passed by value.
@bames32 That's because Java doesn't support "objects" as arguments. It only supports primitive types and references (aka pointers), which are values: docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1
In Java, the primitives are value types, classes and arrays are reference types.
In Java, all objects and enums are reference types, and all primitives are value types. The distinction between the two is the same as in C# with respect to copy semantics, but you cannot define a new value type in Java.
In other words Java lacks the ability to create "fast" efficient value types?
@Pacerier: it is not possible in Java currently (2014). Some people are looking into it: cr.openjdk.java.net/~jrose/values/values-0.html
So is there a difference between int x and Integer x? Looking at java Integer.java source-code, does it tell anything about whether it's passed as value or as reference?
@Shimmy: there are many differences between int (a primitive type) and Integer (a class). Currently all Java classes, such as Integer, are treated as reference types. This means that references to Integer objects are passed around. (Side note: this is different than pass-by-reference, which Java does not have. See stackoverflow.com/a/40523/454967 for a full explanation.)