Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Dog aDog = new Dog("Max");
- Dog oldDog = aDog;
- // we pass the object to foo
- foo(aDog);
- // aDog variable is still pointing to the "Max" dog when foo(...) returns
- aDog.getName().equals("Max"); // true
- aDog.getName().equals("Fifi"); // false
- aDog == oldDog; // true
- }
- public static void foo(Dog d) {
- d.getName().equals("Max"); // true
- // change d inside of foo() to point to a new Dog instance "Fifi"
- d = new Dog("Fifi");
- d.getName().equals("Fifi"); // true
- }
- public static void main(String[] args) {
- Dog aDog = new Dog("Max");
- Dog oldDog = aDog;
- foo(aDog);
- // when foo(...) returns, the name of the dog has been changed to "Fifi"
- aDog.getName().equals("Fifi"); // true
- // but it is still the same dog:
- aDog == oldDog; // true
- }
- public static void foo(Dog d) {
- d.getName().equals("Max"); // true
- // this changes the name of d to be "Fifi"
- d.setName("Fifi");
- }
- Dog myDog;
- Dog myDog = new Dog("Rover");
- foo(myDog);
- public void foo(Dog someDog) {
- someDog.setName("Max"); // AAA
- someDog = new Dog("Fifi"); // BBB
- someDog.setName("Rowlf"); // CCC
- }
- public class Main{
- public static void main(String[] args){
- Foo f = new Foo("f");
- changeReference(f); // It won't change the reference!
- modifyReference(f); // It will modify the object that the reference variable "f" refers to!
- }
- public static void changeReference(Foo a){
- Foo b = new Foo("b");
- a = b;
- }
- public static void modifyReference(Foo c){
- c.setAttribute("c");
- }
- }
- Foo f = new Foo("f");
- public static void changeReference(Foo a)
- changeReference(f);
- Foo b = new Foo("b");
- 1. Person person;
- 2. person = new Person("Tom");
- 3. changeName(person);
- 4.
- 5. //I didn't use Person person below as an argument to be nice
- 6. static void changeName(Person anotherReferenceToTheSamePersonObject) {
- 7. anotherReferenceToTheSamePersonObject.setName("Jerry");
- 8. }
- int x = 3;
- float y = 101.1f;
- boolean amIAwesome = true;
- int problems = 99;
- String name = "Jay-Z";
- JButton[] marxBros = new JButton[3];
- marxBros[0] = new JButton("Groucho");
- marxBros[1] = new JButton("Zeppo");
- marxBros[2] = new JButton("Harpo");
- private static void shout(String name){
- System.out.println("There goes " + name + "!");
- }
- public static void main(String[] args){
- String hisName = "John J. Jingleheimerschmitz";
- String myName = hisName;
- shout(myName);
- }
- void cppMethod(int val, int &ref, Dog obj, Dog &objRef, Dog *objPtr, Dog *&objPtrRef)
- {
- val = 7; // Modifies the copy
- ref = 7; // Modifies the original variable
- obj.SetName("obj"); // Modifies the copy of Dog passed
- objRef.SetName("objRef"); // Modifies the original Dog passed
- objPtr->SetName("objPtr"); // Modifies the original Dog pointed to
- // by the copy of the pointer passed.
- objPtr = new Dog("newObjPtr"); // Modifies the copy of the pointer,
- // leaving the original object alone.
- objPtrRef->SetName("objRefPtr"); // Modifies the original Dog pointed to
- // by the original pointer passed.
- objPtrRef = new Dog("newObjPtrRef"); // Modifies the original pointer passed
- }
- int main()
- {
- int a = 0;
- int b = 0;
- Dog d0 = Dog("d0");
- Dog d1 = Dog("d1");
- Dog *d2 = new Dog("d2");
- Dog *d3 = new Dog("d3");
- cppMethod(a, b, d0, d1, d2, d3);
- // a is still set to 0
- // b is now set to 7
- // d0 still have name "d0"
- // d1 now has name "objRef"
- // d2 now has name "objPtr"
- // d3 now has name "newObjPtrRef"
- }
- public static void javaMethod(int val, Dog objPtr)
- {
- val = 7; // Modifies the copy
- objPtr.SetName("objPtr") // Modifies the original Dog pointed to
- // by the copy of the pointer passed.
- objPtr = new Dog("newObjPtr"); // Modifies the copy of the pointer,
- // leaving the original object alone.
- }
- public static void main()
- {
- int a = 0;
- Dog d0 = new Dog("d0");
- javaMethod(a, d0);
- // a is still set to 0
- // d0 now has name "objPtr"
- }
- private void foo(Object bar) {
- bar = null;
- }
- public static void main(String[] args) {
- String baz = "Hah!";
- foo(baz);
- System.out.println(baz);
- }
- public static void tricky(Point arg1, Point arg2) {
- arg1.x = 100;
- arg1.y = 100;
- Point temp = arg1;
- arg1 = arg2;
- arg2 = temp;
- }
- public static void main(String [] args) {
- Point pnt1 = new Point(0,0);
- Point pnt2 = new Point(0,0);
- System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y);
- System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
- System.out.println(" ");
- tricky(pnt1,pnt2);
- System.out.println("X1: " + pnt1.x + " Y1:" + pnt1.y);
- System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
- }
- Point pnt1 = new Point(0,0);
- Point pnt2 = new Point(0,0);
- System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y);
- System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
- System.out.println(" ");
- X1: 0 Y1: 0
- X2: 0 Y2: 0
- tricky(pnt1,pnt2); public void tricky(Point arg1, Point arg2);
- arg1.x = 100;
- arg1.y = 100;
- Point temp = arg1;
- arg1 = arg2;
- arg2 = temp;
- X1: 0 Y1: 0
- X2: 0 Y2: 0
- X1: 100 Y1: 100
- X2: 0 Y2: 0
- public class PassByValue {
- public static void main(String[] args) {
- Test t = new Test();
- t.name = "initialvalue";
- new PassByValue().changeValue(t);
- System.out.println(t.name);
- }
- public void changeValue(Test f) {
- f.name = "changevalue";
- }
- }
- class Test {
- String name;
- }
- changevalue
- Test t = new Test();
- new PassByValue().changeValue(t);
- public class PassByValue {
- public static void main(String[] args) {
- Test t = new Test();
- t.name = "initialvalue";
- new PassByValue().changeRefence(t);
- System.out.println(t.name);
- }
- public void changeRefence(Test f) {
- f = null;
- }
- }
- class Test {
- String name;
- }
- (Name)[Location] -> [Value at the Location]
- ---------------------
- (Ref2Foo)[223] -> 47
- (Foo)[47] -> 5
- public static void swap(StringBuffer s1, StringBuffer s2) {
- StringBuffer temp = s1;
- s1 = s2;
- s2 = temp;
- }
- public static void main(String[] args) {
- StringBuffer s1 = new StringBuffer("Hello");
- StringBuffer s2 = new StringBuffer("World");
- swap(s1, s2);
- System.out.println(s1);
- System.out.println(s2);
- }
- public static void appendWorld(StringBuffer s1) {
- s1.append(" World");
- }
- public static void main(String[] args) {
- StringBuffer s = new StringBuffer("Hello");
- appendWorld(s);
- System.out.println(s);
- }
- public static void appendWorld(String s){
- s = s+" World";
- }
- public static void main(String[] args) {
- String s = new String("Hello");
- appendWorld(s);
- System.out.println(s);
- }
- class StringWrapper {
- public String value;
- public StringWrapper(String value) {
- this.value = value;
- }
- }
- public static void appendWorld(StringWrapper s){
- s.value = s.value +" World";
- }
- public static void main(String[] args) {
- StringWrapper s = new StringWrapper("Hello");
- appendWorld(s);
- System.out.println(s.value);
- }
- public class PassByValueString {
- public static void main(String[] args) {
- new PassByValueString().caller();
- }
- public void caller() {
- String value = "Nikhil";
- boolean valueflag = false;
- String output = method(value, valueflag);
- /*
- * 'output' is insignificant in this example. we are more interested in
- * 'value' and 'valueflag'
- */
- System.out.println("output : " + output);
- System.out.println("value : " + value);
- System.out.println("valueflag : " + valueflag);
- }
- public String method(String value, boolean valueflag) {
- value = "Anand";
- valueflag = true;
- return "output";
- }
- }
- output : output
- value : Nikhil
- valueflag : false
- public class PassByValueNewString {
- public static void main(String[] args) {
- new PassByValueNewString().caller();
- }
- public void caller() {
- String value = new String("Nikhil");
- boolean valueflag = false;
- String output = method(value, valueflag);
- /*
- * 'output' is insignificant in this example. we are more interested in
- * 'value' and 'valueflag'
- */
- System.out.println("output : " + output);
- System.out.println("value : " + value);
- System.out.println("valueflag : " + valueflag);
- }
- public String method(String value, boolean valueflag) {
- value = "Anand";
- valueflag = true;
- return "output";
- }
- }
- output : output
- value : Nikhil
- valueflag : false
- public class PassByValueObjectCase1 {
- private class Student {
- int id;
- String name;
- public Student() {
- }
- public Student(int id, String name) {
- super();
- this.id = id;
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + "]";
- }
- }
- public static void main(String[] args) {
- new PassByValueObjectCase1().caller();
- }
- public void caller() {
- Student student = new Student(10, "Nikhil");
- String output = method(student);
- /*
- * 'output' is insignificant in this example. we are more interested in
- * 'student'
- */
- System.out.println("output : " + output);
- System.out.println("student : " + student);
- }
- public String method(Student student) {
- student.setName("Anand");
- return "output";
- }
- }
- output : output
- student : Student [id=10, name=Anand]
- public class PassByValueObjectCase2 {
- public static void main(String[] args) {
- new PassByValueObjectCase2().caller();
- }
- public void caller() {
- // student has the actual reference to a Student object created
- // can we change this actual reference outside the local scope? Let's see
- Student student = new Student(10, "Nikhil");
- String output = method(student);
- /*
- * 'output' is insignificant in this example. we are more interested in
- * 'student'
- */
- System.out.println("output : " + output);
- System.out.println("student : " + student); // Will it print Nikhil or Anand?
- }
- public String method(Student student) {
- student = new Student(20, "Anand");
- return "output";
- }
- }
- output : output
- student : Student [id=10, name=Nikhil]
- void getValues(int& arg1, int& arg2) {
- arg1 = 1;
- arg2 = 2;
- }
- void caller() {
- int x;
- int y;
- getValues(x, y);
- cout << "Result: " << x << " " << y << endl;
- }
- void getValues(int[] arg1, int[] arg2) {
- arg1[0] = 1;
- arg2[0] = 2;
- }
- void caller() {
- int[] x = new int[1];
- int[] y = new int[1];
- getValues(x, y);
- System.out.println("Result: " + x[0] + " " + y[0]);
- }
- public void method (String param) {}
- ...
- String var = new String("ref");
- method(var);
- method(var.toString());
- method(new String("ref"));
- public class Test {
- public static void main(String[] args) {
- Integer a = new Integer(2);
- Integer b = new Integer(3);
- System.out.println("Before: a = " + a + ", b = " + b);
- swap(a,b);
- System.out.println("After: a = " + a + ", b = " + b);
- }
- public static swap(Integer iA, Integer iB) {
- Integer tmp = iA;
- iA = iB;
- iB = tmp;
- }
- }
- public void test() {
- MyClass obj = null;
- init(obj);
- //After calling init method, obj still points to null
- //this is because obj is passed as value and not as reference.
- }
- private void init(MyClass objVar) {
- objVar = new MyClass();
- }
- public class PassByCopy{
- public static void changeName(Dog d){
- d.name = "Fido";
- }
- public static void main(String[] args){
- Dog d = new Dog("Maxx");
- System.out.println("name= "+ d.name);
- changeName(d);
- System.out.println("name= "+ d.name);
- }
- }
- class Dog{
- public String name;
- public Dog(String s){
- this.name = s;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement