03Reference and primitive data types

# In order to get same behaviour with the two

• Notes
• 81

This preview shows page 41 - 49 out of 81 pages.

In order to get same behaviour with the two environments, let’s use our wrapper, MyInteger Marcel Turcotte ITI 1121. Introduction to Computing II

Subscribe to view the full document.

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

Subscribe to view the full document.

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators 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 q u 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
Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators 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 q u 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

Subscribe to view the full document.

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

Subscribe to view the full document.

Preamble Data types Call-by-value Scope Memory representation Primitive vs reference types Comparison operators Solution MyInteger a = new MyInteger ( 5 ) ; MyInteger b = new MyInteger ( 5 ) ; i f ( a . e q u a l s ( b ) ) { System . out . p r i n t l n ( ”a e q u 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 q u a l s ( MyInteger other ) { r e t u r n s t h i s . value == other . value ; } Would print “ a equals b ”.
You've reached the end of this preview.
• Spring '10
• Samaan
• Marcel Turcotte

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern