Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

Pre 2010

Uploaded on 08/05/2009

koofers-user-xrq-1
koofers-user-xrq-1 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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