By user496949


2011-03-05 02:57:43 8 Comments

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

3 comments

@Samuel Audet 2011-03-05 03:12:28

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?

@seand 2011-03-05 03:24:48

you can look at it like this: In Java everything is passed as a value but some objects may be mutable.

@bames53 2013-03-06 18:48:39

-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.

@Samuel Audet 2013-03-07 07:52:27

@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.

@bames53 2013-03-07 14:42:20

"everything in Java is passed by value" Java objects are not passed by value.

@Samuel Audet 2013-03-08 06:09:22

@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

@Orbit 2011-03-05 02:59:13

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

@ide 2011-03-05 02:59:01

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.

@Pacerier 2014-06-16 09:43:15

In other words Java lacks the ability to create "fast" efficient value types?

@ide 2014-06-16 22:52:14

@Pacerier: it is not possible in Java currently (2014). Some people are looking into it: cr.openjdk.java.net/~jrose/values/values-0.html

@Shimmy 2014-10-23 03:15:08

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?

@ide 2014-12-03 00:14:52

@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.)

Related Questions

Sponsored Content

24 Answered Questions

77 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

9 Answered Questions

[SOLVED] What are the correct version numbers for C#?

21 Answered Questions

36 Answered Questions

[SOLVED] Differences between HashMap and Hashtable?

26 Answered Questions

[SOLVED] How do I enumerate an enum in C#?

41 Answered Questions

[SOLVED] "implements Runnable" vs "extends Thread" in Java

19 Answered Questions

[SOLVED] Does Java support default parameter values?

25 Answered Questions

[SOLVED] What is the best way to iterate over a dictionary?

  • 2008-09-26 18:20:06
  • Jake Stewart
  • 1227120 View
  • 2065 Score
  • 25 Answer
  • Tags:   c# dictionary loops

8 Answered Questions

[SOLVED] Why is subtracting these two times (in 1927) giving a strange result?

  • 2011-07-27 08:15:58
  • Freewind
  • 568678 View
  • 6162 Score
  • 8 Answer
  • Tags:   java date timezone

Sponsored Content