The single biggest difference between Javaand C/C++ is thatJava has “a inner safepointer-model”, therefore it eliminates thepossibility of overwriting memory and corruptingdata, so programmers feel very safe in coding.

Java’s Features(continue)

•GUI [Java-Swing]

For some reason, Sun believe their java-swingis very important, so they always put it in theircertificate-tests.

•Multi-threaded

•Secure [ Exception handling ]

•Dynamic [ for Server-side coding]

Java’s cross-platform

•Interpreted Execute: cross-platform

why: Forcross-platform

purpose.Once coding, run anywhere.

TheJava interpreter ( java.exe and its javaVirtualMachine)

canexecute compiled Java-byte-codes(Xxx.class) directlyon any machine

towhich the interpreter has been ported.

How:

( eg. Dos command line style)

-

Edit

source code“demo.java”

, by notepad/or other IDE tools

-

Compile ( javac.exe )

“demo.java”

javac

Demo.java



Java byte

codes, namely,Demo.class

-

Execute

(Interpreted Execute)

java

Demo

•Speed issue AND new solutions: java is slower than c++ in running.

however, by now, there are some new technology of Java compiler, suchas “Just-in-time”, and “HotSpot adaptive Compiler”. They make javavery faster than before.

Ps: Compiler and Interpreters: Run inPhysical CPU

1.Compilers

use the traditional compile/link/run strategy.Examples:

C, C++, ML.

source [complie] native-files [link] nativeprogram [run]

demo.c---

obj-

demo.exe--Intel cpu

Demoh.h

2.Interpreters

execute the source code directly.Examples:

BASIC, Perl, TCL/Tk, ML.

source [load] [interpret run]

demo.perl-

source-program

-

Intel cpu

data

Java: Run inVirtual Cpu

:cross-platfrom

Demo.java†䍯浰楬攠䑥浯⹣污獳†汩湫†硸砮捬慳x

Source-code “javac” byte-code files bytecode program

interpretedly run on VM |--

䥮I敬 †䍐

(virtual CPU: JSDK ) |--

… CPU

|--†䅰灬A⁃偕

Part-2 2 samples

•How many kinds of java programs ?

•Demo-1: Stand-lone sample

•Demo-2: an Applet sample

How many kinds of Java Programs?

•Un-network app.: (1)Standalone Java program (today)

•Network app:

non-standalone Java program

Internet:(2)Applet ,

(today)

(3)servlet

(4)JavaBean classes

Intranet: (5)EJB( EnterpriseJavaBean ),

(6)RMI, etc

Standalone Java Program

•The main() method

public

static

void

main(String args[]){

...

}

public---

the interpreter can call it

static----It is a static method belonging to the class

void-----It does not return a value

String----It always has an array of String objects as its formal parameter.the array contains any arguments passed to the program on thecommand line

the source file’s name must match the class name which main method is in

Java program

Program Output

1

// Fig. 2.1: Welcome1.java

2

// A first program in Java

3

4

public class Welcome1 {

5

public static void main( String args[] )

6

{

7

System.out.println( "Welcome to Java Programming!" );

8

}

Welcome to Java Programming!

9}

Java program

Program Output

1

// Fig. 2.1: Welcome1.java

2

// A first program in Java

3

4

public class Welcome1 {

5

public static void main( String args[] )

6

{

7

System.out.println( "Welcome to Java Programming!" );

8

}

9}

A Simple GUI Program: Printing aLine of Text

•Display

–Most Java applications use windows or a dialog box

•We have used command window

–ClassJOptionPane

allows us to use dialog boxes

•Packages

–Set of predefined classes for us to use

–Groups of related classes calledpackages

•Group of all packages known as Java class library or Javaapplications programming interface (Java API)

–JOptionPane

is in thejavax.swing

package

•Package has classes for using Graphical User Interfaces (GUIs)

1

// Fig. 2.6: Welcome4.java

2

// Printing multiple lines in a dialog box

3

import javax.swing.JOptionPane; // import class JOptionPane

4

5

public class Welcome4 {

6

public static void main( String args[] )

7

{

8

JOptionPane.showMessageDialog(

9

null, "Welcome\nto\nJava\nProgramming!" );

10

11

System.exit( 0 ); // terminate the program

12

}

13

}

Packages

•Like “namespace” in C++

•How to use:

–C++: using namespace xxx

–Java: import xxx, or

import xxx.xx

A Simple Java Applet: Drawing aString

–appletviewer

only understands<applet>

tags

•Ignores everything else

•Minimal browser

–Executing the applet

•appletviewer WelcomeApplet.html

•Perform in directory containing.class

file

1

<html>

2

<applet code="WelcomeApplet.class" width=300 height=30>

3

</applet>

4

</html>

1

// Fig. 3.6: WelcomeApplet.java

2

// A first applet in Java

3

import javax.swing.JApplet; // import class JApplet

4

import java.awt.Graphics; // import class Graphics

5

6

public class WelcomeApplet extends JApplet {

7

public void paint( Graphics g )

8

{

9

g.drawString( "Welcome to Java Programming!", 25, 25 );

10

}

11

}

1

<html>

2

<applet code="WelcomeApplet.class" width=300 height=30>

3

</applet>

4

</html>

import

allows us to usepredefined classes (allowingus to use applets andgraphics, in this case).

extends

allows us to inherit thecapabilities of classJApplet.

Methodpaint

is guaranteed tobe called in all applets. Its firstline must be defined as above.

1

// Fig. 3.8: WelcomeApplet2.java

2

// Displaying multiple strings

3

import javax.swing.JApplet; // import class JApplet

4

import java.awt.Graphics; // import class Graphics

5

6

public class WelcomeApplet2 extends JApplet {

7

public void paint( Graphics g )

8

{

9

g.drawString( "Welcome to", 25, 25 );

10

g.drawString( "Java Programming!", 25, 40 );

11

}

12

}

1

<html>

2

<applet code="WelcomeApplet2.class" width=300 height=45>

3

</applet>

4

</html>

The twodrawString

statementssimulate a newline. In fact, theconcept of lines of text does notexist when drawing strings.