Chapter2 IntroductionToJava
Chapter2 IntroductionToJava
Introduction to Java
STIA1014 : Introduction To
Programming
Lecture Outlines
• Java Application
• Java Applet
• Java API packages
• Java Compiler and Interpreter
Learning Objectives
–Simple – Architecture
–Object Oriented neutral
–Distributed – High-
–Interpreted performance
–Robust – Multithreaded
– Secure – Dynamic
– Portable
The Basics of Java Program
• Applications
– single plain-looking terminal window
or
– with a graphical user interface (GUI) use
one or more windows filled with user
interface components such as buttons, text
input fields, and menus
The Basics of Java Program
Output:
12 * 6 = 72
12 +6 = 18
How to use Java?
class body
} 27
Java Program Structure
// comments about the class
public class MyProgram
{
// comments about the method
} 28
Compiling and Running
Source code
Byte code
• init method:
– Initializes variables.
• paint method:
– Performs output.
Applets
• The paint method is executed automatically
whenever the applet’s contents are drawn
• The paint method accepts a parameter that is an
object of the Graphics class
• A Graphics object defines a graphics context on
which we can draw shapes and text
• The Graphics class has several methods for
drawing shapes
//********************************************************************
// Einstein.java Author: Lewis/Loftus
//
// Demonstrates a basic applet.
//********************************************************************
import javax.swing.JApplet;
import java.awt.*;
import javax.swing.JApplet;
import java.awt.*;
<html>
<head>
<title>The Einstein Applet</title>
</head>
<body>
<applet code="Einstein.class" width=350 height=175>
</applet>
</body>
</html>
Skeleton of a Java Applet
import java.awt.Graphics;
import javax.swing.JApplet;
}
import java.awt.*;
import javax.swing.JApplet;
public class GrandWelcome extends JApplet{
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.setFont(new Font("Courier",Font.BOLD, 24));
g.drawString("Welcome to Hava
Programming",30,30);
}
}
<HTML>
<HEAD>
<TITLE>WELCOME</TITLE>
</HEAD>
<BODY>
<APPLET code = "GrandWelcome.class" width="440" height="50">
</APPLET>
</BODY>
</HTML>
Java Platform
• Platform is a hardware or software
environment to execute a program.
• It also can be defined as a combination of
operating system and hardware.
• The popular platform are Windows 2000,
Linux, Solaris, and MacOS.
• Java platform is different with other platform
because it is only a software to execute an
application on different hardware platform .
Java Platform
• It has 2 components :
– Java Virtual Machine (JVM)
– Java Application Programming Interface
(Java API)
• (source : Website http://java.sun.com).
Java Virtual Machine (JVM)
• Also called a "Java Interpreter"- Converts bytecode into
OS specific commands. In addition to governing the
execution of an application's bytecodes, JVM handles
related tasks such as managing the system's memory,
providing security against malicious code, and managing
multiple threads of program execution. JVM sits on top of
a native environment (OS) and allows for portability of
applications across various hardware as long as the
hardware has the JVM on it.
• JVM executes instructions that a Java compiler generates.
This run time environment, is embedded in various
products, such as web browsers, servers, and operating
systems.
java.sun.com/developer/onlineTraining/new2java/program
ming/learn/unravelingjava.html
Java Application Programming
Interface (API)
• The Java Application Programming Interface
(API) is prewritten code (predefined classes),
organized into packages of similar topics. For
instance, the Applet and AWT packages include
classes for creating fonts, menus, and buttons.
• There are 3 editions of the Java API:
– Java 2 Standard Edition (J2SE) : to develop client-
side standalone applications or applets.
– Java 2 Enterprise Edition (J2EE) : to develop
server-side applications. E.g. Java servlets and
JSP.
– Java 2 Micro Edition (J2ME) : to develop
applications for mobile devices.
Characters String
• A string literal is represented by putting
double quotes around the text
• Examples:
"This is a string literal."
"123 Main Street"
"X"
object method
name information provided to the method
(parameters)
The print() Method
• The System.out object provides another service
as well
• See Countdown.java
//********************************************************************
// Countdown.java Author: Lewis/Loftus
//
// Demonstrates the difference between print and println.
//********************************************************************
- To format output
- Usage (Syntax):
System.out.printf(formatString , items);
where
-formatString: a string with format specifiers
- items: list of data/variables to be displayed
System.out.println ();
continue
continue
X: 25
Y: 65
Z: 30050
Conclusion
Q & A Session