ics 23 fundamental data structures introduction to java
Skip this Video
Download Presentation
ICS 23 Fundamental Data Structures Introduction to Java

Loading in 2 Seconds...

play fullscreen
1 / 22

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

  • Uploaded 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

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 Introduction to Java' - 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

ICS 23 Fundamental Data StructuresIntroduction to Java

Section 1:

Overview of Java

University of California, Irvine

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

  • 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

  • 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
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
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
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
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
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
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
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
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
Hierarchy of Throwable class




|--(abnormal condition, not caught)



|--(normal error, not caught)

|--(various exceptions that must be caught)

University of California, Irvine