DOC PREVIEW
Duke CPS 100E - Java Basics

This preview shows page 1-2-14-15-29-30 out of 30 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 30 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 30 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 30 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 30 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 30 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 30 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 30 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Java Basics (ala Goodrich & Tamassia)Java BasicsSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Java Basics - MethodsSlide 12Slide 13Slide 14Slide 15Slide 16Java Basics - ExpressionsSlide 18Slide 19Slide 20Slide 21Java Basics – Control of FlowJava Basics – Control FlowSlide 24Java Basics – LoopsSlide 26Slide 27Slide 28Slide 29Slide 30CompSci 100EJB1.1Java Basics (ala Goodrich & Tamassia)Everything is in a class A minimal program:public class Hello { public static void main(String[] args) { System.out.println(”Hello Computer Science”); }}Output? Where? Do colors mean something?Identify the pieces . . .CompSci 100EJB1.2Java BasicsObjects Every object is an instance of a class (which defines its type)Objects contain data (state) and function (methods)StateStored in instance variables (fields, members)Can be base types (e.g. integers) or instances of (objects) of other classesFunctionExpressed as methods (subroutines, functions, procedures…)These define the behavior of objects of this classCompSci 100EJB1.3Java BasicsDeclaring a Class: public class Counter { protected int count; Counter() { count = 0; } public int getCount() { return count; } public void incCount() { count = count + 1; } public void decCount() { count = count – 1; }} Identify the methods by kind  Constructor Accessor Mutator (modifier) Note Syntax from this and previous examples Braces Semicolons Parentheses Indentifiers . . .CompSci 100EJB1.4Java BasicsClass ModifiersAbstract, final, public, default Reserved WordsMay not be used as identifiersShown in red by Eclipse and many of our examplesSee table in text (p4) or in any Java textCommentsFor human consumption: ignored by compilerInline comments: //oEffective for rest (to end) of current lineBlock comments: /* */oEffective between start and stop groupsCompSci 100EJB1.5Java BasicsPrimitive Types (base types)Built-in data types; native to most hardwareNote: not objects (will use mostly first four)boolean (1bit)int (4 bytes)double (8 bytes)char (2 bytes)Constants/Literals (by example):boolean f = false;int i = 32769;double d = 0.333333;char c = ’x’;byte (1 byte = 8 bits)short (2 bytes)long (8 bytes)float (4 bytes)byte b = 33;short s = 21;long l = 289L;float = 3.141592F;CompSci 100EJB1.6Java BasicsCreating and Using Objects (Example)public class Example { public static void main(String[] args) { Counter c; // Counter defined on a previous slide Counter d = new Counter(); c = new Counter(); System.out.println(”c = ” + c.getCount() + ” d = ” + d.getCount()); c.incCount(); d.decCount(); System.out.println(”c = ” + c.getCount() + ” d = ” + d.getCount()); d = c; // what does this really mean??? c.incCount(); d.incCount(); System.out.println(”c = ” + c.getCount() + ” d = ” + d.getCount()); }}CompSci 100EJB1.7Java BasicsString Objectsstring is a sequences of characters (char)oUnicode (16 bit)String is a built-in classoConstants: ”this is an example”String Concatenation (+) String s = ”Happy birthday to you.”; s = s + ”\n” + s; System.out.println(s); // what ?CompSci 100EJB1.8Java BasicsObject ReferencesWhen creating object with new, get location or address of new objectTypically assign this to a reference variable: Counter c = new Counter();Every object reference variable refers to object or nullNull is an important value that indicates object not created or not available.Can have multiple references to same objectAccess members of class using dot operator (“.”). Counter c = new Counter(); c.incCount();May have multiple methods with same name but different signature: e.g.: c.incCount(); c.incCount(5);CompSci 100EJB1.9Java BasicsInstance VariablesClasses have 0 or more instance variablesoAlso called fieldsoKeep state of objectMay be primitive typeoE.g. int, doubleMay be reference type (object)oE.g., String, Counter, (an array), . . .If public can:oAccess or alter reference variables using dot operator Counter c = new Counter(); System.out.println(c.count + ” = ” + c.getCount());CompSci 100EJB1.10Java BasicsVariables Modifiers: scopepublicoAnyone can accessprotectedoOnly subclass or same package may accessprivateoOnly methods of same class may access(omitted) defaultoAnyone in same package may accessOther Variable ModifiersstaticoAssociated with whole class, shared among instancesfinaloMust be initialized, then not changed: CONSTANTCompSci 100EJB1.11Java Basics - MethodsMethodsLike functions, procedure, subroutines, . . .Has header and bodySyntax: modifiers type name(parameter_declarations){ method_body }Modifiers like those of variables:opublic, private, protected, static, finalType is return type and give type of information being passed backName is any valid Java identifier nameParameters define type of info being passed into methodCompSci 100EJB1.12Java Basics - MethodsMethod modifierspublic: anyone can invoke (call)protected: only called from subclass of same packageprivate: only called from same class(omitted) (default): only called from same package abstract: has no code (dealt with in subclass)final: cannot be overridden in subclassstatic: associated with class, not with instanceReturn typesUse void is no information to be returned (procedure)Use actual type of information to be returned (function)orequires return statement(s)oonly one item returned (may be compound object, e.g., array)CompSci 100EJB1.13Java Basics - MethodsParametersParameter list may be empty (parentheses still required).Parameter list consists of comma separated pairs of types and parameter names.public void setAge(String name, int age){…}ConstructorsUsed to initialize new objectsHas same name as class and no return typepublic Counter() {count = 0;}public Professor(String aName, String aDept){ name = aName; department = aDept;}CompSci 100EJB1.14Java BasicsUsing a ConstructorInvoked using a new operatoroExamples:Professor compSciProf = new Professor(”Jeff Chase”, ”Computer Science”);Counter tally = new Counter();Class may have multiple constructors as long a


View Full Document

Duke CPS 100E - Java Basics

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Java Basics
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 Basics 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 Basics 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?