DOC PREVIEW
CORNELL CS 211 - Java Bootcamp

This preview shows page 1-2-3-25-26-27-28-50-51-52 out of 52 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 52 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java Bootcamp 2004We concentrate on the following Java constructs:DrJavaResources for learning JavaPrimitive typesUse mainly these typesmin, max values for primitive typesType booleanSlide 9Basic variable declarationsAssignment statementBasic initializing declarationsCastingSlide 14Type charClass StringThe ClassSlide 18Three kinds of methodThe new-expressionSlide 21Slide 22Slide 23The new-expression –used in an assignmentOur analogy for explaining classes and objectsDefault constructorStatic componentsJava class Math contains only static componentsGetter methodsSetter methodsMethod toStringKeyword this.When it appears in a method, this refers to the object in which the method occurs.Summary of classes in JavaSubclassesClass AnimalSubclass DogSubclass Dog: constructorSubclass Dog: OverridingCalling the inherited method: another use of superCasting up and down (narrowing and widening) Assume Dog and Cat are subclasses of AnimalSlide 41Why cast? Assume Dog and Cat are subclasses of AnimalApparent and real class-type of a variable? Assume Dog and Cat are subclasses of AnimalApparent class-type determines what components of object can be referencedSlide 45x[5].noise(); x[7].noise();Class Object the superest class of them allOverriding function equalsSlide 49Four kinds of variableArray: an object that contains a bunch of variables of the same type.Slide 52Java bootcamp Spring 2004 1Java Bootcamp 2004Expectations:We assume that you have studied C, C++, or Java and know about the following:• Variables and variable declarations• Expressions (integer, boolean)• Assignment statement• If-statement and if-else statement• While-loop and for-loopJava bootcamp Spring 2004 2We concentrate on the following Java constructs:•Primitive types, variables, expressions•Casting between types•The class as a definition of the format of an object (instance, manilla folder)•The new-expression•Referencing instance variables and methods•Methods (procedures, functions, constructors)•Subclasses, inheritance, and overridingJava bootcamp Spring 2004 3DrJavaWe use the IDE (Interactive Development Environment) DrJava in this course. We use it to demo during lecture. Its Interactions pane allows us to evaluate expressions and execute statements (including method calls) without having to have a complete Java application.If you have your own computer, please get on the course website, download DrJava, and practice using it. Use it to learn about Java.We’ll use it in this bootcamp.Java bootcamp Spring 2004 4Resources for learning Java See website for reading material• ProgramLive, by Gries & Gries. Has a CD, which has 250 2-4-minute lectures with synched animation. Used in CS100J this year. The glossary of the CD is a good source of information.• Course textbook.• Java Precisely.• Java in a Nutshell.• Java tutorial: http://java.sun.com/docs/books/tutorial/Java bootcamp Spring 2004 5Primitive typestype range of values space used byte –128..127 1 byteshort –32768..32767 2 bytesint –231..2314 bytes ***long –263..2638 bytesfloat 6 significant digits, 10–46..10384 bytesdouble 15 sig. digits, 10–324..103088 bytes ***char Unicode character 2 bytes ***boolean {false, true} 1 bit ***or 1 byteJava bootcamp Spring 2004 6Use mainly these typestype range of values space used int –231..2314 bytesdouble 15 sig. digits, 10–324..103088 byteschar Unicode character 2 bytesboolean {false, true} 1 bit or 1 byteOperations on type int– hh + k h – kh * k h / k h % kh / k yields an int: 7 / 2 is 3!!!!h % k is the remainder when h is divided by k.Java bootcamp Spring 2004 7min, max values for primitive typesShort.MAX_VALUE smallest short valueShort.MIN_VALUE largest short valueInteger.MAX_VALUE smallest int valueInteger.MIN_VALUE largest int valueDouble.MAX_VALUE smallest POSITIVE doubleDouble.MIN_VALUE largest POSITIVE doubleetc.Java bootcamp Spring 2004 8Type booleanValues: true and falseComplement: ! bAnd (conjunction): b && c value: if b is false, then false; otherwise, whatever c is.Or (disjunction): b || c value: if b is true, then true; otherwise, whatever c is SHORT-CIRCUIT EVALUATION x = 0 || 5 / x = 1 is true x 5 / x = 1 || x = 0 GIVES AN EXCEPTIONC, C++ use 1 and 0 for true and false. Java has a special type, with two values: true and false.0Java bootcamp Spring 2004 9Type booleanDon’t write Writeif (b == true) if ( b )if (b == false) if ( !b )if (x == 0) y= x == 0; y= true;else y= falseJava bootcamp Spring 2004 10Basic variable declarationsVariable: a name with associated integer; a named box,with a value in the box.Basic declaration: <type> <variable> ;int x; xboolean b; bWhether a variable has a specific default value when declared depends on where it is declared. Discuss later. 5 trueJava bootcamp Spring 2004 11Assignment statementsyntax: <variable> = <expression> ;semantics: Evaluate the <expression> and store its value in the <variable>.read as: <variable> becomes <expression>x= x + 1; // Add 1 to x// or add 1 to the value of xx= y; // The value of the expression is the value in // y. The value is stored in x. trueJava bootcamp Spring 2004 12Basic initializing declarationsBasic initializing declaration: <type> <variable> = <expression> ;Equivalent to:<type> <variable>; <variable>= <expression>;int x= 5 * 3; xboolean b= true; b 15 trueJava bootcamp Spring 2004 13Castingnarrowest type widest type byte -> short -> int -> long -> float -> doubleThere are no operations in types byte and short. If they appear as operand of an operation, they are promoted (automatically cast) to int or long and the operation is performed in int or long.If one operand of x + y is long, the other is cast to long and a long addition is done. Otherwise, the operation is an int addition.Java bootcamp Spring 2004 14Castingnarrowest type widest type byte -> short -> int -> long -> float -> doublebyte b= 5;b= b + 5; // illegal, because exp 5 + 5 has type int.b= (byte) (b + 5); (byte) is a prefix operator, called a caste. It casts its operand to type byte.Use any type as a caste, e.g. (int)Widening casts performed automatically.Narrowing casts have to be explicit.Java


View Full Document

CORNELL CS 211 - Java Bootcamp

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Java Bootcamp
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java Bootcamp and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java Bootcamp 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?