Sunteți pe pagina 1din 12

Lecture 1

i) Java History
ii) Introducing Java
iii) Characteristics of Java
iv) Java Environment
v) Java Program Structure
vi) Java Development Kit (JDK)
vii) Compiling and Running Java Programs (Applet and
Application)
viii) Characteristics of OOP (Class, Encapsulation,
Polymorphism, Inheritance etc.)
ix) Java Class Library
JAVA HISTORY
Back in 1990, a gentleman by the name of James Gosling was
given the task of creating programs to control consumer
electronics. Gosling and his team of Sun Microsystems started
designing their software using C++. Gosling quickly found that
C++ was not suitable for the projects he and his team had in
mind. They ran into trouble with complicated aspects of C++
such as multiple inheritance of classes and with program bugs
such as memory leaks .
Although Gosling did not care for the complexity of languages
such as C++, he did like the basic syntax and object-oriented
features of the language. When Gosling completed his language-
design project, he had a new programming language that he
named "Oak". By the time Sun discovered that the name "Oak"
was already claimed and they changed the name to "Java".
INTRODUCING JAVA

Java is an object-oriented programming language developed by


Sun Microsystems, a company best known for its nigh-end Unix
workstations. Modeled after C++, the Java language was
designed to be small, simple, and portable across platforms and
operating systems, both at the source and at the binary level.
Java is often mentioned in the same breath as HotJava, a World
Wide Web browser from Sun like Netscape or Mosaic. Applets
appear in a Web page much in the same way as images do, but
unlike images, applets are dynamic and interactive. Applets can
be used to create animations, figures, or areas that can respond
to input from the reader, games, or other interactive effects on
the same Web pages among the text and graphics.
CHARACTERISTICS OF JAVA
i) Simple
ii) Object-Oriented
iii) Compiled
iv) Platform Independent
v) Multi-trreaded
vi) Garbage collected
vii) Robust
viii) Secured
ix) Well understood
Java Environment

Java Environment
Java Program Structure
Compiling and running Java programs
Using console mode
1. Select the Start/Programs/MS-DOS prompt command
from the Start menu. A DOS window appears.
2. Change to the directory by typing cd\jdkl.3\bin at the
DOS prompt.
3. Type javac filename.java and press Enter to compile,
where filename.java is the name of the
java-source file (Application/Applet).
4. Type java filename and press Enter to run java
application.
But to run java applet create a HTML file
with .html extension as follows:
• <HTML>
• <APPLET CODE="filename.class" WlDTH=100
HEIGHT=200>
• </APPLET>
• </HTML>
• Now type appletviewer filename and press Enter.

Using TextPad
– Write the source code you want to compile.
– Save it with filename.java
– Click Tools from menubar and then click compile java.
– If it is java application click run java application from
Tools menu.
– If it is java applet click run java applet from Tools
menu.
Program 1:

//A program in Console mode


public class Welcome
{
public static void main(String args[]) {
System.out.println("Welcome to Java Programming");
}
}
• You must save the file as Welcome.java that means the class name & the
saved filename should be same and in same case.
• The main() method is defined as being public static with a void return
type.
• Here, public means that the method can be called from anywhere inside
or outside the class.
• static means that the method is the same for all instances of the class.
• The void return type means that main() does not return a value.
• String args[ ] means args is an array of String objects that represents
command-line arguments passed to the class at execution.
Program 2:

// A program in Window mode


import javax.swing. JOptionPane;
public class Welcome1
{
public static void main(String args[]) {
JOptionPane.showMessageDialog(null,"Welcome
to java Programming","My first
program", JOptionPane.INFORMATION_MESSAGE);
}
}
Here JOptionPane is a class file imported from javax.swing package for
showing dialog box.
Program 3:
//A Java Applet Program
import javax. swing.JApplet;
import java.awt.Graphics;
public class WelcomeApplet extends JApplet
{
public void paint(Graphics g)
{
g.drawString("To climb a ladder, start at the
bottom rung",20,120);
}
}

HTML File for WelcomeApplet


<HTML>
<APPLET CODE="WelcomeApplet.class" WIDTH=100 HEIGHT=200>
</APPLET>
</HTML>
Save the above file named WelcomeApplet.html. Now run this file in any
browser or in DOS mode by appletviewer.
Characteristics of Object-Oriented
Programming;
To learn OOP, you need to understand three main concepts that are the backbone of OOP.
They are as follows:

• Encapsulation
• Inheritance
• Polymorphism
Encapsulation: Encapsulation enables you to hide inside the objects both the data fields
and the methods that act on that data. In strict object-oriented design, an object's data is
always private to the object.

Inheritance: Inheritance enables you to create a class that is similar to a previously defined
class, but one that still has some of its own properties. Think of how human children inherit
many of their characteristics from their parents. But the children also have characteristics
that are uniquely their own. In object-oriented programming, you can think of a base class
as a parent and a derived class as a child.

Polymorphism: The last major feature of object-oriented programming is polymorphism. By


using polymorphism you can create new objects that perform the same functions as the
base object but which perform one or more of these functions in a different way.
Popular Packages

Package Name Description


• java.lang Default support basic language
features and handling of arrays and
strings.
• java.io Classes for data input and output.
• javax.swing Easy to use and flexible components
for building GUI interface known
swing components.
• java.awt Provides the original GUI
components as well as some
basic support necessary for swing.
• java.awt.image Support image handling.
• java.util Utility classes of various kind
including classes for managing data
within classes
• java.awt.event Used to handle events

S-ar putea să vă placă și