Está en la página 1de 44

J2SE Core Java

PG-DESD Aug-2013

J2SE Core Java


Objective: To introduce the student to Core Java Prerequisites: Knowledge of objectoriented programming Evaluation method:
Theory exam (objective/subjective/both) 40% weightage Lab exam 40% weightage Assignments 20% weightage

List of Books / Other training material Courseware:


The Complete Reference Java Eight Edition, Herbert Schidt/ TMH

Reference:
Beginning Java 2 by Ivor Horton; Wrox Publication

Session 1:
Learning Objectives
List and explain Java features Differentiate between C++ and Java

Write a simple Java program HelloWorld

Programming Language Levels


A programming language specifies the words and symbols that we can use to write a program by following certain rules There are three programming language levels: Machine language Assembly language High-level language Each type of CPU has its own specific machine language The other levels were created to make it easier for a human being to write programs

Language Translators

Machine language is the only language capable of directly instructing the CPU. Every non machine language program instruction must be translated into machine language prior to execution. Language Translators convert High-level code into Machine language. Interpreters translate one program statement at a time, as the program is running. Compilers translate a complete program into machine language, then the machine language program is executed as needed. Because compiled programs run faster than programs that are translated line by line by an interpreter, programmers usually choose compilers to translate frequently run business programs.

Java History
Computer language innovation and development occurs for two fundamental reasons:

1) To adapt to the changing environments and uses 2) To implement improvements in the art of programming
The development of Java was driven by both in equal measures. Many Java features are inherited from the earlier languages: C C++ Java

Before Java: C
Designed by Dennis Ritchie in 1972. Before C: BASIC, COBOL, FORTRAN, PASCAL

C is structured, efficient, high-level language that could replace assembly code when creating systems programs.

Before Java: C++


Designed by Bjarne Stroustrup in 1979. In response to the increased complexity of programs and respective improvements in the programming paradigms and methods: 1) Assembler languages 2) High-level languages

3) Structured programming 4) Object-oriented programming (OOP)


OOP a methodology that helps organize complex programs through the use of inheritance, encapsulation and polymorphism. C++ extends C by adding object-oriented features.

Java: History
In 1990, Sun Microsystems started a project called Green. Objective: To develop software for consumer electronics. Project was assigned to James Gosling, a veteran of classic network software design. Others included Patrick Naughton, ChrisWarth, Ed Frank, and Mike Sheridan. The team started writing programs in C++ for embedding into Set top boxes Washing machines VCRs - Ovens Aim was to make these appliances more intelligent.

Java: History
C++ is powerful, but also dangerous.

The power and popularity of C derived from the extensive use of pointers.
Incorrect use of pointers can cause memory leaks, leading the program to crash.

Replacing pointers by references, and automating memory management was the proposed solution.

Java: History
Hence, the team built a new programming language called Oak, which avoided potentially dangerous constructs in C++, such as pointers, pointer arithmetic, operator overloading etc.

Introduced automatic memory management, freeing the programmer to concentrate on other things.
Architecture neutrality (Platform independence) Many different CPUs are used as controllers in consumer electronic devices (They may change as per the new trends in technology). So, the software and programming language had to be architecture neutral.

Java: History
It was soon realized that these design goals of consumer electronics perfectly suited an ideal programming language for the Internet and WWW, which should be: Object-oriented (& support GUI) Robust

Architecture neutral

Internet programming presented a BIG business opportunity. Much bigger than programming for consumer electronics. Java was re-targeted for the Internet The team was expanded to include Bill Joy (developer of Unix), Arthur van Hoff, Jonathan Payne, Frank Yellin, Tim Lindholm etc. In 1994, an early web browser called WebRunner was written in Oak.

WebRunner was later renamed HotJava.

In 1995, Oak was renamed Java. A common story is that the name Java relates to the place from where the development team got its coffee. The name Java survived the

trade mark search.

What is Java?

Programming language Another programming language using which we can develop applets, standalone applications, web applications and enterprise applications. Platform Independent A Java program written and compiled on one machine can be executed on any other machine (irrespective of the operating system/architecture)

