Class Notes (835,309)
Canada (509,088)
CSC207H1 (40)
Lecture

sept17c.docx

4 Pages
131 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
SEPTEMBER 17  Primitive aliasing not possible, only for objects o ex. ints are all primitive, not aliased  int a = 19 points directly to 19red  a and b are not aliases, so changing a has no effect on b. o new constructs an object  not for primitives  s1 and s2 are aliases, so changing s1 affects s2.  Changing what s1 refers to affects what s2 refers to. o Alias.java 1. package javabasics; 2. class Alias { 3. public static void main(String[] args) { 4. int a = 19; // a and b are not aliases 5. int b = a; // so changing a has no effect on b 6. a = 42; 7. System.out.println(a + " " + b); 8. 9. // s1 and s2 are aliases, so changing s1 affects s2. 10. // Much more precise: 11. // Changing what s1 refers to affects what s2 refers to. 12. StringBuffer s1 = new StringBuffer("Vanilla ice cream"); 13. StringBuffer s2 = s1; 14. s1.append(" with carmel sauce"); 15. System.out.println(s1 + "\n" + s2); 16. } 17. }  Output: >>> 42 19 >>> Vanilla ice cream with carmel sauce >>> Vanilla ice cream with carmel sauce  Strings are immutable o Every change constructs a new string, slow method o Change string variable to refer to new string “b”, nothing will refer to the old “a” anymore  Java keeps track of unused memory, slated for garbage collection  StringBuilder is like a string, but mutable o StringBuffer is the older way to do this o StringBuilder is faster but can’t be used w/ multiple threads o AboutStrings.java 1. package javabasics; 2. public class AboutStrings { 3. public static void main(String[] args) { 4. String a = "Joshua"; 5. String b = "Giraffe"; 6. a = a + b; // Constructs a new String. 7. 8. StringBuilder c = new StringBuilder("Baby"); 9. StringBuilder d = new StringBuilder("Beluga"); 10. c.append(d); // Mutates -- faster. 11. } 12. }  interning = creates only one object for each distinct string o when strings are created using the short form notation, w/o explicit new java usesstring interning o if using “new” creates two different object o Interning.java 1. package javabasics; 2. class Interning { 3. public static void main(String[] args) { 4. String s1 = "Happy"; // s1 and s2 are not only equivalent 5. String s2 = "Ha" + "ppy"; // they actually are the same object b/c interning 6. 7. System.out.println("Str test"); 8. System.out.println("==?: " + (s1 == s2)); 9. System.out.println("equals?: " + s1.equals(s2)); 10. } 11. } SEPTEMBER 17  Output: >>> Str test >>> ==?: true >>> equals?: true  equality o objects can be equivalent or be two of the same objects at the same location o .equals asks value equaity  All classes have an x.equals(y) method for comparing objects x & y, inherited from Object class  When its inherited, just compares the memory location, ie. ==  every class is chidern from the Object class  equivalent to == in python o == implies asks identity equality, same object at the exact same location  identity equality not value equality like python  equivalent to is in python  All primitives have a wrapper class, in its full name and capital o It lets you wrap up a value in an object for times like this when you need an object. o Integer is the wrapper class for int o Equality.java 1. package javabasics; 2. class Equality { 3. 4. public static void main(String[] args) { 5. Integer num1 = new Integer(42); // num1 and num2 are not the same object 6. Integer num2 = new Integer(42); // but they are equivalent 7. 8. System.out.println("Int test"); 9. System.out.println("==?: " + (num1 == num2)); 10. System.out.println("equals?: " + num1.equals(num2)); 11. } 12. }  Output: >>> Int test >>> ==?: false >>> equals?: true  Parameters, one variable can’t refer to more than one type in a method o Parameters.java 1. package javabasics; 2. class Parameters { 3. 4. static void increase(int i) { 5. i = i * 1000; 6. } 7. 8. static void increase(StringBuilder sb) { // overloading the method 9. sb.append(sb); 10. sb.append(sb); 11. sb.append("!"); 12. } 13. 14. public static void main(String[] args) { 15. int cost = 14; 16. increase(cost); 17. System.out.println(cost); 18. 19. StringBuilder word = new StringBuilder("rah"); 20. increase(word); 21. System.out.println(word); 22. } 23. }  Output: >>> 14 >>> Rahrahrahrah! SEPTEMBER 17  Arrays, no negative indices, arrays are objects o You can get multiple dimensions using array of array  In this case, only need to specify the length of the first axis of an arry o Unassigned array value = null o Array items defined by array of “objects/strings/ints”, al
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit