1 module objective & outline module objective: after completing this module, you will be able...

21
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java applications and fine-tune applications using threads, exception handling collections, and perform input/ output operations, JUI, JDBC and Networking concepts.

Upload: julie-hensley

Post on 31-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

1

Module Objective & Outline

Module Objective: After completing this Module, you will be able to, appreciate java as a

programming language, write java applications and fine-tune applications using threads, exception handling collections, and perform input/ output operations, JUI, JDBC and Networking concepts.

Page 2: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

2

1.0 The Java Environment : Overview

Introduction:Java is an object-oriented programming language developed by the Sun Microsystems. Java has emerged to be a very popular programming language in the development of enterprise applications.To better understand the Java language, it is necessary to understand the Java platform, the compiler and the interpreter and the entire Java environment.

Objective: After completing this topic, you will be able to,

appreciate the Java programming environment,its goal and the Java platform and write , compile and execute your first Java program.Topics included in this Module are : 1. Appreciate the advantages of Java (simple, object oriented, robust, secure,

portable, High performance, interpreted, multi-threaded, dynamic)2. Knowing the Java platform – Java virtual machine and Java API3. Java runtime environment4. Just In Time compiler (JIT)

Page 3: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

3

The Java Environment : Overview

5. Creating and executing the first Java application

6. Defining a class

7. Describing the main method

8. Importing classes and packages

9. Handling the compiler and interpreter problems

Page 4: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

4

Appreciate the Advantages of Java

• Java is an object oriented programming language developed by the Sun Microsystems. Since its inception java has become a very popular language due to its robust design and programming features.

• As written in the java language white paper by Sun Microsystems, Java – is simple– is object oriented– is distributed– is robust– is secure– is architecture neutral – is portable– provides high performance – is an interpreted language– is multithreaded language– is dynamic

Page 5: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

5

Knowing Java Platform – JVM and Java API

• The Java platform comprises of the Java Virtual Machine (JVM) and the Java API’s.

Java Virtual Machine • The Java Virtual machine is the core

part of the Java platform. The Java Virtual Machine, or JVM, is an abstract computer that runs compiled Java programs.

• The JVM is "virtual" because it is generally implemented in software on top of a "real" hardware platform and operating system.

• It provides a layer of abstraction between the compiled Java program and the underlying hardware platform and operating system.

Java Object Framework

Java Virtual Machine

Native Operating System

Java API

Fig 1.0

Page 6: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

6

Knowing Java Platform – JVM and Java API

JAVA API • The Java Application Programming Interface (API) is a collection of

classes that can be used to develop Java programs. These classes are classified under different packages. There are numerous such packages for different tasks like

– File input and output

– Networking, database access

– Security

– Threads

– Utility classes like data and many more.

Page 7: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

7

Java Runtime Environment

• The Java Runtime Environment also called as the JRE consists of the Java Virtual Machine , the Java API and other supporting files required to run Java programs.

• Java Virtual Machine (JVM) is that part of the Java Runtime Environment responsible for interpreting Java bytecode.

• JRE is the Runtime component of the Java Development Kit (JDK), without the compiler, debugger and the other tools.

• JRE is the smallest set of executables and files that constitute the standard Java Platform.

• The JRE consists of two important deployment technologies– Java Plug-In

Java Plug-In enables the execution of the applets in the browsers.

– Java Web Start

Java Web Start enables deployment of standalone applications over network.

Page 8: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

8

Just-In-Time Compiler(JIT)

• Used to improve application performance.• Part of the JVM itself.• Translates Java bytecode to native machine code at runtime. • Compiles methods on a method by method basis just before they

are called.• Calling the same method more than once will result in performance

improvement.• Using compiler + interpreter + JIT-compiler considerably speeds up

program execution, while the portability of the generated by the compiler code is preserved.

Page 9: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

9

Creating & Executing the First Java Program

Writing the java program• Lets start by writing a basic “HelloWorld.java” program.

class HelloWorld{

public static void main(String args[] ){

System.out.println (“Hello World !!!”);}

}

Compiling the program• The compiler converts the java source code into architecture

neutral bytecodes.• To compile, type javac HelloWorld.java at the command prompt.• The java compiler stores the executable as Helloworld.class in the

form of bytecode.

Page 10: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

10

Creating & Executing the First Java Program

Executing the program• After compilation of the program and formation of the

HelloWorld.class, it is ready for execution using the Java interpreter, which is a part of the JVM.

• To execute the program, type java HelloWorld at the command prompt.

