great coders are today s rock stars will i am watch the
play

Great coders are todays rock stars Will.I.Am Watch the video at - PowerPoint PPT Presentation

Preamble Data types Call-by-value Scope ITI 1121. Introduction to Computing II Marcel Turcotte (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 11, 2015


  1. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... (a) null α ... > i n t [ ] a ; a = new i n t [ 5 ] ; ⇒ The declaration of a reference variable only allocates memory to hold a reference (sometimes called pointer or address), null is a special value (literal), which does not reference an object Marcel Turcotte ITI 1121. Introduction to Computing II

  2. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... (a) null α ... (instance) 0 β i n t [ ] a ; > a = new i n t [ 5 ] ; 0 0 0 0 ... ⇒ The creation of a new instance, new int[ 5 ] , allocates memory to hold 5 integer values (and the housekeeping information). Each cell of the array is initialized with the default int value, which is 0. Marcel Turcotte ITI 1121. Introduction to Computing II

  3. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... (a) β α ... (instance) 0 β i n t [ ] a ; a = new i n t [ 5 ] ; 0 > 0 0 0 ... ⇒ Finally, the reference of the newly created object is assigned to the location designed by the label a . Marcel Turcotte ITI 1121. Introduction to Computing II

  4. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following, ... (a) β α ... (instance) β 0 0 0 0 0 ... Marcel Turcotte ITI 1121. Introduction to Computing II

  5. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following, (a) β α ... (a) β α ... (instance) 0 β (instance) β 0 ⇒ 0 0 0 0 0 0 0 0 ... Marcel Turcotte ITI 1121. Introduction to Computing II

  6. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Because we don’t know (and shouldn’t care) about the actual memory layout, we often use memory diagrams such as the following, (a) a β α ... (a) β α ... (instance) 0 0 β (instance) β 0 ⇒ ⇒ 0 0 0 0 0 0 0 0 0 0 0 0 ... Marcel Turcotte ITI 1121. Introduction to Computing II

  7. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram In a memory diagram, I want to see: ◮ a box for every reference variable with an arrow pointing a the designated object ◮ a box for every primitive variable with the value inside the box ◮ a box for every object i n t [ ] a ; a = new i n t [ 5 ] ; Marcel Turcotte ITI 1121. Introduction to Computing II

  8. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram In a memory diagram, I want to see: ◮ a box for every reference variable with an arrow pointing a the designated object ◮ a box for every primitive variable with the value inside the box ◮ a box for every object a i n t [ ] a ; 0 ⇒ 0 a = new i n t [ 5 ] ; 0 0 0 Marcel Turcotte ITI 1121. Introduction to Computing II

  9. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Given the following class declaration: public c l a s s Constant { private f i n a l S t r i n g name ; private f i n a l double value ; public Constant ( S t r i n g name , double value ) { t h i s . name = name ; t h i s . value = value ; } } Marcel Turcotte ITI 1121. Introduction to Computing II

  10. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Given the following class declaration: public c l a s s Constant { private f i n a l S t r i n g name ; private f i n a l double value ; public Constant ( S t r i n g name , double value ) { t h i s . name = name ; t h i s . value = value ; } } Draw the memory diagram for the following statments: Constant c ; c = new Constant ( ” golden r a t i o ” , 1.61803399 ) ; Marcel Turcotte ITI 1121. Introduction to Computing II

  11. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram Marcel Turcotte ITI 1121. Introduction to Computing II

  12. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram a reference c variable of type Constant value 1.6180339887 name "golden ratio" an instance an instance of the class of the class String Constant Marcel Turcotte ITI 1121. Introduction to Computing II

  13. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Memory diagram a reference c variable of type Constant value 1.6180339887 name "golden ratio" an instance an instance of the class of the class String Constant Marcel Turcotte ITI 1121. Introduction to Computing II

  14. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Remember ◮ Variables have types ◮ Objects have classes Marcel Turcotte ITI 1121. Introduction to Computing II

  15. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Class Integer In the following examples, we’ll be using our own class Integer : c l a s s I n t e g e r { i n t value ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  16. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Class Integer In the following examples, we’ll be using our own class Integer : c l a s s I n t e g e r { i n t value ; } Usage: I n t e g e r a ; a = new I n t e g e r ( ) ; a . value = 33; a . value++; System . out . p r i n t l n ( ”a . value = ” + a . value ) ; We use the dot notation to access the value of an instance variable. Marcel Turcotte ITI 1121. Introduction to Computing II

  17. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Class Integer In the following examples, we’ll be using our own class Integer : c l a s s I n t e g e r { i n t value ; } Usage: I n t e g e r a ; a = new I n t e g e r ( ) ; a . value = 33; a . value++; System . out . p r i n t l n ( ”a . value = ” + a . value ) ; We use the dot notation to access the value of an instance variable. Java has a pre-defined class named Integer . It is called a “wrapper” class; it wraps an int value into an object. Marcel Turcotte ITI 1121. Introduction to Computing II

  18. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Class Integer Adding a constructor c l a s s I n t e g e r { i n t value ; I n t e g e r ( i n t v ) { value = v ; } } Usage: I n t e g e r a ; a = new I n t e g e r ( 33 ) ; Marcel Turcotte ITI 1121. Introduction to Computing II

  19. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... ... i 33 i null ... ... alias null ... i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; At compile time the necessary memory to hold the reference is allocated Marcel Turcotte ITI 1121. Introduction to Computing II

  20. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... ... i 33 i null ... ... alias null ... α value 33 ... i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; Creating an object ( new Integer( 33 ) ) Marcel Turcotte ITI 1121. Introduction to Computing II

  21. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... ... i 33 i α ... ... alias null ... α value 33 ... i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; Assigning the reference of that object to the reference variable i Marcel Turcotte ITI 1121. Introduction to Computing II

  22. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... ... i 33 i α ... ... alias α ... α value 33 ... i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; Copying the value of the reference variable i into alias Marcel Turcotte ITI 1121. Introduction to Computing II

  23. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... ... i 33 i ... ... alias ... value 33 ... i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; i and alias are both designating the same object! Marcel Turcotte ITI 1121. Introduction to Computing II

  24. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Primitive vs reference variables ... i alias value 33 i 33 i n t i = 33; I n t e g e r i , a l i a s ; i = new I n t e g e r ( 33 ) ; a l i a s = i ; Using the memory diagram representation Marcel Turcotte ITI 1121. Introduction to Computing II

  25. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Wrappers ◮ For every primitive type there is an associated wrapper class Marcel Turcotte ITI 1121. Introduction to Computing II

  26. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Wrappers ◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive type int Marcel Turcotte ITI 1121. Introduction to Computing II

  27. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Wrappers ◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive type int ◮ A wrapper stores a value of a primitive type inside an object Marcel Turcotte ITI 1121. Introduction to Computing II

  28. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Wrappers ◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive type int ◮ A wrapper stores a value of a primitive type inside an object ◮ This will be paramount for stacks, queues, lists and trees Marcel Turcotte ITI 1121. Introduction to Computing II

  29. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Wrappers ◮ For every primitive type there is an associated wrapper class ◮ For instance, Integer is the wrapper class for the primitive type int ◮ A wrapper stores a value of a primitive type inside an object ◮ This will be paramount for stacks, queues, lists and trees ◮ Besides holding a value, the wrapper classes possess several class methods, mainly to convert values from/to other types, e.g. Integer.parseInt( “33” ) Marcel Turcotte ITI 1121. Introduction to Computing II

  30. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; Marcel Turcotte ITI 1121. Introduction to Computing II

  31. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? Marcel Turcotte ITI 1121. Introduction to Computing II

  32. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int , but i is a reference variable, of type Integer Marcel Turcotte ITI 1121. Introduction to Computing II

  33. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int , but i is a reference variable, of type Integer ◮ In Java 1.4 or older, this would cause a compile-time error! Marcel Turcotte ITI 1121. Introduction to Computing II

  34. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int , but i is a reference variable, of type Integer ◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement! Marcel Turcotte ITI 1121. Introduction to Computing II

  35. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int , but i is a reference variable, of type Integer ◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement! Marcel Turcotte ITI 1121. Introduction to Computing II

  36. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Quiz This is valid Java statement, true or false ? I n t e g e r i = 1; ◮ If this is a valid statement, what are the implications? ◮ Okay, 1 is a value of a primitive type, int , but i is a reference variable, of type Integer ◮ In Java 1.4 or older, this would cause a compile-time error! ◮ However, in Java 5, 6 or 7, this is a valid statement! Why? Marcel Turcotte ITI 1121. Introduction to Computing II

  37. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Auto-boxing This is because Java 5, 6 and 7 automagically transform the following statement I n t e g e r i = 1; into I n t e g e r i = new I n t e g e r ( 1 ) ; Marcel Turcotte ITI 1121. Introduction to Computing II

  38. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Auto-boxing This is because Java 5, 6 and 7 automagically transform the following statement I n t e g e r i = 1; into I n t e g e r i = new I n t e g e r ( 1 ) ; This is called auto-boxing . Marcel Turcotte ITI 1121. Introduction to Computing II

  39. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Auto-unboxing Similarly, the statement i = i + 5 I n t e g e r i = 1; i = i + 5; is transformed into i = new I n t e g e r ( i . i n t V a l u e () + 5 ) ; where the value of the wrapper object designated by i is extracted, unboxed , with the method call, i.intValue() . Marcel Turcotte ITI 1121. Introduction to Computing II

  40. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Boxing/unboxing Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class. Marcel Turcotte ITI 1121. Introduction to Computing II

  41. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Boxing/unboxing Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class. The automatic conversion from primitive to reference type is called boxing , Marcel Turcotte ITI 1121. Introduction to Computing II

  42. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Boxing/unboxing Primitive Reference byte Byte short Short int Integer long Long float Float double Double bool Boolean char Character All 8 primitive types have a corresponding wrapper class. The automatic conversion from primitive to reference type is called boxing , and the conversion from reference to primitive type is called unboxing . Marcel Turcotte ITI 1121. Introduction to Computing II

  43. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Does it matter? long s1 = ( long ) 0 ; Long s2 = ( long ) 0 ; f o r ( j =0; j < 10000000; j++ ) { f o r ( j =0; j < 10000000; j++ ) { s1 = s1 + ( long ) 1 ; s2 = s2 + ( long ) 1 ; } } Marcel Turcotte ITI 1121. Introduction to Computing II

  44. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Does it matter? long s1 = ( long ) 0 ; Long s2 = ( long ) 0 ; f o r ( j =0; j < 10000000; j++ ) { f o r ( j =0; j < 10000000; j++ ) { s1 = s1 + ( long ) 1 ; s2 = s2 + ( long ) 1 ; } } 49 milliseconds 340 milliseconds Marcel Turcotte ITI 1121. Introduction to Computing II

  45. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Does it matter? long s1 = ( long ) 0 ; Long s2 = ( long ) 0 ; f o r ( j =0; j < 10000000; j++ ) { f o r ( j =0; j < 10000000; j++ ) { s1 = s1 + ( long ) 1 ; s2 = s2 + ( long ) 1 ; } } 49 milliseconds 340 milliseconds ◮ Why? Marcel Turcotte ITI 1121. Introduction to Computing II

  46. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Does it matter? long s1 = ( long ) 0 ; Long s2 = ( long ) 0 ; f o r ( j =0; j < 10000000; j++ ) { f o r ( j =0; j < 10000000; j++ ) { s1 = s1 + ( long ) 1 ; s2 = s2 + ( long ) 1 ; } } 49 milliseconds 340 milliseconds ◮ Why? On the right side, s2 is declared as a Long , hence, the line, s2 = s2 + ( long ) 1; is rewritten as, s2 = new Long ( s2 . longValue () + ( long ) 1 ) ; Marcel Turcotte ITI 1121. Introduction to Computing II

  47. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Programming tip: benchmarking your code long s t a r t , stop , e l a p s e d ; s t a r t = System . c u r r e n t T i m e M i l l i s ( ) ; // s t a r t the c l o c k for ( j =0; j < 10000000; j++ ) { s2 += ( long ) 1; // stands f o r ‘ s2 = s2 + ( long ) 1 ’ } stop = System . c u r r e n t T i m e M i l l i s ( ) ; // stop the c l o c k e l a p s e d = stop − s t a r t ; Marcel Turcotte ITI 1121. Introduction to Computing II

  48. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Programming tip: benchmarking your code long s t a r t , stop , e l a p s e d ; s t a r t = System . c u r r e n t T i m e M i l l i s ( ) ; // s t a r t the c l o c k for ( j =0; j < 10000000; j++ ) { s2 += ( long ) 1; // stands f o r ‘ s2 = s2 + ( long ) 1 ’ } stop = System . c u r r e n t T i m e M i l l i s ( ) ; // stop the c l o c k e l a p s e d = stop − s t a r t ; where System.currentTimeMillis() returns the number of milliseconds elapsed since midnight, January 1, 1970 UTC (Coordinated Universal Time). System.nanoTime() also exists. Marcel Turcotte ITI 1121. Introduction to Computing II

  49. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Billion-dollar mistake (Null reference) “I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.” Tony Hoare Marcel Turcotte ITI 1121. Introduction to Computing II

  50. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators – primitive data types Variables of primitive data types can be compared directly i n t a = 5; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } What will be printed out on the output? Marcel Turcotte ITI 1121. Introduction to Computing II

  51. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators – primitive data types Variables of primitive data types can be compared directly i n t a = 5; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } What will be printed out on the output? ⇒ Prints “ a < b ” Marcel Turcotte ITI 1121. Introduction to Computing II

  52. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... a 5 α ... b 10 β ... i n t a = 5 ; i n t b = 10; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  53. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators: primitive and reference types What will happen and why? i n t a = 5; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  54. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators: primitive and reference types What will happen and why? i n t a = 5; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

  55. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... a 5 α ... b γ β ... i n t a = 5 ; γ I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { value 5 System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

  56. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... a 5 ... b ... i n t a = 5 ; I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { value 5 System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

  57. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope a 5 i n t a = 5 ; b I n t e g e r b = new I n t e g e r ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; value 5 } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } References.java:7: operator < cannot be applied to int,java.lang.Integer if (a < b) ^ References.java:9: operator == cannot be applied to int,java.lang.Integer else if (a == b) ^ 2 errors Marcel Turcotte ITI 1121. Introduction to Computing II

  58. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will be the result? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 10 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  59. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will be the result? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 10 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } Less.java:14: operator < cannot be applied to MyInteger,MyInteger if ( a < b ) { ^ 1 error Marcel Turcotte ITI 1121. Introduction to Computing II

  60. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Remarks ◮ These error messsages are produced by pre-1.5 Java compilers ◮ Starting with Java 1.5, autoboxing masks the “problem” ◮ In order to get same behaviour with the two environments, let’s use our wrapper, MyInteger Marcel Turcotte ITI 1121. Introduction to Computing II

  61. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Class MyInteger c l a s s MyInteger { i n t value ; MyInteger ( i n t v ) { value = v ; } } Marcel Turcotte ITI 1121. Introduction to Computing II

  62. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope a 5 i n t a = 5 ; b MyInteger b = new MyInteger ( 5 ) ; i f ( a < b ) { System . out . p r i n t l n ( ”a < b” ) ; } e l s e i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; value 5 } e l s e { System . out . p r i n t l n ( ”a > b” ) ; } ◮ Fix this! Marcel Turcotte ITI 1121. Introduction to Computing II

  63. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Solution Marcel Turcotte ITI 1121. Introduction to Computing II

  64. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Solution i n t a = 5; MyInteger b = new MyInteger ( 5 ) ; i f ( a < b . value ) { System . out . p r i n t l n ( ”a i s l e s s than b” ) ; } e l s e i f ( a == b . value ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a i s g r e a t e r than b” ) ; } ⇒ Prints “ a equals b ”. Marcel Turcotte ITI 1121. Introduction to Computing II

  65. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will happen and why? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  66. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will happen and why? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } ⇒ The result is “ a does not equal b ”. Marcel Turcotte ITI 1121. Introduction to Computing II

  67. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... a ... b ... value 5 MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; ... i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; value 5 } e l s e { System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; } ... Marcel Turcotte ITI 1121. Introduction to Computing II

  68. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope ... a ... b ... value 5 MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; ... i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; value 5 } e l s e { System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; } ... ⇒ The result is “ a does not equal b ”. Marcel Turcotte ITI 1121. Introduction to Computing II

  69. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope a b value 5 MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a == b ) { System . out . p r i n t l n ( ”a e q u a l s b” ) ; } e l s e { value 5 System . out . p r i n t l n ( ”a does not e q u a l s b” ) ; } ⇒ The result is “ a does not equal b ”. Marcel Turcotte ITI 1121. Introduction to Computing II

  70. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Solution Marcel Turcotte ITI 1121. Introduction to Computing II

  71. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Solution MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not b” ) ; } where equals could have been defined as an instance method: public boolean e qu a l s ( MyInteger other ) { r e t u r n s t h i s . value == other . value ; } ⇒ Would print “ a equals b ”. Marcel Turcotte ITI 1121. Introduction to Computing II

  72. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope What will happen? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  73. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope What will happen? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; } ⇒ Prints “ a == b ”, why? Marcel Turcotte ITI 1121. Introduction to Computing II

  74. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope What will happen? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; } ⇒ Prints “ a == b ”, why? because a and b reference the same object (instance), in other words, the two memory locations are the same; we say that b is an alias for a . Marcel Turcotte ITI 1121. Introduction to Computing II

  75. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will happen? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

  76. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope Comparison operators and reference types What will happen? MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a . e qu a l s ( b ) ) { System . out . p r i n t l n ( ”a e qu a l s b” ) ; } e l s e { System . out . p r i n t l n ( ”a does not equal b” ) ; } ⇒ prints “ a equals b ” because the two values are equal. Marcel Turcotte ITI 1121. Introduction to Computing II

  77. Preamble Memory representation Data types Primitive vs reference types Call-by-value Comparison operators Scope a b MyInteger a = new MyInteger ( 5 ) ; MyInteger b = a ; i f ( a == b ) { value 5 System . out . p r i n t l n ( ”a == b” ) ; } e l s e { System . out . p r i n t l n ( ”a != b” ) ; } Marcel Turcotte ITI 1121. Introduction to Computing II

Recommend


More recommend