Internet Software Development

Post on 06-Jan-2016

20 views 0 download

Tags:

description

Internet Software Development. Object-Orientation and Java Paul J Krause. Object-Orientation & Java. Contents Getting Started A Little Bit of Syntax Differences between C and Java Object-Oriented Programming in Java. Getting Started. Goto: http://java.sun.com - PowerPoint PPT Presentation

Transcript of Internet Software Development

Internet Software Internet Software DevelopmentDevelopment

Object-Orientation and JavaObject-Orientation and Java

Paul J KrausePaul J Krause

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java Object-Oriented Programming in JavaObject-Oriented Programming in Java

Getting StartedGetting Started

Goto:Goto:http://java.sun.comhttp://java.sun.com

Download the Java Software Development Kit Download the Java Software Development Kit (SDK)(SDK) Its free!Its free!

Download the DocumentationDownload the Documentation Also free! (Also free! (http://java.sun.com/docs/books/tutorial)http://java.sun.com/docs/books/tutorial)

Buy Buy JAVA In a NutshellJAVA In a Nutshell, by David Flanagan, , by David Flanagan, Publ. O’RiellyPubl. O’Rielly It’ll cost you, sorry!It’ll cost you, sorry!

File extensions in JavaFile extensions in Java

.java Source

Byte code.class

javac (compiler)

JVM Java Virtual Machine

Any Hardware (that supports the JVM)

What you get in the JDKWhat you get in the JDK

appletviewerappletviewer For running AppletsFor running Applets

javacjavac Compiles .java Compiles .java .class .class

javajava Interprets a Java ClassInterprets a Java Class

classes.zipclasses.zip The system provided classesThe system provided classes

src.zipsrc.zip Complete source for standard Complete source for standard classesclasses

javadocjavadoc Generates Java HTML documentsGenerates Java HTML documents

… …

……

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java Object-Oriented Programming in JavaObject-Oriented Programming in Java

Defining a ClassDefining a Class

Account

numberbalance

credit_accountdebit_account

{membersfields

methods

public class Account {

public int number; public double balance;

public void credit(double x) {// do some sums } public void debit(double y) {// do checking then sums }}

““Circle” ExampleCircle” Example

Circle

radius

circumferencearea

public class Circle {

}

public double radius;

public double circumference() { return 2 * PI * radius;}public double area() { return PI * radius * radius;}

public static final double PI = 3.14159;

The “Circle” classThe “Circle” class

public class Circle {

// A class field public static final double PI= 3.14159; // A useful constant // A class method: just compute a value based on the arguments public static double radiansToDegrees(double rads) { return rads * 180 / PI; } // An instance field public double r; // The radius of the circle

// Two instance methods: they operate on the instance fields // of an object public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }}

The “main” methodThe “main” method

public class Circle { public double r; // The radius of the circle public double area() { // Compute the area of the circle return PI * r * r; } public double circumference() { // Compute the circumference return 2 * PI * r; }

}

public static void main(String[] args) { int input = Integer.parseInt(args[0]); Circle c = new Circle(); c.r = input; double result = c.circumference(); System.out.println(result); }

Put “main” in a new class?Put “main” in a new class?

public class MakeCircle { public static void main(String[] args) {

int input = Integer.parseInt(args[0]);

Circle c = new Circle(); c.r = input; double circum = c.circumference(); System.out.println(circum); double a = c.area(); System.out.println(a);

}}

An AssociationAn Association

MakeCircle

main

Circle

radius

circumferencearea

creates instances of

File extensions in JavaFile extensions in Java

.java Source

Byte code.class

javac (compiler)

JVM Java Virtual Machine

Any Hardware (that supports the JVM)

The Circle exampleThe Circle exampleC:\>cd Java

C:\Java>javac Circle.java

C:\Java>javac MakeCircle.java

C:\Java>java MakeCircle 4

25.1327250.26544

C:\Java>java MakeCircle 5

31.415978.53975

C:\Java contains Circle.java and MakeCircle.java

C:\Java now also contains Circle.class and MakeCircle.class

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java Object-Oriented Programming in JavaObject-Oriented Programming in Java

DifferencesDifferences

No PreprocessorNo Preprocessor No analogues of No analogues of #define#define, , #include#include, , #ifdef#ifdef

Constants are replaced by Constants are replaced by static finalstatic final fieldsfields

No Global VariablesNo Global Variables Avoids possibility of namespace collisionsAvoids possibility of namespace collisions We will see later how you can make a We will see later how you can make a

constant or variable globally accessibleconstant or variable globally accessible

Java vs. CJava vs. C

Well-defined primitive type sizesWell-defined primitive type sizes Removes this as a platform dependencyRemoves this as a platform dependency

No pointersNo pointers Although Java Classes and Arrays are Although Java Classes and Arrays are

reference types, these references are reference types, these references are “opaque”. No “address of” or “dereference” “opaque”. No “address of” or “dereference” operatorsoperators

This is This is notnot a handicap and eliminates an a handicap and eliminates an important source of bugsimportant source of bugs

Java vs. CJava vs. C

Garbage CollectionGarbage Collection Objects are “tidied away” as soon as there are Objects are “tidied away” as soon as there are

no further references to themno further references to them So, no need to explicitly manage memorySo, no need to explicitly manage memory Eliminates memory leaksEliminates memory leaks

No goto statementNo goto statement Adds exception handling and labelled Adds exception handling and labelled breakbreak

and and continuecontinue statements statements

Java vs. CJava vs. C

Variable declarations anywhereVariable declarations anywhere Java allows local variable definitions to be Java allows local variable definitions to be

made anywhere in a method or blockmade anywhere in a method or block Good practice to group them, thoughGood practice to group them, though

Forward referencesForward references Methods can be invoked before they are Methods can be invoked before they are

defined (we’ll see why it is important to be defined (we’ll see why it is important to be able to do this)able to do this)

Java vs. CJava vs. C

Method overloadingMethod overloading Multiple methods can be defined with the same name, Multiple methods can be defined with the same name,

so long as they have different parameter listsso long as they have different parameter lists

No struct and union typesNo struct and union types No enumerated typesNo enumerated types No bitfieldsNo bitfields No typedefNo typedef No method pointersNo method pointers No variable-length argument listsNo variable-length argument lists

Object-Orientation & JavaObject-Orientation & Java

ContentsContents Getting StartedGetting Started A Little Bit of SyntaxA Little Bit of Syntax Differences between C and JavaDifferences between C and Java Object-Oriented Programming in JavaObject-Oriented Programming in Java