Introduction to Java Applications. Java Programs. Java class libraries. Learning Java CSC OBJECT ORIENTED PROGRAMMING

2/15/2011 Introduction to Java 1 Introduction to Java Applications 2 Lesson 1 CSC 108 3.0 OBJECT ORIENTED PROGRAMMING Ms. GS Makalanda Introduc...
1 downloads 2 Views 115KB Size
2/15/2011

Introduction to Java

1

Introduction to Java Applications

2

Lesson 1

CSC 108 3.0 OBJECT ORIENTED PROGRAMMING Ms. GS Makalanda

Introduction to Java

Java Programs

Java class libraries

3

4

  

Consists of classes Classes include methods Programmers can create classes they need to form Java programs



Rich collection of classes in the Java libraries 



Known as Java APIs (Application Programming Interface)

You can  

Create your own classes Use existing Java classes

Introduction to Java

Introduction to Java

Learning Java

Typical Java Development Environment

5

6



Two aspects 1. 2.



Java Language – to program your own classes Classes in the extensive Java class libraries

Edit Compile  Load  Verify  Execute  



Introduction to Java

Java programs normally go through five phases

Sun provides a Java standard Edition implementation called Java Development Kit (JDK) that includes the tools you need to write s/w in Java Introduction to Java

1

2/15/2011

Phase 1- Creating a program 7

8



Editing a file with an editor  



Type the program – source code Make necessary corrections use any text editor  Integrated development environment (IDEs) 



Eclipse, NetBeans, JBuilder



With .java extension

Example: javac Welcome.java

If the program compiles, the compiler produces a .class file 

Save the program 

Use the javac (the Java compiler) to compile the program 

 Can



Phase 2 – Compiling a Java program into Bytecodes

Welcome.class The compiled version of the program

 Welcome.java

Introduction to Java

9

Phase 2 – Compiling a Java program into Bytecodes (contd.) 



Introduction to Java

Virtual Machine - VM 10

Java compiler translates Java source code into bytecodes. Bytecodes are executed by the Java Virtual Machine (JVM)



VM is a software application that simulates a computer 





But, hides the underlying operating system and hardware from the programs that interact with the VM.

If the same VM is implemented on many computer platforms, applications that it executes can be used on all those platforms. JVM is one of the most widely used VM.

Introduction to Java

Introduction to Java

Java Bytecodes

Java Virtual Machine - JVM

11

12



Platform independent instructions 



Not dependent on a particular hardware platform

Portable 

  

The same bytecodes can execute on any platform containing a JVM.

 



Introduction to Java

A part of the JDK. The foundation of the Java platform. The JVM is invoked by the java command . Example: java Welcome

This will then initiate the steps necessary to execute the application. This begins Phase 3.

Introduction to Java

2

2/15/2011

13

Phase 3 – Loading a program into memory 

The program must be placed in memory before it can execute. 





Phase 4 – Bytecode Verification 14



Known as loading

The class loader takes the .class files containing the program’s bytecodes and transfers them to primary memory. The class loader also loads any of the .class files provided by Java that your program uses.



The bytecode verifier examines their bytecodes to ensure that they are valid, and do not violate Java’s security restrictions. Java enforces strong security 

Computer viruses and worms

Introduction to Java

Introduction to Java

Phase 5 - Execution 15

Two compilations phases 16

 

The JVM executes the program’s bytecodes. JVMs execute bytecodes using a combination of interpretation and so called just-in-time (JIT) compilation.



During compilation 

Source code  bytecode  For





Bytecodes  Machine language for the actual computer on which the program executes

Introduction to Java

Introduction to Java

First program in Java 17

portability

During Execution

Line 1 18

 



Comments Insert comments to document programs and improve their readability In Java // : end-of-line (or single line) comment /* …. */ : Traditional (or multiple-line comments)  /** … */ : Javadoc comments  

Introduction to Java

Introduction to Java

3

2/15/2011

Javadoc comments 19

Line 3 20







All text between Javadoc comment delimiters is ignored. Enable programmers to embed program documentation directly in their programs The javadoc utility program (part of the Java SE Development Kit) reads Javadoc comments and uses them to prepare your program’s documentation in HTML format.



Blank line 

Insert blank lines and spaces to make it more readable

Introduction to Java

Introduction to Java

Line 4

Line 4 (contd.)

21

22

public class Welcome1 Begins a class declaration  Every program in Java consists of at least one class declaration





  

 

Defined by the programmer

You will learn about public later

A public class must be saved in a file that has the same name as the class with the .java extension 

The class name is Welcome1 By convention, all class names in Java begin with a capital letter 

Starts with the keyword public

In terms of both spelling and capitalization

and capitalize the first letter of each word they include 

e.g. SampleClassName Introduction to Java

Introduction to Java

Lines 5 and 13

Line 7

23

24



public static void main(String args[])

Braces Left brace begins the body of every class declaration  A corresponding right brace must end the class declaration 



Lines 6- 11 are indented

   





Introduction to Java

