

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The difference between primitive types and reference types in java, with examples of each. Primitive types include integers and characters, while reference types are objects, such as strings. How these types are stored in memory and demonstrates assignment and uninitialized references.
Typology: Study notes
1 / 3
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();