Ics 23 fundamental data structures introduction to java
1 / 22

ICS 23 Fundamental Data Structures - PowerPoint PPT Presentation

  • Updated On :

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

Related searches for ICS 23 Fundamental Data Structures

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'ICS 23 Fundamental Data Structures' - PamelaLan

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Ics 23 fundamental data structures introduction to java l.jpg

ICS 23 Fundamental Data StructuresIntroduction to Java

Section 1:

Overview of Java

University of California, Irvine

Java introduction l.jpg
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

Why java l.jpg
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

Why is java platform independent l.jpg
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

Java and class files l.jpg
.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

Similarities between java and c l.jpg
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

Java is not c 1 l.jpg
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

Java is not c 2 l.jpg
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

Java is not c 3 l.jpg
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

A simple java application l.jpg
A Simple Java Application


public class FirstSample


public static void main(String [] args)


System.out.println(“Hello, World!”);



University of California, Irvine

Useful java programming hints l.jpg
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

Arrays l.jpg

  • 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

Strings l.jpg

  • 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

Strings cont d l.jpg
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

Interface a kind of class l.jpg
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

Interface declaration l.jpg
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;


University of California, Irvine

Notes on using interfaces l.jpg
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

Modifier keyword static l.jpg
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:



    Math.PI //in java.lang package

    System.out // in java.lang package

    Math.pow(3, 2);

University of California, Irvine

Modifier keyword abstract l.jpg
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

Exception handling l.jpg
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

Catch an exception l.jpg
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

Hierarchy of throwable class l.jpg
Hierarchy of Throwable class




|--(abnormal condition, not caught)



|--(normal error, not caught)

|--(various exceptions that must be caught)

University of California, Irvine