Java: Base TypesJava: Operators for Base TypesJava: ObjectsJava: ClassesJava: Methods (Functions)Java: Sample ClassJava: Sample Class (cont)Slide 8Java StringsJava Control of Flow (by example)Slide 11What can an Object do (to itself)?Objects and valuesObjects, values, classesCompSci 100E2.1Java: Base TypesAll information has a type or class designationBuilt-in TypesCalled : primitive types or base typesboolean, char, byte, short, int, long, float, doublePrimarily use: int, double, booleanNeed to declare before using; defined (created) when declaredint x, y;// declaration only ? Initialization // some people group declarations at frontx = 39; // usey = 3 + x;int num = 10; // declaring when first usedEach type has its limitationsint: whole numbers only, limited rangedouble: implements scientific notation: fractions and wider rangeboolean: true or false values onlyCompSci 100E2.2Java: Operators for Base TypesFamiliar operators available: +, -, *, /, %For ints, / yields whole number quotient% yields the remainderWhat are o12/5 ?o13%5 ?What is the meaning of (p + c -1)/c?Also have comparison operators: <, <=, ==, !=, >=, >Do not use = (assignment) where you mean ==Learn about operator precedence from text Most pretty intuitive – well designed languageHowever, when in doubt, use parenthethesMost of these operators have no meaning for objectsCompSci 100E2.3Java: ObjectsObjects are instantiations of a classUse new operator to createInvokes constructor which initializes objectAssume we have class DLicense(Class names start with capital letter)DLicense john; // declareo(Object names (e.g. john) start with lower case letter)john = new DLicense(); // createDLicense susan = new DLicense( ); //combine DLicense bud = new License(”Ted Bud”,”6/4/1989”);oCreation may allow or require argumentsoDepends on constructor(s)CompSci 100E2.4Java: ClassesCombine data and function in one packageUse new operator to createInvokes constructor which initializes objectDataStore in instance variables (fields)oMay be primitive types oroMay be objectsMethodsOften called functions, subroutines, or proceduresUsually do things to and with the instance variablesCompSci 100E2.5Java: Methods (Functions)Classes usually define one or more methodsStructure:access return_type name(parameters) {// method body}Accessor methods return infoReturn_type indicates type of info returnedOften have empty parameter listMutator methods change stateUsually parameters are involved in making the changesOften no info returned, thus return_type is voido(Object names (e.g. john) start with lower case letter)Invoking methodsUse: object.method(params);object is implicit parameterCompSci 100E2.6Java: Sample Classpublic class DLicense { private String name; private String dob; private double height; public DLicense() { // constructor name = ””; dob = ””; height = 0.0; } public DLicense(String nm, String bd){ name = nm; dob = bd; height = 0.0; } // methods to followCompSci 100E2.7Java: Sample Class (cont) public String getName(){ return name; } public String getDOB() { return dob; } public double getHeight() { return height; } public void setHeight(double ht) { height = ht; } public String display() { return name + ” ” + height + ”\” born: ” + dob; }CompSci 100E2.8Java: Sample Class (cont) public static void main(String[] args) { DLicense john; john = new DLicense(); john.setName(”John Smith”); john.setHeight(72.0); DLicense susan = new DLicense(); susan.setName(”Sue Peggy”); susan.setHeight(66.5); DLicense bud = new License(”Ted Bud”,”6/4/1989”); bud.setHeight(68.0); System.out.println(bud.display()); System.out.println(john.getName()); System.out.println(susan.getHeight()); }}CompSci 100E2.9Java StringsString is a classDo not need new to create StringString msg = ”hello”;String constants (literals) use ”, not ’Can join strings (concatenate) with +String mail = ”John says ” + msg;Most common String methods:int length(); // get number of chars in it String substring(int start, int stop); // substring gets part of stringint indexOf(String key); //finds loc of keyCompSci 100E2.10Java Control of Flow (by example)ifif (a > b) { msg = ”good”;}if-elseif (w.hasMore()) { System.out.println(w.next()); count = count + 1;} else System.out.println(”All done”);CompSci 100E2.11Java Control of Flow (by example)whilen = 10;while (n > 0){ System.out.println(”down to ” + n); n = n – 1;}forfor (n = 10; n > 0; n--) System.out.println(”down to ” + n);do-whilen = 10;do { System.out.println(”down to ” + n); n = n – 1;} while (n > 0);CompSci 100E2.12What can an Object do (to itself)?http://www.cs.duke.edu/csed/java/jdk1.5/docs/api/index.htmlLook at java.lang.ObjecttoString()Used to print (System.out.println) an object, overriding toString() can result in 'useful' information being printed, also used in String concatenation: String s = x + y;Default is basically a pointer-valueequals()Determines if guts of two objects are the same, must override, e.g., for using a.indexOf(o) in ArrayList aDefault is ==, pointer equalityhashCode()Hashes object (guts) to value for efficient lookupCompSci 100E2.13Objects and valuesPrimitive variables are boxes think memory location with valueObject variables are labels that are put on boxesString s = new String("genome");String t = new String("genome");if (s == t) {they label the same box}if (s.equals(t)) {contents of boxes the same}stWhat's in the boxes? "genome" is in the boxesCompSci 100E2.14Objects, values, classesFor primitive types: int, char, double, booleanVariables have names and are themselves boxes (metaphorically)Two int variables assigned 17 are equal with ==For object types: String, Sequence, othersVariables have names and are labels for boxesIf no box assigned, created, then label applied to nullCan assign label to existing box (via another label)Can create new box using newObject types are references or pointers or labels to
View Full Document