DOC PREVIEW
Duke CPS 100E - Java Basics

This preview shows page 1-2-3 out of 8 pages.

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

Unformatted text preview:

CompSci 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 definesits 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 andprevious 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: //o Effective for rest (to end) of current lineþ Block comments: /* */o Effective 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)booleanintdoublecharÿ Constants (by example):boolean f = false;int i = 32769;double d = 0.333333;char c = ’x’;byteshortlongfloatbyte 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 slideCounter 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)o Unicode (16 bit)þ String is a built-in classo Constants: ”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,getlocation or address ofnew 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 createdor 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 differentsignature: e.g.: c.incCount(); c.incCount(5);CompSci 100EJB1.9Java Basicsÿ Instance Variablesþ Classes have 0 or more instance variableso Also called fieldso Keep state of objectþ May be primitive typeo E.g. int, doubleþ May be reference type (object)o E.g., String, Counter, (an array), . . .þ If public can:o Access or alter reference variables using dot operatorCounter c = new Counter();System.out.println(c.count + ”=”+c.getCount());CompSci 100EJB1.10Java Basicsÿ Variables Modifiers: scopeþ publico Anyone can accessþ protectedo Only subclass or same package may accessþ privateo Only methods of same class may accessþ (omitted) defaulto Anyone in same package may accessÿ Other Variable Modifiersþ statico Associated with whole class, shared among instancesþ finalo Must 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:o public, private, protected, static, finalþ Type is return type and give type of information beingpassed 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)o requires return statement(s)o only 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 andparameter 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 operatoro Examples:Professor compSciProf =new Professor(”Jeff Chase”, ”Computer Science”);Counter tally = new Counter();þ Class may have multiple constructors as long a signaturesare differentþ If class has no constructors defined, then a defaultconstructor is used that does not initialize anythingCompSci 100EJB1.15Java Basics - Methodsÿ The main Methodþ Required for an Applicationo This is a stand-alone Java programo Typically invoked from a command lineo Must include the following code:public static void main(String[] args){// main body of the main method}o (The parameter name args can actually be any nameyou choose.)o Argument may be used to pass command line argumentsto the program.CompSci 100EJB1.16Java Basics - Methodsÿ Blocks and Local Variablesþ Body of a method is a block:a sequence of statements and declarations enclosed in braces ( {});o Blocks may have blocks nested


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?