cmsc 150 primitive vs class variables
Download
Skip this Video
Download Presentation
CMSC 150 primitive vs. class variables

Loading in 2 Seconds...

play fullscreen
1 / 44

CMSC 150 primitive vs. class variables - PowerPoint PPT Presentation


  • 109 Views
  • Uploaded on

CMSC 150 primitive vs. class variables. CS 150: Fri 10 Feb 2012. Primitive vs. Class Variables. primitive type variables: value of interest stored directly in memory cell use assignment statement to set value reference (class) type variables:

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CMSC 150 primitive vs. class variables' - kathie


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
primitive vs class variables
Primitive vs. Class Variables
  • primitive type variables:
    • value of interest stored directly in memory cell
    • use assignment statement to set value
  • reference (class) type variables:
    • stores memory addy where object of interest resides
    • use construction

intanswerToLife = 42;

double pi = 22.0 / 7.0; // not really

Integer myInteger = new Integer( 42 );

in memory
In Memory

jenny

8675309

public class StringThing

{

public static void main(String[] args)

{

int jenny = 8675309;

booleangrammasTeeth= false;

double pi = 3.14159;

SimpleStringstr =

new SimpleString(‘H’,’i’);

}

}

in memory1
In Memory

jenny

8675309

grammasTeeth

false

public class StringThing

{

public static void main(String[] args)

{

int jenny = 8675309;

booleangrammasTeeth= false;

double pi = 3.14159;

SimpleStringstr =

new SimpleString(‘H’,’i’);

}

}

in memory2
In Memory

jenny

8675309

grammasTeeth

false

public class StringThing

{

public static void main(String[] args)

{

int jenny = 8675309;

booleangrammasTeeth= false;

double pi = 3.14159;

SimpleStringstr =

new SimpleString(‘H’,’i’);

}

}

pi

3.14159

in memory3
In Memory

jenny

8675309

grammasTeeth

false

public class StringThing

{

public static void main(String[] args)

{

int jenny = 8675309;

booleangrammasTeeth= false;

double pi = 3.14159;

SimpleStringstr =

new SimpleString(‘H’,’i’);

}

}

pi

3.14159

str

(memaddr)

myFirstChar

‘H’

mySecondChar

‘i’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result
What is the result?

myString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result1
What is the result?

myString

0x125BD

yourString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result2
What is the result?

myString

0x125BD

yourString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result3
What is the result?

myString

0x125BD

yourString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

SimpleStringourString =

new SimpleString(‘Z’,’a’);

ourString

0x18A2D

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

slide11

myString

0x125BD

myFirstChar

‘Z’

mySecondChar

‘a’

yourString

2

0x125BD

myLength

char chartAt(int index)

int length()

void printString()

ourString

0x18A2D

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

slide12

myString

0x125BD

myFirstChar

‘Z’

mySecondChar

‘a’

yourString

2

0x125BD

myLength

char chartAt(int index)

int length()

void printString()

ourString

0x18A2D

myFirstChar

‘Z’

Notice the two objects contain the

“same” data, but are two distinct objects at separate locations in memory.

Each object gets its own copies of

data & methods.

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result4
What is the result?

SimpleStringmyString;

int length = myString.length();

System.out.println( length );

what is the result5
What is the result?

SimpleStringmyString;

int length = myString.length();

System.out.println( length );

  • Won\'t even compile!
  • But let’s move the variable declaration…
red text of death
Red Text Of Death!!

in other words, Java throws an “exception”

first try printing the object
First, try printing the object…

public class StringThing

{

static SimpleStringmyString; // instance variable

public static void main(String[] args)

{

// int length = myString.length();

// System.out.println( length );

System.out.println( myString );

}

}

recall from before
Recall from before…
  • SimpleStringaPhrase;
  • aPhrase = new SimpleString(‘H’,’i’);

127

aPhrase

128

130

129

  • variable references the actual SimpleStringobject which resides elsewhere in memory

130

‘H’

‘i’

131

2

132

char chartAt(int index)

int length()

void printString()

in this context
In this context…

127

null

myString

