java. why java? it’s the current “hot” language it’s almost entirely object-oriented it has...

25
Java

Upload: ferdinand-curtis

Post on 26-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Java

Page 2: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Why Java?

• It’s the current “hot” language

• It’s almost entirely object-oriented

• It has a vast library of predefined objects

• It’s platform independent (except for J++)– this makes it great for Web programming

• It’s secure

• It isn’t C++

Page 3: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Applets and applications

• An applet is a program designed to be embedded in a Web page

• Applets run in a sandbox with numerous restrictions; for example, they can’t read files

• An application is a conventional program

• Java isn't a baby language anymore!

Page 4: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Comments are almost like C++

• /* This kind of comment can span multiple lines */

• // This kind is to the end of the line• /**

* This kind of comment is a special * ‘javadoc’ style comment */

Page 5: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Primitive data types are like C

• Main data types are int, double, boolean, char

• Also have byte, short, long, float• boolean has values true and false• Declarations look like C, for example,

– double x, y;– int count = 0;

Page 6: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Expressions are like C

• Assignment statements mostly look like those in C; you can use =, +=, *= etc.

• Arithmetic uses the familiar + - * / %• Java also has ++ and --• Java has boolean operators && || !• Java has comparisons < <= == != >= >• Java does not have pointers or pointer arithmetic

Page 7: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Control statements are like C

• if (x < y) smaller = x;• if (x < y) { smaller = x; sum += x; }

else { smaller = y; sum += y; }• while (x < y) { y = y - x; }• do { y = y - x; } while (x < y)• for (int i = 0; i < max; i++) sum += i;• BUT: conditions must be boolean !

Page 8: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Control statements II

• Java also introduces the try statement, about which more later

switch (n + 1) { case 0: m = n - 1; break; case 1: m = n + 1; case 3: m = m * n; break; default: m = -n; break;}

Page 9: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Java isn't C!

• In C, almost everything is in functions

• In Java, almost everything is in classes

• Typically, there is only one class per file

• There must be only one public class per file

• The file name must be the same as the name of the public class, but with a .java extension

Page 10: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Java program layout

• A typical Java file looks like:

import java.awt.*;import java.util.*;

public class Something { // field and method definitions go here . . .}

The file must be named Something.java !

Page 11: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

What is a class?

• Early languages had only arrays– all elements had to be of the same type

• Then languages introduced structures (called records, or structs)– allowed different data types to be grouped

• Then Abstract Data Types (ADTs) became popular– grouped operations along with the data

Page 12: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

So, what is a class?

• A class consists of– a collection of fields, or variables, very like the

named fields of a struct – all the operations (called methods) that can be

performed on those fields

• A class is like a type; it describes objects

• The objects are like values of that type

Page 13: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Name conventions

• Java is case-sensitive; maxval, maxVal, and MaxVal are three different names

• Class names begin with a capital letter• All other names begin with a lowercase letter• Subsequent words are capitalized: theBigOne• Underscores are only used in constants• Constants use capitals and underscores: BIT_SIZE• These are very strong conventions!

Page 14: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

The class hierarchy

• Classes are arranged in a hierarchy

• The root, or topmost, class is Object• Every class but Object has a (one) superclass

• A class may have subclasses

• Each class inherits all the fields and methods of its superclasses

Page 15: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

An example of a class

class Person { String name; int age;

void birthday ( ) { age++; System.out.println (name + " is now " + age); }}• Person extends (is a subclass of) Object

Page 16: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Another example of a class

class Driver extends Person { long driversLicenseNumber; Date expirationDate;}

Page 17: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Creating and using an object

• Person john;john = new Person ( );john.name = "John Smith";john.age = 37;

• Person mary = new Person ( );mary.name = "Mary Brown";mary.age = 33;mary.birthday ( );

Page 18: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Arrays are objects

• Person mary = new Person ( );int myArray[ ] = new int[5];– or:

• int myArray[ ] = {1, 4, 9, 16, 25};– but not

• int myArray[5]; // syntax error– because the type of an array doesn't include its length

• String languages [ ] = {"Prolog", "Java"};

Page 19: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Errors are objects

• In most other languages, errors just "happen"

• In Java, when something goes wrong, an Error object is created and "thrown"

• You can "catch" the error if you wish, but if you don't, the program will halt with an error message

Page 20: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

Exceptions are like Errors

• An Error is a bug in the program; for example, going outside the bounds of an array

• An Exception is a problem outside the program; for example, a missing

• You don't have to do anything about an Error• But wherever an Exception might be thrown,

you must handle it

• Java provides special syntax for this

Page 21: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

try - catch

try { // code that could throw an Exception}catch (SomeSpecificException e) { // code to deal with that Exception}catch (SomeMoreGeneralException e) { // code to deal with that Exception}

Page 22: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

catch clauses

• The most general Exception is Exception

• catch clauses are tried in order, therefore– if catch (Exception e) {...} comes before

catch (IOException e) {...} , then the latter will never be reached

• In the above, e looks like a parameter because it is a parameter--it's the Exception

• Exceptions have fields and methods

Page 23: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

try - catch - finally

try { // code that could throw an Exception}catch (SomeSpecificException e) { // code to deal with that Exception}catch (SomeMoreGeneralException e) { // code to deal with that Exception}

finally { // code that always gets done }

Page 24: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

A minimal exception handler

try { line = myFile.readLine ( );}catch (Exception e) { e.printStackTrace ( );}

Page 25: Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent

The End