POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

24
POS 406 Java Technology And Beginning Java Code Source:http://java.sun.com/docs/books/tutorial/ getStarted/index.html (Modified by E. Yanine)

Transcript of POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Page 1: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

POS 406

Java Technology

And Beginning Java CodeSource:http://java.sun.com/docs/books/tutorial/getStarted/index.html

(Modified by E. Yanine)

Page 2: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Java Technology Programming

Language– Cross Platform– Object oriented– Portable– Distributed– High performance– Interpreted– Robust– Dynamic– Secure

Platform– The Java Virtual Machine

(Java VM) • base for the Java

platform and is ported onto various hardware-based platforms.

– The Java Application Programming Interface (Java API)

• The Java API is a large collection of ready-made software components

known as packages

Page 3: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Java both compiles and interprets code

Page 4: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Java Program Types

Applications are standalone programs.

Applets are similar to applications, but they don't run standalone. Applets adhere to a set of conventions that lets them run within a Java-compatible browser.

Page 5: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Java Classes

Class

– the highest level– a set of related methods and fields– declared “public” so you can access it– contains many methods– must be stored in a file name that exactly

matches class name

Page 6: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Anatomy of an Application The skeleton of any Java program is a class

definition. The HelloWorldApp class implements an

application that * simply displays "Hello World!" to the standard output.

Public Class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!");

//Display the string. } }

The name of the class is HelloWorldApp

Page 7: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

The Main Method(Like the Main Function in C) Every Java application must contain a

main method whose signature looks like this:

public static void main(String[] args)

/* The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */

public class HelloWorldApp

{ public static void main(String[] args)

{ System.out.println("Hello World!"); //Display the string. }

}

Page 8: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

The Method Signature

There must be a main method in the controlling class in a Java application.

The method signature provides all the information there is about the interface to the method. In other words, it provides all the information that you need to know to be able to invoke the method. – public static void main(String[] args) OR– public static void main(String args[])– Let’s dissect the method signature…………….

Page 9: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Defining Terms in the Signature public static void main(String[] args) public static void main(String args[])

– Public: The keyword public indicates that the method can be called by any object.

– Static: indicates that the method is a class method, which can be called without the requirement to instantiate an object of the class.

– Void: The keyword void indicates that the method doesn't return any value

– Args: The formal parameter args is an array of type String, which contains arguments entered at the command line. Note that the args parameter must be specified whether or not the user is required to enter a command-line argument and whether or not the code in the program actually makes use of the argument.

Page 10: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Can all Classes have Main Methods? The controlling class of every Java

application must contain a main method. Can other classes in the same application also have a main method? – Yes. It is often desirable to provide a main

method for a class that will not ultimately be the controlling class in an application to allow the class to be tested in a stand-alone mode, independent of any other classes.

Page 11: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

The System Class

The "Hello World" application does use another class--the System class--that is part of the API (application programming interface) provided with the Java environment.

/* The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */

Public class HelloWorldApp { public static void main(String[] args)

{ System.out.println("Hello World!"); //Display the string. }} The construct System.out is the full name of the

out variable in the System class.

Page 12: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

The System Class

When the System class is loaded into the application, it instantiates the PrintStream object from the class System and assigns the new out class variable to the PrintStream object.

Now that you have an instance of a class, you can call one of its object methods (in this case println):

System.out.println("Hello World!");

Page 13: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Anatomy of an Applet-Classimport java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

{ public void paint(Graphics g)

{ g.drawString("Hello world!", 50, 25); }

}

The extends keyword indicates that HelloWorld is a subclass of the class whose name precedes: Applet.

Page 14: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Anatomy of an Applet-Methodimport java.applet.Applet;

import java.awt.Graphics;

public class HelloWorld extends Applet

{ public void paint(Graphics g)

{ g.drawString("Hello world!", 50, 25); } }

Every applet must implement one or more of the init, start, and paint methods

the Graphics object passed into the paint method represents the applet's onscreen drawing context. The first argument to the Graphics drawString method is the string to draw onscreen. The second and third arguments are the (x,y) position

of the lower left corner of the text onscreen.

Page 15: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Anatomy of Applet - HTML<HTML>

<HEAD> <TITLE> A Simple Program </TITLE> </HEAD>

<BODY> Here is the output of my program:

<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>

</APPLET>

</BODY>

</HTML>

Page 16: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Problems Compiling or Interpreting?

http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html

http://www.javaranch.com/cattledrive.jsp(another good web site for java tutorials)

Page 17: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Time for a Java Break

Page 18: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Getting Ready to Write Code

To write your first program, you need:

1. The JavaTM 2 Platform, Standard Edition. You can download the SDK now and consult the installation instructions . (Make sure you download the SDK, not the JRE.) Download Version 1.3.1_08 to ensure it will work with code samples provided.

2. A text editor. In this example, we'll use NotePad, the simple editor included with the Windows platforms. To find NotePad, from the Start menu select Programs > Accessories > NotePad.

3. You should download JcreatorPro for use later.

Page 19: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Steps you will take1.Create a source file. This source file contains text, written in

the Java programming language. You can use any text editor to create and edit source files. Save as filename.java

2.Compile the source file into a bytecode file. The compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler converts these instructions into a bytecode file (filename.class).

3.Run the program contained in the bytecode file. The Java interpreter installed on your computer implements the Java VM. This interpreter takes your bytecode file and carries out the instructions by translating them into instructions that your computer can understand.

Page 20: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Create the Source File Start NotePad. In a new document, type in the

following code: /* The HelloWorldApp class implements

an application that * displays "Hello World!" to the standard output. */

public class HelloWorldApp { public static void main(String[] args) // Display "Hello World!" { System.out.println("Hello World!"); } } Save the file as HelloWorldApp.java

Page 21: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Compile the Source Code Compile the Source File. From the Start menu, select the MS Dos

prompt application (Windows 98) or Command Prompt application (Windows NT, 2000, XP).

Change directory to directory that .java file is stored in

At the prompt, type: javac HelloWorldApp.java  The compiler has generated a bytecode file

named HelloWorldApp.class To run the program type: Java HelloWorldApp

Page 22: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Compile

Run(Test)

Edit

Build Cycle

Page 23: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Text Editor(IDE)

Java Compiler(javac)

MyFile.java

(java code)

MyFile.class

(byte-code)Run Application

Java Interpreter(JVM)

Java Build Cycle

Page 24: POS 406 Java Technology And Beginning Java Code Source: (Modified by E. Yanine)

Important Links

J2SE SDK: Java JDK 5.0 SE and Documentation J2SE SDK: Java JDK 6.0 SE (DO NOT USE)

J2SE Help (oln): Java 6.0 Help (DO NOT USE)

JCreator: http://www.jcreator.com/download.htm Textpad: http://www.textpad.com Magic Draw: http://www.magicdraw.com Appendix B on Textbook for J2SDK setup

information (CLASSPATH)