1 / 13

Using the Sun Java Development Kit.

Learn how to set up the Sun Java Development Kit in Windows, create application folders, compile and run Java programs, define classes and objects, constructors, method overloading, parameter passing, static members, and garbage collection in Java.

Download Presentation

Using the Sun Java Development Kit.

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. Using the Sun Java Development Kit. Download the JDK from www.javasoft.com (Warning: it’s over 20MB!) Suppose you’ve installed it in c:\jdk1.2.1 Create an application development folder: c:\myjava\hwo1 In this folder create a file COMPILE.BAT c:\jdk1.2.1\bin\javac Myprog.java Also in this folder create a file RUN.BAT set classpath=.;c:\jdk1.2.1\lib; c:\jdk1.2.1\bin\java Myprog This assumes your main class Myprog has a static main method. CSE 341, courtesy of L. Shapiro Java 2 -

  2. Classes and Objects in Java Class Body { // (for astronomical entities) public long idNum; public String name; public Body orbits; public static long nextID = 0; public long getID( ) { return idNum; } public String getName( ) { return name; } } SYNTAX: Class <name> { <field declarations> <method definitions> } CSE 341, courtesy of L. Shapiro Java 2 -

  3. Instance Creation with new Body sun = new Body( ); sun.idNum = Body.nextID++; sun.name = "Sol "; sun.orbits = null; Body earth = new Body( ); earth.idNum = Body.nextID++; earth.name = "Earth"; earth.orbits = sun; "Earth" "Sol" CSE 341, courtesy of L. Shapiro Java 2 -

  4. Access to Fields and Methods public: accessible anywhere the class is and inherited by subclasses private: accessible ONLY in the class itself protected: accessible to subclasses and code in the same package and inherited by subclasses package: accessible only to code and inherited only by subclasses in the same package How does this compare to C++ ? CSE 341, courtesy of L. Shapiro Java 2 -

  5. Defining Constructors Class Body { public long idNum; public String name = "unnamed"; public Body orbits = null; private static long nextID = 0; Body( ) { idNum = nextID++; } Body(String bodyName, Body orbitsAround) { this(); name = bodyName; orbits = orbitsAround; } } NOTE: a constructor can invoke another constructor from the same class using this. * CSE 341, courtesy of L. Shapiro Java 2 -

  6. Use of Constructors constructor with 2 arguments Body sun = new Body("Sol", null); Body earth = new Body("Earth", sun); constructor with no arguments Body mars = new Body( ); mars.name = "Mars"; mars.orbits = sun; Overloading: 2 methods with the same name, but different signatures (different numbers or types of parameters). When are zero-argument constructors useful? CSE 341, courtesy of L. Shapiro Java 2 -

  7. The Method toString( ) Public String toString( ) { String desc = idNum + " (" + name + ")"; if (orbits != null) desc += " orbits " + orbits.toString( ); return desc; } NOTE: the toString method is special. If you provide a toString( ) method for an object, then it will be used whenever the object is used in a string concatenation. System.out.println("Body " + earth); What is the output ? CSE 341, courtesy of L. Shapiro Java 2 -

  8. Parameter Passing In Java, parameters are passed by value or by reference. Variables containing primitive types cannot be changed by a method. Class PassByValue{ public static void main(String[] args) { double one = 1.0; System.out.println("before: one = " + one); halveIt(one); System.out.println("after: one = " + one); } What will the output be ? public static void halveIt(double arg) { arg /= 2.0; System.out.println("halved: arg = " + arg); } } CSE 341, courtesy of L. Shapiro Java 2 -

  9. If a variable contains an object reference, the fields of that object can be changed. Class PassRefByValue { public static void main(String[] args) { Body sirius = new Body("Sirius", null); System.out.println("before: " + sirius); commonName(sirius); System.out.println("after: " + sirius); } public static void commonName(Body bodyRef) { bodyRef.name = "Dog Star"; bodyRef = null; } } What does this do? Does the name field of sirius change ? Does the value of sirius change to null? CSE 341, courtesy of L. Shapiro Java 2 -

  10. Static Members A member is a field or a method. A static member is a member that belongs to the class, not to instances of the class. A static field is just a class variable, such as nextID in class Body. It is assigned its initial value before any instances of the class are created. A static initialization block can be used to initialize static structures. static { <initialization statements> } A static method (also called a class method) can be used to modify static fields. CSE 341, courtesy of L. Shapiro Java 2 -

  11. Garbage Collection Java has new, but it doesn’t have free. The garbage collector is a system method that finds objects that are no longer referenced and reclaims their memory. Garbage collection is especially useful in applications that require linked structures, such as linked lists. myList  1  2  27 null myList = null; 27 null 1  2  null Dangling reference myList CSE 341, courtesy of L. Shapiro Java 2 -

  12. Example: Linked Lists in Java Public class ListNode { int Element; ListNode Next; ListNode(int NewElement, ListNode Node) { Element = NewElement; Next = Node; } } public class IntList { ListNode Head; IntList( ) { Head = new ListNode(0, null); } void InsertEnd(int NewElement) { ListNode Marker; for (Marker = Head; Marker.Next != null; Marker = Marker.Next); Marker.Next = new ListNode(NewElement, null); } } CSE 341, courtesy of L. Shapiro Java 2 -

  13. Void Delete(int DelElement) throws ListException { ListNode Marker; for (Marker = Head; Marker.Next != null && Marker.Next.Element != DelElement; Marker = Marker.Next); if (Marker.Next != null && Marker.Next.Element == DelElement) Marker.Next = Marker.Next.Next; else throw new ListException("Cannot delete: element not in list."); } What does this do when Delete(17) is invoked for the list myList? What about when Delete(1) is invoked for the list myList? What’s wrong with myList ? CSE 341, courtesy of L. Shapiro Java 2 -

More Related