Cmsc 150 primitive vs class variables
This presentation is the property of its rightful owner.
Sponsored Links
1 / 44

CMSC 150 primitive vs. class variables PowerPoint PPT Presentation


  • 86 Views
  • Uploaded on
  • Presentation posted in: General

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:

Download Presentation

CMSC 150 primitive vs. class variables

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


Cmsc 150 primitive vs class variables

CMSC 150primitive vs. class variables

CS 150: Fri 10 Feb 2012


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()


Cmsc 150 primitive vs class variables

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()


Cmsc 150 primitive vs class variables

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…


What is the result6

What is the result?


Red text of death

Red Text Of Death!!

in other words, Java throws an “exception”


The question is

The question is…

WHY ?!?!


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


Cmsc 150 primitive vs class variables

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


Cmsc 150 primitive vs class variables

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


Cmsc 150 primitive vs class variables

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


Cmsc 150 primitive vs class variables

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


Cmsc 150 primitive vs class variables

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


Cmsc 150 primitive vs class variables

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


  • Login