ABOUT THE JAVA TECHNOLOGY Java technology is both a programming language and a... ABOUT THE JAVA TECHNOLOGY In the Java programming language, all source code is first written in plain
Trang 1ADVANCED PROGRAMMING (LTNC)
Trang 2Course Topics – Java Basics
Declarations and Access Control
Trang 3Course Topics – Java Basics
Constructor and instantiation
Class and Object varialables
Trang 4Course Topics – GUI (SWING)
Performing custom painting
More Swing Features and Concepts
How to use the Swing components
Trang 5 Attendance: 15%
Midterm exam: 35% (Lab)
Final exam: 50% (Lab)
Trang 6Bibliography
1 Core JAVA, Volume I , Cay S Horstmann, Gary
Cornell, Sun MicroSystem Press, USA 2008.
2 Core JAVA, Volume II , Cay S Horstmann, Gary
Cornell, Sun MicroSystem Press, USA 2008
Trang 7Computer Program
Set of instructions written in
a programming language that tells the computer what to do
Trang 8The Programming Process
Defining the problem
Planning the solution
Coding the program
Testing the program
Documenting the program
Trang 9The Programming Process:
Defining the Problem
What is the input
What output do you expect
How do you get from the input to the output
Coding the Program
Translate algorithm into a formal programming
Trang 10Computer Programming Languages
1 Machine code or machine languages
A sequence of 0’s and 1’s giving machine specific
instructions Example: 00011001
Displayed as hexadecimal
Only language the computer understands
All other programming languages are translated to machine language
Computer dependent
Trang 11Computer Programming Languages
Assembler: Assembly code machine code
Disassembler: machine code assembly code
Trang 12Computer Programming Languages
3 High-level languages
Similar to everyday English and use mathematical
notations (processed by compilers or interpreters)
Example of a C statement: a = a + 8;
High-level Languages and Their Inventors
FORTRAN John W Backus, 1954
BASIC George Kemeny and Tom Kurtz, 1964
Pascal Nicolas Wirth
C Dennis M Ritchie
C++ Bjarne Stroustrup
Java James Gosling
C# Anders Hejlsberg
Trang 13Programming Language Popularity
Trang 14Language Level
Trang 15ABOUT THE JAVA TECHNOLOGY
Java technology is both a programming language and a
Trang 16ABOUT THE JAVA TECHNOLOGY
In the Java programming language, all source code is first written in plain text files ending with the .java extension Those source files are then compiled into .class files by the Java compiler (javac) A .class file does not contain code that is native to your processor; it instead contains
bytecodes the machine language of the Java Virtual
Machine The Java launcher tool (java) then runs your
application with an instance of the Java Virtual Machine
Trang 17ABOUT THE JAVA TECHNOLOGY
Trang 18JVM – Java Virtual Machines
Virtual machines depend on specific platforms
(hardware, OS)
Provide Java programs with (platform independent) run-time environments
Ensure system security
Normally provided as software
JRE - Java Runtime Environment
Java platform: JVM + APIs
Trang 19The Java Runtime Environment
The Java application environment performs as
follows:
Trang 20JVM™ Tasks
– Loads code – Performed by the class loader
• Loads all classes necessary for the execution of a program.
• Avoids execution of the program whose bytecode has been changed illegally.
– Verifies code – Performed by the bytecode verifier
• The code adheres to the JVM specification.
• The code does not violate system integrity.
• The code causes no operand stack overflows or underflows.
• The parameter types for all operational code are correct.
• No illegal data conversions have occurred.
– Executes code – Performed by the runtime interpreter
Trang 21Java Technology Runtime Environment
Trang 22JDK – Java Development Kit
Sun Microsystems (http://java.sun.com)
– javac compiler, converts source code into Java bytecode
– java interpreter and application loader
– appletviewer interpreter, run and debug Java applets
without a web browser
– javadoc documentation generator, automatically
generates documentation from source code comments
– jdb debugger
– javap class file disassembler
Trang 23Types of Java applications
Desktop application – Java SE (Java Standard Edition)
– Java Application: normal Java application running on
desktops; console or GUI
– Java Applet: embedded application running within Web browsers
Server application – Java EE (Java Enterprise Edition)
– JSP và Servlet, JSF, EJB
Mobile (embedded) application – Java ME (Java Micro Edition)
Java Card
Trang 24Java Desktop Applications
Complete application programs
Console or GUI
Launched by java command
Trang 25A Sample Java program
// This is a simple program
public class TestGreeting {
public static void main(String[] args) {
System.out.println("Hello, world" );
}
}
TestGreeting.java
commented line
Declare new class
main method from where the program begins its execution
displays the string Hello, world on
same name with class
Trang 26Compiling & executing the program
To actually run the program, a java interpreter called
java is required to execute the code
The java compiler creates a file called
TestGreeting.class that contains the byte codes
Trang 27Passing Command Line Arguments
class CommLineArg {
public static void main (String[] pargs) {
System.out.println( "These are the arguments
passed to the main method." );
Trang 28Small improvement
Two classes in separated files
public class TestGreeting {
public static void main(String[] args) {
Greeting gr = new Greeting();
gr.greet();
} }
TestGreeting.java
public class Greeting {
public void greet() {
System.out.print( "Hello, world" );
} }
Greeting.java
Trang 29Compile and run
Trang 30Java Applets
Embedded into webpages, i.e run in web browsers.
Or in appletviewer
Limited graphical interface
No access to client’s resources
can do no evil
Trang 31A simple applet
// Java packages
import java.awt.Graphics;
import java.applet.Applet;
public class Welcome extends Applet {
public void paint(Graphics g) {
// call superclass version of method paint
Trang 32Embedded into a Webpage
Trang 33• in web browser
• In applet viewer: appletviewer Welcome.html
Trang 34Integrated Development Environment - IDE
Provides comprehensive facilities to computer
programmers for software development
Normally consists of a source code editor, build
automation tools and a debugger.
IDEs can contain compiler, interpreter.
Some Java IDEs:
Eclipse
Netbean
Trang 35MORE ABOUT THE JAVA TECHNOLOGY
The Java Platform
A platform is the hardware or software environment
in which a program runs We've already mentioned
some of the most popular platforms like Microsoft
Windows, Linux, Solaris OS, and MacOS Most
platforms can be described as a combination of the
operating system and underlying hardware The Java platform differs from most other platforms in that it's a software-only platform that runs on top of other
hardware-based platforms The Java platform has two components:
The Java Virtual Machine
The Java Application Programming Interface (API)
Trang 36MORE ABOUT THE JAVA TECHNOLOGY
Simple
programmed easily without a lot of esoteric training and which leveraged today's standard practice.Java omits
many rarely used, poorly understood, confusing
features of C++ that, in our experience, bring more
grief than benefit.
The syntax for Java is, indeed, a cleaned-up version of the syntax for C++ There is no need for header files, pointer arithmetic (or even a pointer syntax),
structures, unions, operator overloading, virtual base
classes, and so on.
Trang 37MORE ABOUT THE JAVA TECHNOLOGY
The object-oriented features of Java are
comparable to C++ The major difference between Java and C++ lies in multiple inheritance, for which Java has found a better solution, and in the Java
metaclass model
Trang 38MORE ABOUT THE JAVA TECHNOLOGY
Distributed
Java has an extensive library of routines for coping with TCP/IP protocols like HTTP and FTP Java applications can open and access objects across the Net via URLs with the same ease as when accessing a local file
system.
The networking capabilities of Java to be both strong and easy to use Anyone who has tried to do Internet programming using another language will revel in how simple Java makes onerous tasks like opening a socket connection
Trang 39MORE ABOUT THE JAVA TECHNOLOGY
Robust
Java is intended for writing programs that must be reliable in a variety of ways Java puts a lot of emphasis on early checking for possible problems, later dynamic (run-time) checking, and eliminating situations that are error-prone The single
biggest difference between Java and C/C++ is that Java has a pointer model that eliminates the possibility of overwriting
memory and corrupting data
This feature is also very useful The Java compiler detects
many problems that, in other languages, would show up only
at run time As for the second point, anyone who has spent
hours chasing memory corruption caused by a pointer bug will
be very happy with this feature of Java
Trang 40MORE ABOUT THE JAVA TECHNOLOGY
instructions which have nothing to do with a particular
computer architecture Rather, they are designed to be both easy to interpret on any machine and easily translated into native machine code on the fly
Portable
Unlike C and C++, there are no
"implementation-dependent" aspects of the specification The sizes of the
primitive data types are specified, as is the behavior of
arithmetic on them
Trang 41MORE ABOUT THE JAVA TECHNOLOGY
Interpreted
directly on any machine to which the interpreter has
been ported Since linking is a more incremental and
lightweight process, the development process can be
much more rapid and exploratory.
High Performance
While the performance of interpreted bytecodes is
usually more than adequate, there are situations where higher performance is required The bytecodes can be translated on the fly (at run time) into machine code for the particular CPU the application is running on.
Trang 42MORE ABOUT THE JAVA TECHNOLOGY
Secure
Java is intended to be used in networked/distributed environments Toward that end, a lot of emphasis has been placed on security Java enables the construction
of virus-free, tamper-free systems.
Trang 43A SHORT HISTORY OF JAVA
Java goes back to 1991, when a group of Sun engineers, led
by Patrick Naughton, Sun Fellow and James Gosling, wanted to
design a small computer language that could be used for consumer devices like cable TV switchboxes Since
these devices do not have a lot of power or memory, the
language had to be small and generate very tight code Also, because different manufacturers may choose different central processing units (CPUs), it was important not to be tied down
to any single architecture The project got the code name
Trang 44A SHORT HISTORY OF JAVA
The Green project (with a new name of "First Person, Inc.") spent all of 1993 and half of 1994 looking for people to buy its technology—no one was found
The World Wide Web part of the Internet was growing bigger and bigger The key to the Web is the browser that translates the hypertext page to the screen In 1994, most people were using Mosaic, a noncommercial Web browser that came out of the supercomputing center at the University of Illinois in 1993
In the SunWorld interview, Gosling says that in mid-1994, the language developers realized that "We could build a real cool browser It was one of the few things in the client/server
mainstream that needed some of the weird things we'd done: architecture neutral, real-time, reliable, secure—issues that
weren't terribly important in the workstation world So we built
a browser."
Trang 45A SHORT HISTORY OF JAVA
Sun released the first version of Java in early 1996
People quickly realized that Java 1.0 was not going to cut it for serious application development
The big news of the 1998 JavaOne conference was the
upcoming release of Java 1.2, which replaced the early toy-like GUI and graphics toolkits with sophisticated and scalable
versions that come a lot closer to the promise of "Write Once,
Run Anywhere"™ than their predecessors Three days after
(!) its release in December 1998, Sun's marketing department changed the name to the catchy term Java 2 Standard Edition Software Development Kit Version 1.2
Besides the "Standard Edition," two other editions were
introduced: the "Micro Edition" for embedded devices such as
Trang 46HOW WIL JAVA TECHNOLOGY CHANGE MY LIFE
Get started quickly: Although the Java programming
language is a powerful object-oriented language, it's easy to
learn, especially for programmers already familiar with C or C++
Write less code: Comparisons of program metrics (class
counts, method counts, and so on) suggest that a program
written in the Java programming language can be four times smaller than the same program in C++
Write better code: The Java programming language
encourages good coding practices, and its garbage collection helps you avoid memory leaks Its object orientation, its
JavaBeans component architecture, and its wide-ranging, easily extendible API let you reuse other people's tested code and
introduce fewer bugs
Trang 47HOW WIL JAVA TECHNOLOGY CHANGE MY LIFE
Develop programs more quickly: Your development time
may be as much as twice as fast versus writing the same
program in C++ Why? You write fewer lines of code and it is a simpler programming language than C++
Avoid platform dependencies: You can keep your program
portable by avoiding the use of libraries written in other
languages
Write once, run anywhere: Because Java applications are
compiled into machine-independent bytecodes, they run
consistently on any Java platform
Distribute software more easily: With Java Web Start
technology, users will be able to launch your applications with
a single click of the mouse
Trang 48CREATING YOUR FIRST APPLICATION
Create a source file A source file contains text,
written in the Java programming language, that you and other programmers can understand You can use any text editor to create and edit source files
Compile the source file into a class file The Java
compiler , javac, takes your source file and translates its text into instructions that the Java Virtual Machine
can understand The instructions contained within this file are known as bytecodes
Run the program The Java launcher (java) uses the
Java Virtual Machine to run your application