Object Oriented Complies to object oriented programming concepts. Your program is not object oriented unless you code that way
Compiled and Interpreted The .java file is compiled to a .class file and the .class file is interpreted to machine code

The Java Buzzwords


The key considerations were summed up by the Java

team in the following list of buzzwords:


Simple Secure Portable Object-oriented Robust Multithreaded Architecture-neutral Interpreted High performance Distributed Dynamic

The Java Buzzwords


Simple Java is designed to be easy for the professional

programmer to learn and use.

Object-oriented: A clean, usable, pragmatic approach to objects Robust: Restricts the programmer to find the mistakes early,

performs compile-time (strong typing) and run-time (exceptionhandling) checks, manages memory automatically.

Multithreaded: Supports multi-threaded programming for writing program that perform concurrent computations Architecture-neutral and Portable: Java Virtual Machine provides a platform independent environment for the execution of Java byte code and can be ported to any machine.

The Java Buzzwords


Interpreted and High-performance: Java programs are compiled into an intermediate representation byte code:

a) Later interpreted by any JVM b) Translated into the native machine code (JIT)

Distributed: Java handles TCP/IP protocols, accessing a resource through its URL much like accessing a local file

Dynamic: Substantial amounts of run-time type information to verify and resolve access to objects at run-time. Secure: Programs are confined to the Java execution environment

and cannot access other parts of the computer.

The Java Platform


A platform is the hardware and software environment in which a program runs.

The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other, hardwarebased platforms.
The Java platform has two components:

The Java Virtual Machine (Java VM)


The Java Application Programming Interface (Java API) The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets. The Java API is grouped into libraries (packages) of related components which allow you to do various things.

Versions of Java
Java Language vs. Java Platform Current version of the language is 1.7 (JDK 7) Core language plus additional APIs is called the Java 2 platform Three versions of the Java 2 Platform, targeted at different uses Java 2 Standard Edition (J2SE) The basic platform, which this module will cover Java 2 Enterprise Edition (J2EE) For business applications, web services, mission-critical systems Transaction processing, databases, distribution, replication Java 2 Micro Edition (J2ME) Very small Java environment for smart cards, mobile phones, and set-top boxes Subset of the standard Java libraries aimed at limited size and processing power

The Java SE Platform


Develop and deploy Java applications on desktops and servers and embedded environments..

The Java SE Platform


Develop and deploy Java applications on desktops and servers and embedded environments..

Introduction Java Virtual Machine

.java file

Java Compiler

.class file

Java Virtual Machine

Mac

Microsoft

UNIX
21

Java Virtual Machine


Every Java interpreter, whether it's a Java development tool or a Web browser that can run Java applets, is an implementation of the Java VM. The Java VM can also be implemented in hardware. Java byte codes help make "write once, run anywhere" possible. We can compile your Java program into byte codes on any platform that has a Java compiler. The byte codes can then be run on any implementation of the Java VM.

Why Use Java?


Write less code: Comparisons of program metrics (class counts, method counts, and so on) suggest that a program written in Java can be four times smaller than the same program in C++.

Write better code: The Java language encourages good coding practices, and its garbage collection helps you avoid memory leaks.
Develop programs faster: Your development time may be as much as twice as fast versus writing the same program in C++. Avoid platform dependencies with 100% Pure Java Write once, run anywhere: Because 100% Pure Java programs are compiled into machine-independent byte codes, they run consistently on any Java platform. Distribute software more easily: You can upgrade applets easily from a central server. Applets take advantage of the Java feature of allowing new classes to be loaded "on the fly," without recompiling the entire program.

Java Programming Environment


Specification : Java compiler: Transform Java programs into Java byte code Java byte code: Intermediate representation for Java programs Java interpreter: Read programs written in Java byte code and execute them Java virtual machine: Runtime system that provides various services to running programs Java programming environment: Set of libraries that provide services such as GUI, data structures, etc. Java enabled browsers: Browsers that include a JVM + ability to load programs from remote hosts

