CS100J, Fall 2001 Tues, 10/16 Lecture 14 ------------------------------------------------------------------------------- Announcements: + reading: Strings&Chars on Thurs: pp57-58,66-67,79-89,270-278,320-322 $for$ on Thurs and beyond: 167-175 arrays on Thurs-next week: 6.1-6.3, 6.5 (6.4 happens the following week) + P5 posted by Mon or Tues + T2 on 10/23 ------------------------------------------------------------------------------- Topics: + a bit more on static + method overloading + "constructor overloading" + strings&chars ------------------------------------------------------------------------------- class Stuff { public static int k; } public class static_basics2 { public static void main(String args[]) { // You may access class vars and methods DIRECTLY from class // as Class.field: System.out.println( ______________ ); // $static$ also means all objects from same class "know" // the static variable... setting it one object sets it for // all objects: Stuff A = new Stuff(); Stuff B = new Stuff(); ++A.k; System.out.println(B.k); ++B.k; System.out.println(A.k); } } // output? (0,1,2) ------------------------------------------------------------------------------- Method overloading: + A method may have the same name as another method if you change order of arguments, types of arguments, number of arguments, or any combination of these. + Note: The following do NOT constitute method overloading: - Changing *just* the return type - Changing *just* the names of the formal parameters ------------------------------------------------------------------------------- Constructor overloading: + no such term, but makes sense if you think of constructor as a method! + follows same rules as method overloading + Big difference: when you write a constructor, Java removes the default (or empty) constructor. ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- class Square { private int x; public Square() { } public Square(int x) { this.x=x; } public Square(int x, int y) { if (x!=y) System.out.println("Error!Using 1st input"); this.x = x; } public int getPerim() { return 4*x; } // public void getPerim() { } // won't work! public void getPerim(String s) { System.out.println(s+getPerim()); } } public class squares { public static void main(String[] args) { Square s1 = new Square(); Square s2 = new Square(1); Square s3 = new Square(2,1); System.out.println(s2.getPerim()); s3.getPerim("The perimeter is "); } } /* output: Error! Using first input. 4 The perimeter is 8 */ ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- Characters: + primitive type (pg 57) + stores integer codes for 65000+ characters (UNICODE) + deal mostly with ASCII (original 128 characters) (pg 992) - characters on keyboard - non-printing characters + escape, backspace, bell, new line + see \n, \t (pg 88) + syntax of type: char var = 'integer' - default value of char instance variable is NUL character (ASCII 0) - note: $''$ is not NUL character -- it's illegal + interesting tricks: 'A' to 'Z' has ASCII range 65 to 90 'a' to 'z' has ASCII range 97 to 122 --> 'a'-'A' is 32 --> 'Q'+'a'-'A' gives 'q' + arithmetic - character arithmetic gives integers! - can mix ints and chars - can typecast with char to int and vice versa example) System.out.println('Q'+ 'a'-'A') gives 113 To fix and get q? --> System.out.println((char) ('Q'+ 'a'-'A')) --> char tmp = 'Q'+ 'a'-'A'; System.out.println(tmp) + see Character class in API ( ------------------------------------------------------------------------------- Strings: + collections of characters + objects in Java + string literal: "stuff" (saves hassle of calling a constructor) + constructors? String s1 = new String(); // create empty string String s2 = new String("stuff"); // create string of "stuff" char[] tmp = {'a','b','c'}; // uses an array -- will see later! String s3 = new String(tmp); // create string from chars + so, what is a string literal? an instance of class String -- namely, a shortcut from calling a constructor! + immutable -- once created, cannot change! see StringBuffer class for mutable strings + Resemblence of Strings to arrays: - index of characters starts count at ZERO - find number of characters with $length()$ (not $length$) ex) string: ABCD indicies: 0123 + String methods... (see lecture example) + $equals$ - to compare contents of 2 Strings $s1$ and $s2$: $s1.equals(s2)$ - why not $==$? $==$ tests equality of references, not contents! so, $s1==s2$ test if $s1$ and $s2$ refer to the same object $==$ will check contents when comparing 2 String literals only -------------------------------------------------------------------------------