Presentation is loading. Please wait.

Presentation is loading. Please wait.

POS 406 Java Technology And Beginning Java Code

Similar presentations


Presentation on theme: "POS 406 Java Technology And Beginning Java Code"— Presentation transcript:

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

2 Java Technology Programming Language Platform 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

3 Java both compiles and interprets code

4 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.

5 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

6 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

7 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. } }

8 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…………….

9 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.

10 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. I provide an example of this with CEmployee and CLineItem

11 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.

12 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!");

13 Anatomy of an Applet-Class
import 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.

14 Anatomy of an Applet-Method
import 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.

15 Anatomy of Applet - 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>

16 Problems Compiling or Interpreting?
(another good web site for java tutorials)

17 Time for a Java Break

18 Getting Ready to Write Code
To write your first program, you need: 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. 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. You should download JcreatorPro for use later.

19 Steps you will take 1.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.

20 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

21 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

22 Build Cycle

23 Java Build Cycle MyFile.java MyFile.class (java code) Text Editor
(IDE) Java Compiler (javac) Java Interpreter (JVM) MyFile.java (java code) MyFile.class (byte-code) Run Application

24 Important Links J2SE SDK: Java JDK 6.0 SE (DO NOT USE)
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: Textpad: Magic Draw: Appendix B on Textbook for J2SDK setup information (CLASSPATH)


Download ppt "POS 406 Java Technology And Beginning Java Code"

Similar presentations


Ads by Google