Object Oriented Languages A Comparison


Feature
Encapsulation Inheritance Multiple Inherit. Polymorphism Binding (Early/Late) Concurrency Garbage Collection

C++ Yes Yes Yes Yes Both Poor No Yes

Ada
Yes No No Yes Early Difficult No Limited

Java
Yes Yes No Yes Late Yes Yes Yes

Class Libraries

C++: OOL ADA: OBL JAVA: Almost Pure OOL

Java better than C++ ?


No No No No No No No No Typedef, #Defines or Preprocessor Global Variables goto statements Pointers and pointer arithmetic Unsafe Structures Multiple Inheritance Operator Overloading Fragile Data Types

Removed From C++


Operator overloading Pointers and array of pointers Multiple-Inheritance typedef, #define Copy constructors, Destructors Templates And other stuff....

Added or Improved over C++


Interfaces: Type Vs. Class

Garbage collection
Exceptions (More powerful than C++) Strings Instance of Packages Multi-threading

Types of Java Applications


Different ways to write/run a Java codes are: Application- A stand-alone program that can be invoked from command line . A program that has a main() method Applet- A program embedded in a web page , to be run when the page is browsed . A program that contains no main method Application -Java interpreter Applets- Java enabled web <APPLET> tag in .html file)

browser

(Linked

to

HTML

via

Java Program Structure


The Java programming language:
A program is made up of one or more classes A class contains one or more methods A method contains program statements

A Java application always contains a method called main()

Java Program Structure


// comments about the class

public class MyProgram {

class header

class body

Comments can be added almost anywhere

Java Program Structure

//

comments about the class

public class MyProgram { // comments about the method

public static void main (String[] args) {

method body
} }

method header

Comments
Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take two forms:

// this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks

*/

/** This kind of comment is a specialjavadoc style comment */

Java Program - Example

Java Program - Example

Java Program - Example

A Simple Java Application


Define a class HelloWorld and store it into a file: HelloWorld.java: public class HelloWorld { public static void main (String[] args) { System.out.println(Hello, World); } }

Compile the program: javac HelloWorld.java Execute the program: java HelloWorld Output: Hello, World

A Simple Java Application


public class HelloWorld { public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.display(); }

public void display() { System.out.println(Hello, World); }


} Compile the program: javac HelloWorld.java Execute the program: java HelloWorld Output: Hello, World

Compiling Programs
Create/Modify Source Code

On command line

javac file.java
Source Code

Compile Source Code i.e. javac Welcome.java


If compilation errors

Bytecode

Run Byteode i.e. java Welcome

Result

If runtime errors or incorrect result

Executing Applications
On command line

java classname
Bytecode

Java Interpreter on Windows

Java Interpreter on Linux

...

Java Interpreter on Sun Solaris

Phase 1

Editor

Disk

Phase 2

Compiler

Disk Primary Memory

Program is created in the editor and stored on disk. Compiler creates byte codes and stores them on disk.

Phase 3

Class Loader Class loader puts bytecodes in memory.

Disk

. . . . . . Primary Memory

Phase 4

Bytecode Verifier

Phase 5

Interpreter

. . . . . . Primary Memory

Bytecode verifier confirms that all bytecodes are valid and do not violate Javas security restrictions. Interpreter reads bytecodes and translates them into a language that the computer can understand, possibly storing data values as the program executes. JIT is used to improve the performance.

Lifecycle of Java Code

. . . . . .

Next
basic programming constructs of Java Classes and Objects in Java new operator

Constructors
Overloading

J2SE Core Java


PG-DAC Aug-2013

J2SE Core Java


Objective: To introduce the student to Core Java Prerequisites: Knowledge of objectoriented programming Evaluation method:
Theory exam (objective/subjective/both) 40% weightage Lab exam 40% weightage Assignments 20% weightage

List of Books / Other training material Courseware:


The Complete Reference Java Eight Edition, Herbert Schidt/ TMH

Reference:
Beginning Java 2 by Ivor Horton; Wrox Publication

También podría gustarte