

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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