1 / 22

ICS 23 Fundamental Data Structures Introduction to Java

ICS 23 Fundamental Data Structures Introduction to Java. Section 1: Overview of Java. Java Introduction. Visual Café for editing, executing, debugging Java You should learn this on your own during Lab Z. Applications and applets

PamelaLan
Download Presentation

ICS 23 Fundamental Data Structures Introduction to Java

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. ICS 23 Fundamental Data StructuresIntroduction to Java Section 1: Overview of Java University of California, Irvine

  2. Java Introduction • Visual Café for editing, executing, debugging Java • You should learn this on your own during Lab Z. • Applications and applets • Applets run within a web browser where a java virtual machine is installed. • Applications: stand-alone programs executed from Visual Café, or command prompt • C:> java myApplication • We will only implement applications in ICS 23. • Assume you know C++ University of California, Irvine

  3. Why Java? • Platform Independent • write once, run anywhere (in theory) • Object-Oriented • Everything must be in class • Easy to Learn • a lot like C++ University of California, Irvine

  4. Why is Java platform independent? • Ordinary (C / C++) Program Your source code -> (compiler) -> Executable File (native machine code) • Java Program Your source code -> (compiler) -> Java bytecode (platform independent) -> (Interpreter) University of California, Irvine

  5. .java and .class files • Java source code is in .java files • Each .java file can define one or more classes: class Cat { . . . } • When a .java file is compiled, one or more .class files are created • The .class files are executed by the program java University of California, Irvine

  6. Similarities between Java and C++ • Operators: + - * / += -= *= /= | & ^ ! || && ++ -- • Function declaration: void func(int x){} • flow control: if else for while do return switch { } • classes: class Cat { . . . } • object creation: Cat felix = new Cat(); • array reference syntax: cats[10] = felix; • member reference syntax: • felix.meow(); //member function • Color c = felix.eyeColor; //variable University of California, Irvine

  7. Java is not C++ 1 • No explicit pointer type variables, all references to objects are done by using implicit references. • Cat * felix; // C++ • Cat felix; // Java • No & for dereferencing a pointer type or specifying reference for variable or function arguments. • &felix; *felix // C++, not Java • In Java, all objects are manipulated by reference, so there is no need for & syntax. University of California, Irvine

  8. Java is not C++ 2 • All executable code is in a class • no global variables • no non-class functions • Automatic garbage collection. • Classes have constructors but no destructors. • When all references to a class no longer exist, the class can be “garbage collected” -- the memory it occupies is returned to the free pool • first Cat object can now be garbage collected Cat f = new Cat(); // object allocated f = new Cat(); // another obj. allocated University of California, Irvine

  9. Java is not C++ 3 • No multiple inheritance. • No operator overloading. • One exception: operator + for string concatenation. • String s = “I studied “ + hours + “ hours”; • No preprocessor. • No #define, #include, and #ifdef ... • No default parameter values. University of California, Irvine

  10. A Simple Java Application //FirstSample.java public class FirstSample { public static void main(String [] args) { System.out.println(“Hello, World!”); } } University of California, Irvine

  11. Useful Java Programming Hints • Observe good programming styles from C/C++. • Each file can contain at most one class declared as public. • The name of the public class matches the name of the file. • public class MyClass should be in file “MyClass.java”. • Java is case sensitive. Double check the case. University of California, Irvine

  12. Arrays • Array Declaration: • int [] numbers; or int numbers []; • Array Creation: • numbers = new int [2] • int [] numbers = {3, 2, 1}; • Dog[] pound = new Dog[3]; • pound[0] = new Dog(“Rex”, Dog.Collie); • Array length: • int num = numbers.length; University of California, Irvine

  13. Strings • String is a (special) Java class. • String Creation: String greeting = “Hello”; • String Concatenation: String greeting1 = “He”; String greeting2 = “llo”; String greeting = greeting1 + greeting2; • Substring: String greeting = “Hello”; String s = greeting.substring(0, 4); //Hell University of California, Irvine

  14. Strings (cont’d) • Testing for Equality • .equals() tests for same contents • == operator tests for same memory location String a = “Hello”, b = “He” + “llo”; String c = a; if (a.equals(b)) System.out.println(“Same contents.”); if (a != b) System.out.println(“Different address.”); if (a == c) System.out.println(“Same address.”); University of California, Irvine

  15. Interface - a kind of class • An interface defines a set of functions that a class will implement. • An interface can be considered as a purely abstract class with abstract methods. • Provides “add-on” features to a class. • Eliminates the need for multiple inheritance. University of California, Irvine

  16. Interface Declaration interface Drivable{ boolean startEngine(); void stopEngine(); } class Automobile implements Drivable{ public boolean startEngine(){/*do something here*/} public void stopEngine(){/*do something. here*/} // … } //somewhere in your program Automobile auto = new Automobile(); Drivable vehicle; //Declare a variable of Drivable vehicle = auto; vehicle.startEngine(); University of California, Irvine

  17. Notes on Using Interfaces • A class can implement one or more interfaces. • If a class implements an interface, it must implement all the methods defined in the interface. • An interface can extend another interface. interface Container extends Comparable University of California, Irvine

  18. Modifier Keyword static • Methods, variables and nested classes can be declared static. • Static methods, variables or nested classes belong to the (enclosing) class as a whole, not any particular class instance. • Static methods or variables are accessed by: className.variableName className.methodName(….) Math.PI //in java.lang package System.out // in java.lang package Math.pow(3, 2); University of California, Irvine

  19. Modifier Keyword abstract • An abstract method is a prototype subclasses must implement. abstract void eat (String name); // Java virtual void eat (String name) = 0; // C++ • A class that contains one or more abstract methods must be explicitly declared as an abstract class: abstract class animal{ // . . . abstract void eat (String name); } • A class can be declared as abstract even though it has no abstract methods. An abstract class cannot be instantiated. University of California, Irvine

  20. Exception Handling • An exception indicates an unusual or error condition. • Exception handling in Java separates the normal control flow from error handling flow. University of California, Irvine

  21. Catch an Exception // This static method converts String to int java.lang.Integer.parseInt(String s) throws NumberFormatException // my program: int x = 0; try { x = java.lang.Integer.parseInt(inputString); } catch(NumberFormatException nfe) { System.out.println(“Darn, “ + nfe)); x = -1; } // do something with x University of California, Irvine

  22. Hierarchy of Throwable class Java.lang.Object |--java.lang.Throwable |--java.lang.Error |--(abnormal condition, not caught) |--java.lang.Exception |--java.lang.RuntimeException |--(normal error, not caught) |--(various exceptions that must be caught) University of California, Irvine

More Related