OOP - Java - Session 1
OOP - Java - Session 1
PG-DESD Aug-2013
Reference:
Beginning Java 2 by Ivor Horton; Wrox Publication
Session 1:
Learning Objectives
List and explain Java features Differentiate between C++ and Java
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.
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.
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
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
Simple Secure Portable Object-oriented Robust Multithreaded Architecture-neutral Interpreted High performance Distributed Dynamic
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.
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
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:
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
.java file
Java Compiler
.class file
Mac
Microsoft
UNIX
21
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.
Ada
Yes No No Yes Early Difficult No Limited
Java
Yes Yes No Yes Late Yes Yes Yes
Class Libraries
Garbage collection
Exceptions (More powerful than C++) Strings Instance of Packages Multi-threading
browser
(Linked
to
HTML
via
class header
class body
//
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
*/
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
Bytecode
Result
Executing Applications
On command line
java classname
Bytecode
...
Phase 1
Editor
Disk
Phase 2
Compiler
Program is created in the editor and stored on disk. Compiler creates byte codes and stores them on disk.
Phase 3
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.
. . . . . .
Next
basic programming constructs of Java Classes and Objects in Java new operator
Constructors
Overloading
Reference:
Beginning Java 2 by Ivor Horton; Wrox Publication