Guest User

Untitled

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