28
Object Oriented Programming Lecture 3

Object Oriented Programming Lecture 3. Introduction In discussing Java, some items need to be clarified The Java programming language The Java virtual

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Object Oriented Programming

Lecture 3

Page 2: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Introduction

In discussing Java, some items need to be clarified

The Java programming language The Java virtual machine (JVM) The Java platform

Page 3: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Java Programming Language

An object-oriented programming language, which is developed by Sun Microsystems.

A familiar C/C++ style of notation. Use the object-oriented programming

methodology. Allow the same program to be executed

on multiple operating systems. Contain build-in support for using

computer networks.

Page 4: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

The Java Platform

The Java platform, is the environment in which a program runs.

Two components: The Java virtual machine The Java application programming

interface (API)

Page 5: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

The Java Virtual Machine

The heart of the Java Platform is the concept of a "virtual machine" that executes Java bytecode programs.

Sits between the Java program and the machine it is running on.

Offers the program an “abstract computer” that executes the Java code

virtual machine isn't running on a CPU - it is being emulated on the CPU of the host machine.

Page 6: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Contd…..

Through the Java VM, the same application is capable of running on multiple platforms.

Page 7: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Java Programming Process

Page 8: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Key benefits of Java

Simple: automatic memory allocation and garbage collection

Platform independent. Network-Centric programming, Java

is designed to make distributed computing easy with the networking capability that is inherently integrated into it.

Page 9: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Java Editions

J2ME (Micro Edition) – Used to create programs that run on small handheld devices, such as phones, PDAs (personal digital assistants), and appliances.

J2SE (Standard Edition) – Used primarily to create programs for desktop computers or for many computers too large for J2ME and too small for J2EE.

J2EE (Enterprise Edition) – Used to create very large programs that run on servers managing heavy traffic and complicated transactions. These programs are the backbone of many online services, such as banking, e-commerce systems etc.

Page 10: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Installing Java

The Java Development Kit (JDK) is a collection of software available at no charge from Sun Microsystems, Inc.

The v1.6 download is available at java.sun.com.

Page 11: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Java Program Structure

A Java program always consists of one or more classes.

You typically put the program code for each class in a separate file, and

You must give each file the same name as that of the class that is defined within it.

A Java source file name must have the extension .java.

Page 12: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Examples

Page 13: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Sample Java Program

class Hello {public static void main ( String[] args )

{ System.out.println("Hello World!"); }}

Page 14: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Getting Started:

Create the source file: open a text editor, type in the code which

defines a class Hello and then save it in a file (Hello.java)

file and class name are case sensitive and must be matched exactly (except the .java part)

Java is CASE SENSITIVE!Be Careful When You Type Type all code, commands, and file names

exactly as shown. Both the compiler (javac) and launcher tool (java) are case-sensitive, HelloWorldApp helloworldapp

Page 15: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

The compiler requires the source code files to be named according to specific rules:

Correct: SimpleProgram.java Incorrect: simpleprogram.java (wrong case)

SimpleProgram.java.doc (wrong extension) Simple~1.java.doc (Microsoft Windows short-

names not allowed)

Page 16: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

(2) Compile the program: compile Hello.java by using the following

command:

javac Hello.java

it generates a file named Hello.class

Page 17: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual
Page 18: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

At the prompt, type the following command and press Enter. javac HelloWorldApp.java The compiler has generated a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.

Page 19: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

3) Run the program:run the code through:

java Hello

Note that the command is java, not javac, and you refer toHello, not Hello.java or Hello.class

Page 20: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Compiler Problems :Common Error Messages

'javac' is not recognized as an internal or external command, operable program or batch file

If you receive this error, Window cannot find the compiler (javac).

Suppose you installed the JDK in C:\jdk6. At the prompt you would type the following command and press Enter: C:\jdk6\bin\javac HelloWorldApp.java If you choose this option, you'll have to precede your javac and java

commands with C:\jdk6\bin\ each time you compile or run program.

Page 21: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Compiler Problems :Common Error Messages

Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested

If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac

HelloWorldApp.

Page 22: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Runtime Problems :Common Error Messages

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:

cd c:\java The prompt should change to C:\java>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

Page 23: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Runtime Problems :Common Error Messages

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp/class

A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename.

Exception in thread "main" java.lang.NoSuchMethodError: main

The Java VM requires that the class you execute with it have a main method at which to begin execution of your application.

Page 24: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Java Class

A class is an object oriented construct. It is designed to perform a specific task.

A Java class is defined by its class name, an open curly brace, a list of methods and fields, and a close curly brace.

The name of the class is made of alphabetical characters and digits without spaces, the first character must be alphabetical.

Page 25: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

The line public static void main ( String[] args ) shows where the program will start running. The word main means that this is the main method –

The JVM starts running any program by executing this method first.

The main method in Hello.java consists of a single statement

System.out.println("Hello World!"); The statement outputs the characters

between quotes to the console.

Page 26: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Syntax Errors

public Class Hello {public static void main ( String[]args) {System.out.println("Hello World!");}} The required word "class" has been changed to "Class"

with a capital "C". This is called a syntax error. •A syntax error is a “spelling or grammatical error" in

the program. The error message is not very clear. But at least it

shows where the error is. The compiler will not create a new bytecode file

because it stops compiling when it gets to an error.

Page 27: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Comments

// This program outputs “Hello World I am// learning Java” to screenpublic class MyFirstProgram {public static void main ( String[] args ) {// the output statement starts hereSystem.out.println("Hello World!"); // this statement outputs “HelloWorld”System.out.println(“I am learning Java”);}} A comment is a note written to a human reader of a

program. The program compiles and runs exactly the same with or without comments. Comments start with the two characters "//" (slash slash). Those characters and everything that follows them on the same line are ignored by the java compiler.

Page 28: Object Oriented Programming Lecture 3. Introduction  In discussing Java, some items need to be clarified  The Java programming language  The Java virtual

Comments

/*This is my first Java programand I am pretty excited about it.*/public class MyFirstProgram {public static void main ( String[] args ) {System.out.println("Hello World!"); // this statement outputs

“Hello World”System.out.println(“I am learning Java”);}} With this style of comment, everything between the

two characters "/*" and the two characters "*/" are ignored by the compiler. There can be many lines of comments between the "/*" and the "*/".