* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction to Java 2 Programming
Survey
Document related concepts
Resource management (computing) wikipedia , lookup
Structured programming wikipedia , lookup
Go (programming language) wikipedia , lookup
Class (computer programming) wikipedia , lookup
Java syntax wikipedia , lookup
Design Patterns wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Scala (programming language) wikipedia , lookup
Name mangling wikipedia , lookup
String (computer science) wikipedia , lookup
String literal wikipedia , lookup
Object-oriented programming wikipedia , lookup
Java (programming language) wikipedia , lookup
Object lifetime wikipedia , lookup
C Sharp syntax wikipedia , lookup
Transcript
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools Overview • More Syntax – Constants, Arrays – Strings, the Main Method • The Object Lifecycle • Java Programming Tools • Practical Exercises 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. It’s 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; 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. 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 – Can’t be changed once created… – ..but can be concatenated (using +) to create new strings 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 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 Overview • More Syntax – Constants, Strings, Arrays • The Object Lifecycle • Java Programming Tools • Practical Exercises 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) 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 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 Overview • More Syntax – Constants, Strings, Arrays • The Object Lifecycle • Java Programming Tools • Practical Exercises 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 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. Java Programming Tools -- Javadoc /** * A <i>simple</i> Calculator * * @author Leigh Dodds */ public class Calculator { /** * Adds two numbers together and returns the result */ public int plus(int x, int y) { return x + y; } } 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. Overview • More Syntax – Constants, Strings, Arrays • The Object Lifecycle • Java Programming Tools • Practical Exercises The Calculator