Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.71 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Dog aDog = new Dog("Max");
  3. Dog oldDog = aDog;
  4.  
  5. // we pass the object to foo
  6. foo(aDog);
  7. // aDog variable is still pointing to the "Max" dog when foo(...) returns
  8. aDog.getName().equals("Max"); // true
  9. aDog.getName().equals("Fifi"); // false
  10. aDog == oldDog; // true
  11. }
  12.  
  13. public static void foo(Dog d) {
  14. d.getName().equals("Max"); // true
  15. // change d inside of foo() to point to a new Dog instance "Fifi"
  16. d = new Dog("Fifi");
  17. d.getName().equals("Fifi"); // true
  18. }
  19.  
  20. public static void main(String[] args) {
  21. Dog aDog = new Dog("Max");
  22. Dog oldDog = aDog;
  23.  
  24. foo(aDog);
  25. // when foo(...) returns, the name of the dog has been changed to "Fifi"
  26. aDog.getName().equals("Fifi"); // true
  27. // but it is still the same dog:
  28. aDog == oldDog; // true
  29. }
  30.  
  31. public static void foo(Dog d) {
  32. d.getName().equals("Max"); // true
  33. // this changes the name of d to be "Fifi"
  34. d.setName("Fifi");
  35. }
  36.  
  37. Dog myDog;
  38.  
  39. Dog myDog = new Dog("Rover");
  40. foo(myDog);
  41.  
  42. public void foo(Dog someDog) {
  43. someDog.setName("Max"); // AAA
  44. someDog = new Dog("Fifi"); // BBB
  45. someDog.setName("Rowlf"); // CCC
  46. }
  47.  
  48. public class Main{
  49. public static void main(String[] args){
  50. Foo f = new Foo("f");
  51. changeReference(f); // It won't change the reference!
  52. modifyReference(f); // It will modify the object that the reference variable "f" refers to!
  53. }
  54. public static void changeReference(Foo a){
  55. Foo b = new Foo("b");
  56. a = b;
  57. }
  58. public static void modifyReference(Foo c){
  59. c.setAttribute("c");
  60. }
  61. }
  62.  
  63. Foo f = new Foo("f");
  64.  
  65. public static void changeReference(Foo a)
  66.  
  67. changeReference(f);
  68.  
  69. Foo b = new Foo("b");
  70.  
  71. 1. Person person;
  72. 2. person = new Person("Tom");
  73. 3. changeName(person);
  74. 4.
  75. 5. //I didn't use Person person below as an argument to be nice
  76. 6. static void changeName(Person anotherReferenceToTheSamePersonObject) {
  77. 7. anotherReferenceToTheSamePersonObject.setName("Jerry");
  78. 8. }
  79.  
  80. int x = 3;
  81. float y = 101.1f;
  82. boolean amIAwesome = true;
  83.  
  84. int problems = 99;
  85. String name = "Jay-Z";
  86.  
  87. JButton[] marxBros = new JButton[3];
  88. marxBros[0] = new JButton("Groucho");
  89. marxBros[1] = new JButton("Zeppo");
  90. marxBros[2] = new JButton("Harpo");
  91.  
  92. private static void shout(String name){
  93. System.out.println("There goes " + name + "!");
  94. }
  95.  
  96. public static void main(String[] args){
  97. String hisName = "John J. Jingleheimerschmitz";
  98. String myName = hisName;
  99. shout(myName);
  100. }
  101.  
  102. void cppMethod(int val, int &ref, Dog obj, Dog &objRef, Dog *objPtr, Dog *&objPtrRef)
  103. {
  104. val = 7; // Modifies the copy
  105. ref = 7; // Modifies the original variable
  106. obj.SetName("obj"); // Modifies the copy of Dog passed
  107. objRef.SetName("objRef"); // Modifies the original Dog passed
  108. objPtr->SetName("objPtr"); // Modifies the original Dog pointed to
  109. // by the copy of the pointer passed.
  110. objPtr = new Dog("newObjPtr"); // Modifies the copy of the pointer,
  111. // leaving the original object alone.
  112. objPtrRef->SetName("objRefPtr"); // Modifies the original Dog pointed to
  113. // by the original pointer passed.
  114. objPtrRef = new Dog("newObjPtrRef"); // Modifies the original pointer passed
  115. }
  116.  
  117. int main()
  118. {
  119. int a = 0;
  120. int b = 0;
  121. Dog d0 = Dog("d0");
  122. Dog d1 = Dog("d1");
  123. Dog *d2 = new Dog("d2");
  124. Dog *d3 = new Dog("d3");
  125. cppMethod(a, b, d0, d1, d2, d3);
  126. // a is still set to 0
  127. // b is now set to 7
  128. // d0 still have name "d0"
  129. // d1 now has name "objRef"
  130. // d2 now has name "objPtr"
  131. // d3 now has name "newObjPtrRef"
  132. }
  133.  
  134. public static void javaMethod(int val, Dog objPtr)
  135. {
  136. val = 7; // Modifies the copy
  137. objPtr.SetName("objPtr") // Modifies the original Dog pointed to
  138. // by the copy of the pointer passed.
  139. objPtr = new Dog("newObjPtr"); // Modifies the copy of the pointer,
  140. // leaving the original object alone.
  141. }
  142.  
  143. public static void main()
  144. {
  145. int a = 0;
  146. Dog d0 = new Dog("d0");
  147. javaMethod(a, d0);
  148. // a is still set to 0
  149. // d0 now has name "objPtr"
  150. }
  151.  
  152. private void foo(Object bar) {
  153. bar = null;
  154. }
  155.  
  156. public static void main(String[] args) {
  157. String baz = "Hah!";
  158. foo(baz);
  159. System.out.println(baz);
  160. }
  161.  
  162. public static void tricky(Point arg1, Point arg2) {
  163. arg1.x = 100;
  164. arg1.y = 100;
  165. Point temp = arg1;
  166. arg1 = arg2;
  167. arg2 = temp;
  168. }
  169. public static void main(String [] args) {
  170. Point pnt1 = new Point(0,0);
  171. Point pnt2 = new Point(0,0);
  172. System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y);
  173. System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
  174. System.out.println(" ");
  175. tricky(pnt1,pnt2);
  176. System.out.println("X1: " + pnt1.x + " Y1:" + pnt1.y);
  177. System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
  178. }
  179.  
  180. Point pnt1 = new Point(0,0);
  181. Point pnt2 = new Point(0,0);
  182.  
  183. System.out.println("X1: " + pnt1.x + " Y1: " +pnt1.y);
  184. System.out.println("X2: " + pnt2.x + " Y2: " +pnt2.y);
  185. System.out.println(" ");
  186.  
  187. X1: 0 Y1: 0
  188. X2: 0 Y2: 0
  189.  
  190. tricky(pnt1,pnt2); public void tricky(Point arg1, Point arg2);
  191.  
  192. arg1.x = 100;
  193. arg1.y = 100;
  194.  
  195. Point temp = arg1;
  196. arg1 = arg2;
  197. arg2 = temp;
  198.  
  199. X1: 0 Y1: 0
  200. X2: 0 Y2: 0
  201. X1: 100 Y1: 100
  202. X2: 0 Y2: 0
  203.  
  204. public class PassByValue {
  205. public static void main(String[] args) {
  206. Test t = new Test();
  207. t.name = "initialvalue";
  208. new PassByValue().changeValue(t);
  209. System.out.println(t.name);
  210. }
  211.  
  212. public void changeValue(Test f) {
  213. f.name = "changevalue";
  214. }
  215. }
  216.  
  217. class Test {
  218. String name;
  219. }
  220.  
  221. changevalue
  222.  
  223. Test t = new Test();
  224.  
  225. new PassByValue().changeValue(t);
  226.  
  227. public class PassByValue {
  228. public static void main(String[] args) {
  229. Test t = new Test();
  230. t.name = "initialvalue";
  231. new PassByValue().changeRefence(t);
  232. System.out.println(t.name);
  233. }
  234.  
  235. public void changeRefence(Test f) {
  236. f = null;
  237. }
  238. }
  239.  
  240. class Test {
  241. String name;
  242. }
  243.  
  244. (Name)[Location] -> [Value at the Location]
  245. ---------------------
  246. (Ref2Foo)[223] -> 47
  247. (Foo)[47] -> 5
  248.  
  249. public static void swap(StringBuffer s1, StringBuffer s2) {
  250. StringBuffer temp = s1;
  251. s1 = s2;
  252. s2 = temp;
  253. }
  254.  
  255.  
  256. public static void main(String[] args) {
  257. StringBuffer s1 = new StringBuffer("Hello");
  258. StringBuffer s2 = new StringBuffer("World");
  259. swap(s1, s2);
  260. System.out.println(s1);
  261. System.out.println(s2);
  262. }
  263.  
  264. public static void appendWorld(StringBuffer s1) {
  265. s1.append(" World");
  266. }
  267.  
  268. public static void main(String[] args) {
  269. StringBuffer s = new StringBuffer("Hello");
  270. appendWorld(s);
  271. System.out.println(s);
  272. }
  273.  
  274. public static void appendWorld(String s){
  275. s = s+" World";
  276. }
  277.  
  278. public static void main(String[] args) {
  279. String s = new String("Hello");
  280. appendWorld(s);
  281. System.out.println(s);
  282. }
  283.  
  284. class StringWrapper {
  285. public String value;
  286.  
  287. public StringWrapper(String value) {
  288. this.value = value;
  289. }
  290. }
  291.  
  292. public static void appendWorld(StringWrapper s){
  293. s.value = s.value +" World";
  294. }
  295.  
  296. public static void main(String[] args) {
  297. StringWrapper s = new StringWrapper("Hello");
  298. appendWorld(s);
  299. System.out.println(s.value);
  300. }
  301.  
  302. public class PassByValueString {
  303. public static void main(String[] args) {
  304. new PassByValueString().caller();
  305. }
  306.  
  307. public void caller() {
  308. String value = "Nikhil";
  309. boolean valueflag = false;
  310. String output = method(value, valueflag);
  311. /*
  312. * 'output' is insignificant in this example. we are more interested in
  313. * 'value' and 'valueflag'
  314. */
  315. System.out.println("output : " + output);
  316. System.out.println("value : " + value);
  317. System.out.println("valueflag : " + valueflag);
  318.  
  319. }
  320.  
  321. public String method(String value, boolean valueflag) {
  322. value = "Anand";
  323. valueflag = true;
  324. return "output";
  325. }
  326. }
  327.  
  328. output : output
  329. value : Nikhil
  330. valueflag : false
  331.  
  332. public class PassByValueNewString {
  333. public static void main(String[] args) {
  334. new PassByValueNewString().caller();
  335. }
  336.  
  337. public void caller() {
  338. String value = new String("Nikhil");
  339. boolean valueflag = false;
  340. String output = method(value, valueflag);
  341. /*
  342. * 'output' is insignificant in this example. we are more interested in
  343. * 'value' and 'valueflag'
  344. */
  345. System.out.println("output : " + output);
  346. System.out.println("value : " + value);
  347. System.out.println("valueflag : " + valueflag);
  348.  
  349. }
  350.  
  351. public String method(String value, boolean valueflag) {
  352. value = "Anand";
  353. valueflag = true;
  354. return "output";
  355. }
  356. }
  357.  
  358. output : output
  359. value : Nikhil
  360. valueflag : false
  361.  
  362. public class PassByValueObjectCase1 {
  363.  
  364. private class Student {
  365. int id;
  366. String name;
  367. public Student() {
  368. }
  369. public Student(int id, String name) {
  370. super();
  371. this.id = id;
  372. this.name = name;
  373. }
  374. public int getId() {
  375. return id;
  376. }
  377. public void setId(int id) {
  378. this.id = id;
  379. }
  380. public String getName() {
  381. return name;
  382. }
  383. public void setName(String name) {
  384. this.name = name;
  385. }
  386. @Override
  387. public String toString() {
  388. return "Student [id=" + id + ", name=" + name + "]";
  389. }
  390. }
  391.  
  392. public static void main(String[] args) {
  393. new PassByValueObjectCase1().caller();
  394. }
  395.  
  396. public void caller() {
  397. Student student = new Student(10, "Nikhil");
  398. String output = method(student);
  399. /*
  400. * 'output' is insignificant in this example. we are more interested in
  401. * 'student'
  402. */
  403. System.out.println("output : " + output);
  404. System.out.println("student : " + student);
  405. }
  406.  
  407. public String method(Student student) {
  408. student.setName("Anand");
  409. return "output";
  410. }
  411. }
  412.  
  413. output : output
  414. student : Student [id=10, name=Anand]
  415.  
  416. public class PassByValueObjectCase2 {
  417.  
  418. public static void main(String[] args) {
  419. new PassByValueObjectCase2().caller();
  420. }
  421.  
  422. public void caller() {
  423. // student has the actual reference to a Student object created
  424. // can we change this actual reference outside the local scope? Let's see
  425. Student student = new Student(10, "Nikhil");
  426. String output = method(student);
  427. /*
  428. * 'output' is insignificant in this example. we are more interested in
  429. * 'student'
  430. */
  431. System.out.println("output : " + output);
  432. System.out.println("student : " + student); // Will it print Nikhil or Anand?
  433. }
  434.  
  435. public String method(Student student) {
  436. student = new Student(20, "Anand");
  437. return "output";
  438. }
  439.  
  440. }
  441.  
  442. output : output
  443. student : Student [id=10, name=Nikhil]
  444.  
  445. void getValues(int& arg1, int& arg2) {
  446. arg1 = 1;
  447. arg2 = 2;
  448. }
  449. void caller() {
  450. int x;
  451. int y;
  452. getValues(x, y);
  453. cout << "Result: " << x << " " << y << endl;
  454. }
  455.  
  456. void getValues(int[] arg1, int[] arg2) {
  457. arg1[0] = 1;
  458. arg2[0] = 2;
  459. }
  460. void caller() {
  461. int[] x = new int[1];
  462. int[] y = new int[1];
  463. getValues(x, y);
  464. System.out.println("Result: " + x[0] + " " + y[0]);
  465. }
  466.  
  467. public void method (String param) {}
  468. ...
  469. String var = new String("ref");
  470. method(var);
  471. method(var.toString());
  472. method(new String("ref"));
  473.  
  474. public class Test {
  475. public static void main(String[] args) {
  476. Integer a = new Integer(2);
  477. Integer b = new Integer(3);
  478. System.out.println("Before: a = " + a + ", b = " + b);
  479. swap(a,b);
  480. System.out.println("After: a = " + a + ", b = " + b);
  481. }
  482.  
  483. public static swap(Integer iA, Integer iB) {
  484. Integer tmp = iA;
  485. iA = iB;
  486. iB = tmp;
  487. }
  488. }
  489.  
  490. public void test() {
  491. MyClass obj = null;
  492. init(obj);
  493. //After calling init method, obj still points to null
  494. //this is because obj is passed as value and not as reference.
  495. }
  496. private void init(MyClass objVar) {
  497. objVar = new MyClass();
  498. }
  499.  
  500. public class PassByCopy{
  501. public static void changeName(Dog d){
  502. d.name = "Fido";
  503. }
  504. public static void main(String[] args){
  505. Dog d = new Dog("Maxx");
  506. System.out.println("name= "+ d.name);
  507. changeName(d);
  508. System.out.println("name= "+ d.name);
  509. }
  510. }
  511. class Dog{
  512. public String name;
  513. public Dog(String s){
  514. this.name = s;
  515. }
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement