Docsity

Java Programming: Understanding Primitive Types and Reference Types - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 8
Chapter 3
Read Sections 3.1 through 3.3, 3.5
Who remembers what a type is?
What’s an example of a primitive type?
What’s it look like in memory?
Java also has a second variety of types: reference types
Reference Types
Example: String
String has a set of values and operations on those values
- concatenation
- length() : int
- charAt( int i ) : char
- subString( int m, int n ) : String
What's this look like in memory?
String phrase = "My dog has fleas.";
Instead of storing the value in the memory location, Java stores a reference to the value.
We say that an object variable 'references' or 'points to' an object of that type. Thus, a
String variable references an object whose type is String.
Another example:
Scanner stdin = new Scanner( System.in );
And another:
int x = 4;
int y = 7;
int width = 5;
int height = 3;
Rectangle box = new Rectangle( x, y, width, height );
with operations (methods) such as setSize, translate, contains, equals,...
pf3

Partial preview of the text

Download Java Programming: Understanding Primitive Types and Reference Types - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 8

Chapter 3 Read Sections 3.1 through 3.3, 3. Who remembers what a type is? What’s an example of a primitive type? What’s it look like in memory? Java also has a second variety of types: reference types Reference Types Example: String String has a set of values and operations on those values

  • concatenation
  • length() : int
  • charAt( int i ) : char
  • subString( int m, int n ) : String What's this look like in memory? String phrase = "My dog has fleas."; Instead of storing the value in the memory location, Java stores a reference to the value. We say that an object variable 'references' or 'points to' an object of that type. Thus, a String variable references an object whose type is String. Another example: Scanner stdin = new Scanner( System.in ); And another: int x = 4; int y = 7; int width = 5; int height = 3; Rectangle box = new Rectangle( x, y, width, height ); with operations (methods) such as setSize, translate, contains, equals,...

String firstString = "Happy Birthday"; String anotherString = firstString; These examples get their values directly or indirectly from literal values. We've also seen new , which creates an object. In these declarations, the variable references ("points to") a newly constructed object. Assignment We can use assignment with reference variables. But, it's not the same as with primitive variables. String s1 = "Hello"; String s2 = s1; s2 refers to the same memory location as s1 - they have the same reference. Note that the assignment did not make a copy of the String - s1 and s2 point to the same String. String firstOne = "alpha"; String secondOne = firstOne; String firstOne = "beta"; Another example: String word1 = "Happy"; String word2 = "Birthday"; What happens if we have the statement? word1 = word2; What happens to "Happy"? Uninitialized References String studentName; Rectangle box; Scanner input; Can declare reference type variables like this, but they must be given a value before they are used in an expression. How do we do this? input = new Scanner( System.in ); System.out.print("Please type a student's name: "); studentName = input.nextLine();