![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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022081514/56649e215503460f94b0d549/html5/thumbnails/25.jpg)
The End