Created
March 8, 2016 20:08
-
-
Save cuchas/45e797ce92983c28a344 to your computer and use it in GitHub Desktop.
Pass by reference java test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// To execute C#, please define "static void Main" on a class | |
// named Solution. | |
class Solution | |
{ | |
static void Main(string[] args) | |
{ | |
Dog vito = new Dog("vito"); | |
Console.WriteLine(vito.getName()); | |
mudaVito(vito); | |
Console.WriteLine(vito.getName()); | |
Dog aDog = new Dog("Max"); | |
foo(aDog); | |
if (aDog.getName().Equals("Max")) { //true | |
Console.WriteLine("Java passes by value." ); | |
} else if (aDog.getName().Equals("Fifi")) { | |
Console.WriteLine("Java passes by reference." ); | |
} | |
} | |
public static void mudaVito(Dog x) { | |
x.name = "vito mudado"; | |
} | |
public static void foo(Dog d) { | |
d.getName().Equals("Max"); // true | |
//d = new Dog("Fifi"); | |
d.getName().Equals("Fifi"); // true | |
d.name = "Fifi"; | |
} | |
} | |
public class Dog { | |
public String name; | |
public String getName() { | |
return name; | |
} | |
public Dog(String s) { | |
name = s; | |
} | |
} | |
/* | |
Your previous Java content is preserved below: | |
import java.io.*; | |
import java.util.*; | |
/* | |
* To execute Java, please define "static void main" on a class | |
* named Solution. | |
* | |
* If you need more classes, simply define them inline. | |
class Solution { | |
public static void main( String[] args ){ | |
Dog vito = new Dog("vito"); | |
System.out.println(vito.getName()); | |
mudaVito(vito); | |
System.out.println(vito.getName()); | |
Dog aDog = new Dog("Max"); | |
foo(aDog); | |
if (aDog.getName().equals("Max")) { //true | |
System.out.println( "Java passes by value." ); | |
} else if (aDog.getName().equals("Fifi")) { | |
System.out.println( "Java passes by reference." ); | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment