Vous êtes sur la page 1sur 29

Java

Knowledge Level:
Basic / Intermediate
2 Copyright 2004, Cognizant Academy, All Rights Reserved
About the Authors
Created By: Cognizant Academy
Version and
Date:
JAVAPRG/PPT/0704/2.0
3 Copyright 2004, Cognizant Academy, All Rights Reserved
Icons used
Questions References
Key Concepts
A Welcome Break
Demo
Brain Teasers
4 Copyright 2004, Cognizant Academy, All Rights Reserved
Module Information
Module
Description
After completing this Module, you will 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, Java GUI, JDBC and Networking





Level Basic
Prerequisites The participant must know Object Oriented
Programming Concepts
5 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
6 Copyright 2004, Cognizant Academy, All Rights Reserved
Module Objective & Outline
Module Flow:





4.
Errors,
exceptions,
And assertions

2.
Language
Building
Blocks

3.
Object
Oriented
Programming

5.
Garbage
collection &
Java Threads

1.
The Java
Environment
7.
Input/Output
in java

6.
Fundamental
classes &
collections
8.
AWT
Components
9.
Events
10.
Layout Manager
11.
JDBC
12.
Connecting to
Database
7 Copyright 2004, Cognizant Academy, All Rights Reserved
Module Objective & Outline
Module Flow:





14.
Networking

13.
Advanced
Concepts
8 Copyright 2004, Cognizant Academy, All Rights Reserved
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 API
3. Java runtime environment
4. Just In Time compiler (JIT)
9 Copyright 2004, Cognizant Academy, All Rights Reserved
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

10 Copyright 2004, Cognizant Academy, All Rights Reserved
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

11 Copyright 2004, Cognizant Academy, All Rights Reserved
Knowing Java Platform JVM and Java
API
The Java platform comprises of the
Java Virtual Machine (JVM) and the
Java APIs.

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
12 Copyright 2004, Cognizant Academy, All Rights Reserved
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.

13 Copyright 2004, Cognizant Academy, All Rights Reserved
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.

14 Copyright 2004, Cognizant Academy, All Rights Reserved
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.



15 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
16 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
17 Copyright 2004, Cognizant Academy, All Rights Reserved
Creating & Executing the First Java
Program
Snapshot of java program compilation and execution
18 Copyright 2004, Cognizant Academy, All Rights Reserved
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 representation
HelloWorld.class
19 Copyright 2004, Cognizant Academy, All Rights Reserved
Defining a Class
The syntax for defining a class in Java
class 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.
20 Copyright 2004, Cognizant Academy, All Rights Reserved
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);
}
}

21 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
22 Copyright 2004, Cognizant Academy, All Rights Reserved
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.




23 Copyright 2004, Cognizant Academy, All Rights Reserved
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.*;

24 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
25 Copyright 2004, Cognizant Academy, All Rights Reserved
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

26 Copyright 2004, Cognizant Academy, All Rights Reserved
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.

27 Copyright 2004, Cognizant Academy, All Rights Reserved
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.
28 Copyright 2004, Cognizant Academy, All Rights Reserved
: Quiz
The Java compiler converts the source code into what form?
Can the same Java Virtual Machine be used on different operating
systems?
What is the JRE?
If a Java file is named ShopClass.java and it contains the code
class ClassShop { }
On compiling this program as javac ShopClass.java and then
trying to run it as
java ShopClass , what is the outcome?
If the definition of main() method is ,
public static void main(String args) {},
What would be the output on compilation?
To avoid the compiler and interpreter problems, under what
conditions to modify CLASSPATH and when to modify PATH
variable ?

29 Copyright 2004, Cognizant Academy, All Rights Reserved

Vous aimerez peut-être aussi