Download Basic Java Seminar 2/2

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Seminars on 4th SPARCS Exhibition - The second one
Basic Java Seminar 2/2
Novemberr 5, 1997
SPARCS
Nam Sedong
[email protected]
What is Java?
• A way to make Web pages sexy?
• A language like C++, Smalltalk?
• Computing Platform
– Java Programming Language
– Java (Virtual) Machine (JVM)
– Java API (Application Programmers’ Interface)
• MFC
Java Language
• C++ -- +++
– Object Oriented
– no multiple inheritance
– no operator
overloading
– No pointer
• Secure
• Thread-safe
• Exception Handling
• Architecture Neutral
• Internationalization
– Unicode, 2 byte char
• Garbage Collection
• Standardization
– sizeof( int )
• Packages
• Network Oriented
– Class loading
Java Virtual Machine
• The engine that actually executes a Java
program
• key to many of Java’s features
– portability (Write once, run anywhere)
– efficiency
– security (Sandbox)
• virtual processor, virtual memory…
• Used in JDK, JRE, browsers
Java API
•
•
•
•
•
•
•
•
•
•
•
•
package java.awt
package java.awt.datatransfer
package java.awt.event
package java.awt.image
package java.beans
package java.io
package java.lang
package java.lang.reflect
package java.math
package java.net
package java.rmi
package java.rmi.dgc
•
•
•
•
•
•
•
•
•
package java.rmi.registry
package java.rmi.server
package java.security
package java.security.acl
package java.security.interfaces
package java.sql
package java.text
package java.util
package java.util.zip
Application & Applets
• Application
– standalone programs
– HotJava browser
• Applet
– a Java program to be included in HTML pages
and executed in a Java-compatible browser.
How Applet Run?
Compile
Java Source Code
Java Class File
Network
Web Browser
Before You go on
• JDK(Java Development Kit)
• Java-compatible browser
• References
– Java Tutorial HTML format
– Java API Documentation
• You can find many resources about Java at
http://www.javasoft.com
Hello World Application
/**
* The HelloWorldApp class implements an application that
* simply displays "Hello World!" to the standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); //Display the string.
}
}
Compiling
• JDK:
– UNIX:
• javac HelloWorld.java
– DOS shell (Windows 95/NT):
• javac HelloWorld.java
• Other Visual Development Tools:
– Maybe, you can find “Compile” button, or etc.
• Output: HelloWorld.class
Running
• Run the program using the Java interpreter.
• JDK
– java HelloWorldApp
• Other Visual Development Tools:
– Maybe, you can find “Compile” button, or etc.
Variables and Data Types
• Primitive Type
– char
• 2byte, UNICODE
– boolean
• true or false
• Reference Type
• C data types Not supported
– pointer, struct, union
Class java.lang.String
String a = new String( “abc” );
String b;
b = new String( “def” );
String c = “abc”;
System.out.println( a.length( ) );
System.out.println( a.substring( 5, 10 ) );
System.out.println( a + b );
System.out.println( a == “abc” );
System.out.println( a.equals( “abc” ) );
System.out.println( c == “abc” );
String d = “This is”.concat( “String” );
Class java.lang.Math
int a;
a = Math.max( -5, -10 );
a = Math.abs( a );
int pi = Math.PI;
a = ( int )Math.max( -9.0, -10.2 );
java.util.Vector
Vector()
Vector(int)
Vector(int, int)
addElement(Object)
capacity()
contains(Object)
copyInto(Object[])
elementAt(int)
elements()
firstElement()
indexOf(Object) ...
Class java.util.Stack
public class Stack extends Vector {
public Object push(Object item);
public Object pop() throws EmptyStackException;
public Object peek() throws EmptyStackException;
public boolean empty();
public int search(Object o);
}
Abstract Window Toolkit(AWT)
• GUI, Look and Feel
• Components
– Button, Frame, Panel, TextField, ...
• Event Handling
– MouseListener, ComponentListener, ...
• Layout
– GridLayout, FlowLayout, …
AWT Component Hierarchy
HelloSPARCS Application
• A button turn in to red when pressed
– Add components to a container
– Add a listener to a component
– Multiple class in a file
HelloSPARCS.java 1/3
import java.awt.*;
import java.awt.event.*;
public class HelloSPARCS {
public static void main( String[] args ) {
Frame f = new Frame( "HelloSPARCS" );
f.add( new SPARCSButton( ) );
f.pack();
f.show();
}
}
Hello SPARCS.java 2/3
class SPARCSButton extends Button {
public SPARCSButton( ) {
this.setLabel( "SPARCS" );
this.addMouseListener( new SPARCSButtonAdapter( ) );
}
public SPARCSButton( Color argC ) {
this.setBackground( argC );
this.addMouseListener( new SPARCSButtonAdapter( ) );
}
}
Hello SPARCS.java 3/3
class SPARCSButtonAdapter extends MouseAdapter {
public void mouseClicked( MouseEvent argME ) {
if ( argME.getSource( ) instanceof SPARCSButton ) {
( ( SPARCSButton )argME.getSource( ) ).setBackground( Color.
red );
}
else {
System.err.println( "ERROR: ... " );
}
}
}
Hello World Applet
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
Class java.awt.Graphics
• Various devices, as well as onto off-screen
images.
• Component.getGraphics( )
–
–
–
–
–
origin
clip
color
font.
XOR
repaint( )
• Another Thread for AWT
• repaint( )
– may not be called
– final state
• update( Graphics g)
• paint( Graphics g)
HW.html(Calling an Applet)
<HTML>
<HEAD>
<TITLE>
A Simple Program
</TITLE>
</HEAD>
<BODY>
<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
The Life Cycle of an Applet
public class Simple extends Applet {
...
public void init() { . . . }
public void start() { . . . }
public void stop() { . . . }
public void destroy() { . . . }
...
}
한글? Unicode?
• char
– 2byte
• String
• Unicode 2.0
– 2 byte
• 한글로 된 변수이름을 쓸 수 있다.
Java의 미래?
• Today, 5 times slower than C++
– Just In-Time(JIT) compiler
– JAR
• Write Once, Run Anywhere?
– Java Foundation Class
• Assembly  C
• JavaBeans - Software Component
• JavaStation - (Not Virtual)Machine
References
• http://www.javasoft.com/docs/books/tutorial/index.html
• http://www.javasoft.com/products/jdk/1.1/docs/api/packages.htm
l
• http://www.javasoft.com/docs/books/jls/html/index.html
• 전문가로 큰다, 김도형(dynaxis), 마이크로소프트웨어
• Java Unleashed, Sams net
• Java Virtual Machine, Jon Meyer&Troy Downing, O’Reilly
Contact Information
• [email protected][email protected][email protected][email protected][email protected]
• Office: 869-5400
• Off-line: SPARCS room