1 / 21

Internet Software Development

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

sheila
Download Presentation

Internet Software Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Software Development Object-Orientation and Java Paul J Krause

  2. Object-Orientation & Java Contents • Getting Started • A Little Bit of Syntax • Differences between C and Java • Object-Oriented Programming in Java

  3. Getting Started • Goto: http://java.sun.com • Download the Java Software Development Kit (SDK) • Its free! • Download the Documentation • Also free! (http://java.sun.com/docs/books/tutorial) • Buy JAVA In a Nutshell, by David Flanagan, Publ. O’Rielly • It’ll cost you, sorry!

  4. Java Virtual Machine Byte code Source javac (compiler) .class JVM Any Hardware (that supports the JVM) File extensions in Java .java

  5. What you get in the JDK

  6. Object-Orientation & Java Contents • Getting Started • A Little Bit of Syntax • Differences between C and Java • Object-Oriented Programming in Java

  7. { members Defining a Class Account 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 } } number balance fields credit_account debit_account methods

  8. Circle radius circumference area “Circle” Example public class Circle { } public static final double PI = 3.14159; public double radius; public double circumference() { return 2 * PI * radius; } public double area() { return PI * radius * radius; }

  9. The “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; } }

  10. The “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); }

  11. 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); } }

  12. Circle MakeCircle radius creates instances of main circumference area An Association

  13. Java Virtual Machine Byte code Source javac (compiler) .class JVM Any Hardware (that supports the JVM) File extensions in Java .java

  14. The Circle example C:\Java contains Circle.java and MakeCircle.java C:\>cd Java C:\Java>javac Circle.java C:\Java>javac MakeCircle.java C:\Java now also contains Circle.class and MakeCircle.class C:\Java>java MakeCircle 4 25.13272 50.26544 C:\Java>java MakeCircle 5 31.4159 78.53975

  15. Object-Orientation & Java Contents • Getting Started • A Little Bit of Syntax • Differences between C and Java • Object-Oriented Programming in Java

  16. Differences • No Preprocessor • No analogues of #define, #include, #ifdef • Constants are replaced by static final fields • No Global Variables • Avoids possibility of namespace collisions • We will see later how you can make a constant or variable globally accessible

  17. Java vs. C • Well-defined primitive type sizes • Removes this as a platform dependency • No pointers • Although Java Classes and Arrays are reference types, these references are “opaque”. No “address of” or “dereference” operators • This is not a handicap and eliminates an important source of bugs

  18. Java vs. C • Garbage Collection • Objects are “tidied away” as soon as there are no further references to them • So, no need to explicitly manage memory • Eliminates memory leaks • No goto statement • Adds exception handling and labelled break and continue statements

  19. Java vs. C • Variable declarations anywhere • Java allows local variable definitions to be made anywhere in a method or block • Good practice to group them, though • Forward references • Methods can be invoked before they are defined (we’ll see why it is important to be able to do this)

  20. Java vs. C • Method overloading • Multiple methods can be defined with the same name, so long as they have different parameter lists • No struct and union types • No enumerated types • No bitfields • No typedef • No method pointers • No variable-length argument lists

  21. Object-Orientation & Java Contents • Getting Started • A Little Bit of Syntax • Differences between C and Java • Object-Oriented Programming in Java

More Related