Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.

Similar presentations


Presentation on theme: "Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools."— Presentation transcript:

1 Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools

2 Overview More Syntax –Constants, Arrays –Strings, the Main Method The Object Lifecycle Java Programming Tools Practical Exercises

3 More Syntax…Constants Unlike other languages, Java has no Const keyword Must use a combination of modifiers to make a constant –static – indicates a class variable. Its not owned by an individual object –final – to make a variable with a single value. Can be assigned to once –E.g. public final static int MY_CONSTANT = 0;

4 More Syntax…Arrays Fairly straightforward: –int[] myArray; –int[] myArray = {1, 2, 3}; –myArray[1]; –myArray[2] = 4; Arrays have a length property, which holds their size.

5 More Syntax…Strings Strings are objects The compiler automatically replaces any string literal with a String object –E.g. my String becomes new String(my string); Strings are immutable –Cant be changed once created… –..but can be concatenated (using +) to create new strings

6 More Syntax..Strings, StringBuffers Compiler automatically replaces String concatenation with a StringBuffer object –E.g. my String + other String becomes… –new StringBuffer(my String).append(other String).toString(); Take care with String concatenation –Explicitly using a StringBuffer is often more efficient –Can reuse buffer rather than discarding it

7 More Syntax…The main method To turn a class into an application, give it a main method: –public static void main(String[] args) Must be of this format Can then be invoked from the command-line Try to minimise the amount of code in the main method: –Create objects and invoke their behaviour

8 Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

9 Object Life-Cycle -- Creation Objects are created with the new operator –E.g. new String(my String); Causes a constructor to be invoked –Constructor is a special method, used to initialise an object –Class often specifies several constructors (for flexibility) –new operator chooses right constructor based on parameters (overloading)

10 Object Life-Cycle -- Creation public class MyClass { private int x; public MyClass(int a) { x = a; } public MyClass(String number) { x = Integer.parseInt(number); } Can then create an instance of MyClass as follows: MyClass object = new MyClass(5); //first constructor MyClass object2 = new MyClass(5); //second constructor

11 Object Life-Cycle -- Destruction No way to explicitly destroy an object –So no equivalent to C++ destructor Objects destroyed by the Garbage Collector –Once they go out of scope (I.e. no longer referenced by any variable) No way to reclaim memory, entirely under control of JVM –There is a finalize method, but its not guaranteed to be called (so pretty useless!) –Can request that the Garbage Collector can run, buts its free to ignore you

12 Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

13 Java Programming Tools The CLASSPATH –Common source of frustration! –Must set this for any of the java tools to work correctly. –Similar to PATH, but finds class files rather than executables –Basically a list of directories and Jar files in which the JVM will look for referenced classes –set CLASSPATH=%CLASSPATH%;c:\intro2java\bin Compiler and JVM executables –java, javac –Both found in %JAVA_HOME%\bin

14 Java Programming Tools -- Javadoc Javadoc –Automatically generates HTML documentation from Java source code –Extremely flexible. Can be customised in a number of different ways, including adding special tags –Very efficient way to produce development documentation for your application.

15 Java Programming Tools -- Javadoc /** * A simple Calculator * Leigh Dodds */ public class Calculator { /** * Adds two numbers together and returns the result */ public int plus(int x, int y) { return x + y; }

16 Java Programming Tools - Jar Java ARchive –Basically a zip file, used to package java classes –E.g. for delivery as an applet or application Manifest –An index of the contents of a jar file –Major benefit is indicating which class holds the main method –Allows application to be launched automatically from a jar file.

17 Overview More Syntax –Constants, Strings, Arrays The Object Lifecycle Java Programming Tools Practical Exercises

18 The Calculator


Download ppt "Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools."

Similar presentations


Ads by Google