ICS 23 Fundamental Data Structures Introduction to Java - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

ICS 23 Fundamental Data Structures Introduction to Java

Description:

Why is Java platform independent? Ordinary (C / C ) Program ... Your source code - (compiler) - Java bytecode (platform independent) - (Interpreter) ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 23
Provided by: richard306
Category:

less

Transcript and Presenter's Notes

Title: ICS 23 Fundamental Data Structures Introduction to Java


1
ICS 23 Fundamental Data StructuresIntroduction
to Java
  • Section 1
  • Overview of Java

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
  • Cgt java myApplication
  • We will only implement applications in ICS 23.
  • Assume you know C

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

4
Why is Java platform independent?
  • Ordinary (C / C) Program
  • Your source code -gt (compiler) -gt Executable File
    (native machine code)
  • Java Program
  • Your source code -gt (compiler) -gt Java bytecode
    (platform independent) -gt (Interpreter)

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

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 cats10 felix
  • member reference syntax
  • felix.meow() //member function
  • Color c felix.eyeColor //variable

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.

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
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.

10
A Simple Java Application
  • //FirstSample.java
  • public class FirstSample
  • public static void main(String args)
  • System.out.println(Hello, World!)

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.

12
Arrays
  • Array Declaration
  • int numbers or int numbers
  • Array Creation
  • numbers new int 2
  • int numbers 3, 2, 1
  • Dog pound new Dog3
  • pound0 new Dog(Rex, Dog.Collie)
  • Array length
  • int num numbers.length

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

14
Strings (contd)
  • 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.)

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.

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()

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

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)

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.

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

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

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)
Write a Comment
User Comments (0)
About PowerShow.com