22

C# makes distinction of those two. Does java do the same or differently?

user496949
  • 83,087
  • 147
  • 309
  • 426

3 Answers3

29

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.

ide
  • 19,942
  • 5
  • 64
  • 106
  • 4
    In other words Java lacks the ability to create "fast" efficient value types? – Pacerier Jun 16 '14 at 09:43
  • 2
    @Pacerier: it is not possible in Java currently (2014). Some people are looking into it: http://cr.openjdk.java.net/~jrose/values/values-0.html – ide Jun 16 '14 at 22:52
  • 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 Weitzhandler Oct 23 '14 at 03:15
  • 1
    @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 http://stackoverflow.com/a/40523/454967 for a full explanation.) – ide Dec 03 '14 at 00:14
4

In Java, the primitives are value types, classes and arrays are reference types.

Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
2

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:

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

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?

Samuel Audet
  • 4,964
  • 1
  • 26
  • 33
  • you can look at it like this: In Java everything is passed as a value but some objects may be mutable. – seand Mar 05 '11 at 03:24
  • 2
    -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 Mar 06 '13 at 18:48
  • @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. – Samuel Audet Mar 07 '13 at 07:52
  • 1
    "everything in Java is passed by value" Java objects are not passed by value. – bames53 Mar 07 '13 at 14:42
  • 5
    @bames32 That's because Java doesn't support "objects" as arguments. It only supports primitive types and references (aka pointers), which **are** _values_: http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.1 – Samuel Audet Mar 08 '13 at 06:09