The starting point of the program The parentheses after the identifier main, indicate that it is a method Java class declarations normally contain one or more methods For a Java application, exactly one of the methods must be called main and must be defined as above. Keyword void indicates that this method will perform a task but will not return any information when it completes its task String args[] in parentheses is a required part of the method main's declaration.

Introduction to Java

4

2/15/2011

Line 9

Method print

25

26

System.out.println("Welcome to Java Programming!");



A statement – ends with a ; System.out is the standard output object



System.out.println







Does not position cursor at the beginning of the next line System.out.print("Welcome!"); 

Allows to display characters in the command window Display the argument in command window A

line of text

Introduction to Java

Introduction to Java

Newline character 27

Escape sequence 28



Indicates to System.out.print and println methods to start on a newline.

\n \t \r \\ \"

Introduction to Java

Newline Horizontal tab Carriage return Backslash Double quote

Introduction to Java

Another program

Another program (contd.)

29

30

// Addition program that displays the sum of two numbers. import java.util.Scanner; // program uses class Scanner public class Addition { public static void main( String args[] ) { // create Scanner to obtain input from command // window Scanner input = new Scanner( System.in ); Introduction to Java

int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2

Introduction to Java

5

2/15/2011

Another program (contd.) 31

Another program (contd.) 32

System.out.print( "Enter first integer: " ); number1 = input.nextInt(); // read first number

sum = number1 + number2; // add numbers System.out.printf( "Sum is %d\n", sum );

System.out.print( "Enter second integer: " ); number2 = input.nextInt(); // read second number

} // end method main } // end class Addition

Introduction to Java

Introduction to Java

Import declaration 33

Import declaration (contd.) 34



 

Helps the compiler locate a class that is used in this program Rich set of predefined classes These classes are grouped into packages 







import java.util.Scanner; 



Named collection of related classes

Indicates that this program uses predefined Scanner class from package java.util

All import declarations must appear before the first class declaration in the file

Collectively referred to as Java class library or Java Application Programming Interface (Java API) Use import declaration to identify the predefined classes used in Java program Introduction to Java

Introduction to Java

Input data

Input data (contd.)

35

36

 

A Scanner enables a program to read data Data can come from many sources  

File on disk Keyboard

number1 = input.nextInt(); // read first number  Scanner objects input's nextInt method to obtain an integer from the user at the keyboard

Before using a Scanner, the program must create it and specify the source of the data Scanner input = new Scanner( System.in );  The standard input object, System.in, enables to read data from keyboard 

Introduction to Java

Introduction to Java

6

2/15/2011

Variable declaration statements 37

Arithmetic operators 38

int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2



Five basic operators + addition subtraction * multiplication / division % remainder

Introduction to Java

Introduction to Java

Output with printf

Output data 39

40

System.out.print( "Enter first integer: " );  System is a class.  It is a part of package java.lang  Class System is not imported with an import declaration 



Print-formatted

By default, package java.lang is imported in every Java program  Classes

in java.lang are the only ones in the Java API that do not require an import declaration Introduction to Java

Introduction to Java

Output of variables with printf() 41

42



The printf function has general form: System.out.printf("string…..", variables, numbers);

Output of variables with printf() (contd.) System.out.printf ("Some number = %d", number); Some number = 42

Introduction to Java

Introduction to Java

7

2/15/2011

printf conversions

Formatting with printf

43

44

d u x o s c f e g

signed decimal integer unsigned decimal integer hexadecimal integer octal integer string single character fixed decimal floating point scientific notation floating point use f or e, whichever is shorter

 

The conversion specifiers can be extended % [-] [fwidth] [.p] X 

%-10.3f

Introduction to Java

Introduction to Java

Formatting symbols

Examples

45

46

  

[fwidth] [-] [.p]

Object to

field width left justified. For a floating point type number of decimal places For a string number of characters

Control Spec.

Actual Output

be printed 42

%6d

|□□□□42|

42

%-6d

|42□□□□|

324

%10d

|□□□□□□□324|

-1

%-10d

|-1□□□□□□□□|

-1

%1d

|-1|(overspill)

'z'

%3c

|□□z|

'z'

%-3c

|z□□|

Introduction to Java

Introduction to Java

Examples (contd.)

Examples (contd.)

47

48

Object to

Control Spec.

Actual Output

be printed

Object to

Control Spec.

Actual Output

be printed

2.71828

%10f

|□□□2.71828|

2.71828

%10e

|2.71828e+00|

2.71828

%10.2f

|□□□□□□2.71|

2.71828

%10.2e

|□□2.17e+00|

2.71828

%-10.2f

|2.71□□□□□□|

2.71828

%10.2g

|□□□□□□2.71|

2.71828

%2.4f

|2.7182|

2.718

%.4f

|2.7180|

2.718

%10.5f

|□□□2.71800|

(overspill)

Introduction to Java

Introduction to Java

8

2/15/2011

Examples (contd.) 49

Object to

Control Spec.

Actual Output

be printed "printf"

%s

|printf|

"printf"

%10s

|□□□□printf|

"printf"

%2s

|printf|

"printf"

%5.3s

|□□pri|

"printf"

%-5.3s

|pri□□|

"printf"

%.3s

|pri|

(overspill)

Introduction to Java

9