• The program responds by displaying “Hello World !!!” on the screen.

Page 11: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

11

Creating & Executing the First Java Program

• Snapshot of java program compilation and execution

Page 12: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

12

Creating & Executing the First Java Program

• Understanding the complete execution flow of the HelloWorld.Java

HelloWorld.java

Java Compiler(javac)

Native Machine code

JVM(interprter)

Bytecode representationHelloWorld.class

Page 13: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

13

Defining a Class

• The syntax for defining a class in Javaclass ClassName {

/* Class body */ /* Mutiline comment*/

//Class variables //Single line comment

//Constructors and Initialiser section

//Class methods}

• Important points to consider while defining a class in a .java file– A Java file can contain more than one class.– The name of one of the classes in the program should be the same as that of

the .java file.– Any .java file may contain one and only one public class definition.

• This class should contain the definition of the main() method.

Page 14: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

14

Defining a Class

• An Example of a complete Java class :class DemoClass{

String demoDescription;

DemoClass(){ /* Constructor*/

demoDescription = “This is the Demo java class”;

}

public static void main(String args[]){ /* main method */

{

System.out.println(“This is the main method”);

DemoClass demo = new DemoClass();

demo.demoMethod();

}

public void demoMethod(){ /* other method*/

System.out.println(demoDescription);

}

}

Page 15: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

15

Describing the main() Method

The syntax for the main () method : public static void main(String args[])

{

}

• main() method must :– be declared static and public

– have return type as void

– have a single argument of type String array

• The main() method is the starting point of a stand-alone Java program.

• The JVM searches for the corresponding class file and on finding it the JVM looks for the main() method with the matching signature.

• On finding the exact match, the .class file will be interpreted by the JVM.

Page 16: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

16

Describing the main() Method

Understanding the definition of the main() method• The keyword public signifies that the main() method can be

accessed by any other Java class.• The keyword static signifies that no instance of the class is

required to invoke its main() method.• The keyword void signifies that the main() method does not return

any value to the calling function.• The keyword String specifies that the main() method receives an

array of String as argument.

Page 17: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

17

Importing Classes and Packages

• Package is a collection of logically related classes. A package consists of zero or more classes and zero or more sub-packages.

• The Java API provides a ready to use set of such packages. – E.g. java.lang package, java.io package.

• These packages can be incorporated in the Java program using the import keyword. For e.g. import java.io.*;

• There are two types of import statements – Simple-type-import

import packageName.typeName;

– Import-on-demand

import packageName.*;

Page 18: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

18

Importing Classes and Packages

• The import statement causes the Java compiler to search for the mentioned package and find out all classes in that package.

• Once a particular class in a package has been imported the class can be directly referred to by its name instead of its full-package qualifier.

Page 19: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

19

Handling Compiler and Interpreter Problems

Compiler Problems• Most common problem is not being able to locate the compiler

javac.

C:\>javac 'javac' is not recognized as an internal or external

command,operable program or batch file.• To avoid this error, the environment variable PATH must be

modified. Include the path of the directory where the JDK is installed in the PATH variable.

Syntax errors• If the program has any syntax error, the compiler displays the

corresponding error message. Eg :HelloWorld.java:5: `;' expected.

System.out.println(“Hello World !!!") ^1 error

Page 20: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

20

Handling Compiler and Interpreter Problems

Semantic errors• The compiler also checks for other basic correctness. Eg. If a local

variable is used without initialization, then the compiler issues an error. HelloWorld.java:6: variable index may not have been

initialized. index–- ; ^1 error

Interpreter problems• Common interpreter problem is not being able to find the class file

Can't find class HelloWorld.class• The JVM uses the variable CLASSPATH to find the compiled

classes.• To avoid the error , the CLASSPATH variable should include the

path of the directory where the HelloWorld.class is formed.

Page 21: 1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java

21

The Java Environment : Summary

• Java is an object-oriented language.• It is simple, object-oriented, distributed, interpreted, robust, secure,

architecture neutral, portable, multithreaded, and dynamic.• Java platform consists of the Java API and the Java Virtual Machine.• The JVM is the most important part of the Java runtime environment.• The main method must have following syntax

– public static void main(String args[] ){}• To compile the java program use javac and to run it use the java

command.• The compiler converts the source code into architecture neutral

bytecode, which is interpreted by the JVM and converted into native machine code.

• Before compiling and executing a java program, make the necessary changes in the environment variables PATH and CLASSPATH.