128

129

130

131

132

no object to reference
No object to reference…

127

null

myString

128

129

130

???

131

132

printing a class type variable
Printing a Class-Type Variable

When you print a class-type variable,

Java will automatically call the toString() method (if it exists) in that class.

If the variable is not null, equivalent to

System.out.println( myString.toString() );

public class StringThing

{

static SimpleStringmyString; // instance variable

public static void main(String[] args)

{

// int length = myString.length();

// System.out.println( length );

System.out.println( myString );

}

}

a string example
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

a string example1
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

true

false

true

false

false

a string example2
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

These are "equal"

a string example3
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

These are not "equal"

a string example4
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

These are "equal"

a string example5
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

These are not "equal"

a string example6
A String Example

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

These are not "equal"

why references
Why? References

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

"str0"

127

"str1"

128

"str2"

129

130

131

132

slide31
Why?

Compiler recognizes

these as the same

constant

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

"str0"

127

"str1"

128

"str2"

129

130

131

132

slide32
Why?

Stores only one

copy in memory

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

"str0"

127

"str1"

128

"str2"

129

130

131

"Lilly"

132

slide33
Why?

Stores the start address

of the String in each variable

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

132

"str0"

127

"str1"

128

132

"str2"

129

132

130

131

"Lilly"

132

slide34
Why?

str0and str1 reference

the same String

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

132

"str0"

127

"str1"

128

132

"str2"

129

132

130

131

"Lilly"

132

slide35
Why?

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

132

"str0"

127

"str1"

128

132

"str2"

129

132

compares the address values!

"Lilly"

132

slide36
Why?

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

132

"str0"

127

"str1"

128

132

"str2"

129

132

EQUAL!

"Lilly"

132

while elsewhere in memory
While elsewhere in memory…

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

149

"str3"

145

146

149

"Lilly"

150

while elsewhere in memory1
While elsewhere in memory…

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

149

"str3"

145

"str4"

146

150

149

"Lilly"

"Lilly"

150

while elsewhere in memory2
While elsewhere in memory…

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

149

"str3"

145

"str4"

146

150

149

"Lilly"

"Lilly"

150

compares the address values!

while elsewhere in memory3
While elsewhere in memory…

public class ObjectTest {

private String str0 = "Lilly";

public ObjectTest()

{

String str1 = "Lilly";

String str2 = "Lilly";

String str3 = new String("Lilly");

String str4 = new String("Lilly");

System.out.println(str0 == str1);

System.out.println(str0 == str3);

System.out.println(str1 == str2);

System.out.println(str1 == str3);

System.out.println(str3 == str4);

}

149

"str3"

145

"str4"

146

150

149

"Lilly"

NOT EQUAL!

"Lilly"

150

moral of the story
Moral of the Story
  • Never compare equality of reference variables using ==
  • You will be comparing memory addresses
  • Not comparing contents of the actual objects
  • Use .equals() method
what is the result7
What is the result?

myString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

SimpleStringourString =

new SimpleString(‘Z’,’a’);

yourString

0x125BD

ourString

0x18A2D

myFirstChar

‘Z’

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result8
What is the result?

myString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

SimpleStringourString =

new SimpleString(‘Z’,’a’);

yourString

0x125BD

ourString

0x18A2D

myFirstChar

‘Z’

(myString == yourString)

(myString == ourString)

(myString.equals(yourString))

(myString.equals(ourString))

mySecondChar

‘a’

2

myLength

char chartAt(int index)

int length()

void printString()

what is the result9
What is the result?

myString

0x125BD

SimpleStringmyString =

new SimpleString(‘Z’,’a’);

SimpleStringyourString = myString;

SimpleStringourString =

new SimpleString(‘Z’,’a’);

yourString

0x125BD

ourString

0x18A2D

myFirstChar

‘Z’

(myString == yourString)

(myString == ourString)

(myString.equals(yourString))

(myString.equals(ourString))

TRUE

mySecondChar

‘a’

FALSE

2

myLength

TRUE

char chartAt(int index)

int length()

void printString()

TRUE

ad