Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== Reference (in/out) parameters ==== The arguments of primitive types (e.g. int, double) to a method are passed by value in Java whereas objects are passed by reference. This means that a method operates on copies of the primitives passed to it instead of on the actual variables. On the contrary, the actual objects in some cases can be changed. In the following example, object String is not changed. Object of class 'a' is changed. In C#, it is possible to enforce a reference with the {{mono|ref}} keyword, similar to C++ and in a sense to C. This feature of C# is particularly useful when one wants to create a method that returns more than one object. In Java trying to return multiple values from a method is unsupported unless a wrapper is used, in this case named "Ref".<ref>{{cite web |url=http://www.25hoursaday.com/ |title=A Comparison of Microsoft's C# Programming Language to Sun Microsystems' Java Programming Language: D. Now for Something Completely Different: 12. Pass by Reference |author=Dare Obasanjo |year=2007 |publisher=Dare Obasanjo |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/ |archive-date=19 September 2012 |quote=In Java the arguments to a method are passed by value meaning that a method operates on copies of the items passed to it instead of on the actual items. In C#, as in C++ and in a sense C, it is possible to specify that the arguments to a method actually be references to the items being passed to the method instead of copies. This feature is particularly useful when one wants to create a method that returns more than one object. In Java trying to return multiple values from a method is unsupported and leads to anomalies like: a method that swaps two numbers that have been the hallmark of freshman computer science classes for years, is impossible to do in Java without resorting to coding tricks. |access-date=10 September 2012 |url-status=dead |df=dmy-all}}</ref> {| style="width:100%; border:none;" class="wikitable" |- !width=50%| Java !! style="width:50%;"| C# |- valign=top | <syntaxhighlight lang="java" style="font-size:95%"> class PassByRefTest { static class Ref<T> { T val; Ref(T val) { this.val = val; } } static void changeMe(Ref<String> s) { s.val = "Changed"; } static void swap(Ref<Integer> x, Ref<Integer> y) { int temp = x.val; x.val = y.val; y.val = temp; } public static void main(String[] args) { var a = new Ref(5); var b = new Ref(10); var s = new Ref("still unchanged"); swap(a, b); changeMe(s); System.out.println( "a = " + a.val + ", " + "b = " + b.val + ", " + "s = " + s.val ); } } </syntaxhighlight> | <syntaxhighlight lang="csharp" style="font-size:95%"> class PassByRefTest { public static void ChangeMe(out string s) { s = "Changed"; } public static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } public static void Main(string[] args) { int a = 5; int b = 10; string s = "still unchanged"; Swap(ref a, ref b); ChangeMe(out s); System.Console.WriteLine("a = " + a + ", " + "b = " + b + ", " + "s = " + s); } }</syntaxhighlight> |- valign=top | {{code|1=a = 10, b = 5, s = Changed}} || {{code|1=a = 10, b = 5, s = Changed}} |}
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)