Download String args[]

Document related concepts
no text concepts found
Transcript
UTC Flex Program
Java Introduction
January 9, 2002
Jeffrey L. Eppinger
Senior Systems Scientist
School of Computer Science
Outline
Survey
Introductions
Java Overview
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
2
The Survey
• It’s Optional
• It’s Ungraded
• It’s Anonymous
– You can still put your name on the cover sheet
• Answer as much as you can
– It’s intentionally far reaching so if you can’t get
too far…don’t worry (be happy :-)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
3
Bonus Questions
Bonus questions (in my classes):
• will NOT effect your grade,
• but those who answer correctly will receive prized
potentially worth THOUSANDS of dollars!
(If you want to remain anonymous…rip off the
back page of the survey and write your name on it.)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
4
Bonus Question
• So…who is James Gosling?
– Creator of Java
• Where did he get a Ph.D.?
– From CMU (in Computer Science)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
5
Outline
Survey
 Introductions
Java Overview
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
6
Introductions
•
•
•
•
•
Raghu, you know
Me
You
Course Home Page
Goals
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
7
About Me
Jeff Eppinger ([email protected], WeH 4620)
Ph.D. Computer Science (CMU)
Asst Professor of Computer Science (Stanford)
Co-founder of Transarc Corp. (Bought by IBM)
• Transaction Processing Software
• Distributed File Systems Software
IBM Faculty Loan to CMU eCommerce Inst. (99-00)
Now an the faculty of the eCommerce Institute
Most Significant Qualification to Teach Java:
• James Gosling was my officemate in grad school
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
8
You
• 14 Students from United Technologies Corp
• How many know how to program?
– In any language?
– In Java?
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
9
The Course Home Page
Up to date info available on course homepage:
http://euro.ecom.cmu.edu/program/courses/tcr753
– Today:
• Copies of these slides
• Source code for all the examples
– Eventually:
• Up to date information during the Core Java Mini
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
10
My Goals
What should you expect to get from this class?
Intuition about writing software for eCommerce
–
–
–
–
–
What’s possible
What’s hard
How long it takes
Whom to hire
Who is giving you the run around
Ability to write moderately complex Java Programs
– Ability to find what you need in Java
– Ability to do programming in other MSEC classes
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
11
What Are Your Goals?
•
•
•
•
•
Pass the class
Learn Java
Have fun
Learning to swear in Java
Gadgets in Java
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
12
Outline
Survey
Introductions
 Java Overview
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
13
Java
• Java is the coolest technology that you’ll
learn about in the MSEC program.
• Here’s why:
– Runs “everywhere”
– Programming Language with the “Right Stuff”
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
14
Runs Everywhere
• You can run this stuff anywhere
– On Windows, Mac, Linux, Unix, Mainframe, …
– From MS-DOS Prompt
– In Web Browsers
• Using Applets!
– In Web Servers
• Using Servlets and EJBs
– In Smart Cards
– In Integrated Development Environments
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
15
Examples
• First some quick examples from the MS-DOS
Prompt (because these are the smallest programs)
HelloWorld.java
PrintString.java
PrintNumber.java
AddNumbers.java
AddYourNumber.java
IsEven.java
TimesTable.java
• Next we’ll talk about other aspects of Java
• Then I’ll show you examples of Java running
in other environments
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
16
HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
17
How does a program work?
• When you run a program, Java starts a little
man running in the main method. The little
man speaks only Java. He diligently does
whatever the Java program says to do.
main(String args[]) {
System.out.println("Hello…
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
18
PrintString.java
public class PrintString {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
19
PrintNumber.java
public class PrintNumber {
public static void main(String[] args) {
System.out.println(2);
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
20
AddNumbers.java
public class AddNumbers {
public static void main(String[] args) {
System.out.println(2+2);
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
21
AddYourNumber.java
public class AddYourNumber {
public static void main(String[] args) {
int yourNum = Integer.parseInt(args[0]);
System.out.println(2+yourNum);
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
22
IsEven.java
public class IsEven {
public static void main(String[] args) {
int yourNum = Integer.parseInt(args[0]);
int halfYourNum = yourNum / 2;
if (halfYourNum*2 == yourNum)
System.out.println("Yes");
else
System.out.println("No");
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
23
TimesTable.java
public class TimesTable {
public static void main(String[] args) {
int yourNum = Integer.parseInt(args[0]);
int i=1;
while (i <= 10) {
System.out.println(i + " times "
+ yourNum + " is " + i*yourNum);
i=i+1;
}
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
24
Review
We saw:
• a few examples of simple Java programs,
• simple use of Strings
• command line arguments
• conversion of Strings to ints
• if statements
• a loop
I’ll show you more environments toward the end…
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
25
Java
• Java is the coolest technology that you’ll
learn about in the MSEC program.
• Here’s why:
√ Runs “everywhere”
 Programming Language with the “Right Stuff”
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
26
Right Stuff?
•
•
•
•
•
•
Object-oriented
Network-oriented (Security-oriented)
Multithreaded
Large class libraries
High-performance
Easy-to-use
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
27
Object-oriented
• The Data
– Numbers, strings, etc, that represent the object
• The Methods
– Operations you can perform on the data
• Concept
– You don’t need to understand the data or the
implementation of the methods
• In Java these are called classes
• Examples of classes:
– String (or more formally java.lang.String)
– StockQuote
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
28
Example Object: StockQuote
• Constructor:
StockQuote(String ticker)
• Instance Methods:
String
float
String
String
9-Jan-2002
currentQuote()
getPrice()
getName()
getChange()
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
29
Let’s Try It: StockQuoteTest.java
public class StockQuoteTest {
public static void main(String args[]) {
StockQuote sq = new StockQuote(args[0]);
System.out.println("
" +
sq.getName() + "
" +
sq.currentQuote() );
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
30
Network/Security Oriented
• Class Libraries to access the network
• Portable: “write once, run anywhere”
– Interpreted byte codes
• Sandbox
– Allows you to run untrusted code
– Downloaded applets have restricted access
• Libraries to access security features
– ACLs, Certificates, Encryption
• Example
– java.net.URL
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
31
Selected Guts of StockQuote.java
public class StockQuote … {
…
private URL url;
…
private final String urlHeader =
"http://quote.yahoo.com/d/quotes.csv?s=";
…
String urlString = urlHeader+ticker+urlTrailer;
…
url = new java.net.URL(urlString);
…
InputStream is = url.openStream();
9-Jan-2002
UTC Flex Java Intro
32
…
Copyright (C) 2002 J. L. Eppinger
}
Multithreaded
• Lets programs do multiple things at a time
• Your PC is “multithreaded”
– Browser can run multiple windows
• Typical uses:
– Separate threads to perform I/O and computing
• Example:
– StockQuoteMTTest.java
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
33
Threads
• When you run a program the Java loader starts a
thread running in the main method of the class you
load (you know: java HelloWorld)
main(String args[]) {
System.out.println("Hello…
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
34
Making More Threads
• Use new Thread(obj) to create a new thread
– This makes another little man
• Use start() method on the thread to run it
– This starts the man in object’s run() method
main(String args[]) {
StockQuote sq = new StockQuote(IBM);
Thread t = new Thread(sq);
t.start();
…
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
35
Additional threads start in run()
public class StockQuote implements Runnab…
public StockQuote(…ticker) {
…
}
public void run() {
…
}
…
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
36
MT StockQuote Example
Code excerpt from
StockQuoteMTTest.java:
System.out.println("Enter ticker:");
while (true) {
char c = (char) isr.read();
if (c == '\n') {
sq = new StockQuote(b.toString());
backgroundThread = new Thread(sq);
backgroundThread.start();
b = new StringBuffer();
} else if (c != '\r') {
if (sq != null) sq.stopRunning();
b.append(c);
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
37
Large Class Libraries
• Need a class that does something?
– Chances are it’s already been written!
• The Java Foundation Classes
– Packaged with Java
– Provides everything from data structures to GUIs
• Examples
– Vectors (implemented by java.util.Vector)
– Swing GUIs (javax.swing)
– Applets (e.g., java.swing.JApplet)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
38
Example: java.util.Vector
(for those of you with programming background…)
• Implements an array of arbitrary size
• Incredibly handy
Selected Constructor:
Vector()
Selected Instance Methods:
add(obj)
insertElementAt(obj,pos)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
39
Example: ToDoSwingGUI.java
Code excerpt from
ToDoSwingGUI.java:
public class ToDoSwingGUI extends JFrame … {
JButton
topButton, bottomButton;
JTextField
textField;
JTextArea
textArea;
java.util.Vector toDoList;
public ToDoSwingGUI() {
…
}
…
}
public static void main(String[] args) {
new ToDoSwingGUI();
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
40
Example: ToDoApplet.java
Code excerpt from
ToDoApplet.java:
public class ToDoApplet extends JApplet implement… {
JButton
topButton, bottomButton;
JTextField
textField;
JTextArea
textArea;
java.util.Vector toDoList;
public void init() {
…
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
41
Example: ToDo.html
<html>
<head>
<title>HTML File to Launch ToDoApplet</title>
</head>
<body>
<p>
<applet code="ToDoApplet.class" width=500 height=550>
Your browser doesn't support Java!
</applet>
</body>
</html>
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
42
Example: StockQuoteSwingGUI
Code excerpt from
StockQuoteSwingGUI.java:
public class StockQuoteSwingGUI extends JFrame … {
private JButton
button;
private JTextField
textField;
private JTextArea
textArea;
private java.util.Vector quoteHistory;
public StockQuoteSwingGUI() {
…
}
…
}
public static void main(String[] args) {
new StockQuoteSwingGUI();
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
43
Example: TwoSwingGUIs.java
public class TwoSwingGUIs {
public static void main(String[] args) {
new ToDoSwingGUI();
new StockQuoteSwingGUI();
}
}
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
44
High Performance
• High being a relative term:
– Faster than PERL
– (Slower than C or C++)
• Threading can improve throughput
• Many people working on optimization tools
– JIT: Just-in-time compliation
• Your machine is fast, you can afford to run Java
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
45
Easy-to-use
• (Easy being a relative-term)
• Easier than other programming languages
– No pointers
– Automatic Garbage Collection
• Less cryptic than Perl
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
46
Right Stuff?
•
•
•
•
•
•
Object-oriented
Network-oriented (Security-oriented)
Multithreaded
Large class libraries
High-performance
Easy-to-use
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
47
Java Drawbacks
• Version skew
– Newer versions not yet available in Web most web browsers
that are in use
• Internet Explorer 5.0 offers Java 1.1.4
• Netscape 4.7 offers Java 1.1.5
• Netscape 6.1 offers Java 1.3.0_01
– I’m using Java.1.3.1
• You can download an update for your browser, but…
– What’s on your machines?
• Time to download Java code for browser-based apps
• Complexity of programming
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
48
Java Opportunities
• Server-side application programming
– Consistent Environment
– Need for faster development
• Ease of use (relative to older technologies…)
• Availability of programmers
– Typical example: web applications
• Development of appliances
– Smart cards, cellphones, printers, PDAs,
refrigerators, …
– Same reasons as above
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
49
Integrated Development Environment
•
•
•
•
•
•
•
Source-code, syntax-directed editor
Incremental compiler
Repository-based environment for code
Project-based development
Integrated debugging
Support for team development
Tools to facilitate specific programming models
– Applets, Servlets, EJBs, Stored Procedures, XML, ...
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
50
Compile and Run
• Compile (This generates HelloWorld.class)
javac HelloWorld.java
• Run
(This runs HelloWorld.class)
java HelloWorld
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
51
Many IDEs for Java
Being that I’m from IBM, I’ve been using:
• IBM’s WebSphere Visual Age for Java
Also, check out:
• JBuilder (Borland)
• JRun (Allaire)
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
52
Java in a DOS Window
java IsEven
IsEven.class
JVM
IsEven
.class
java.c
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
53
Java in an IDE
IsEven.class
JVM
Respository
VAJ
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
54
Java in an Applet
HTTP Request
HTML
Browser
JVM
HTTP Request
HTTP Server
Data
Files
ToDoApplet.class
Operating System
Operating System
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
55
Java in Servlets
HTTP Request
Browser
HTML
HTTP Server
Servlet
Engine
Operating System
JVM
Operating System
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
.class
files
Data
Files
56
Power of Server Side Java
• No Java version mismatch
– Just generate HTML (HTML mismatch easier problem)
• No long download problems
– Many “real” apps have many big .class files
• Right way to access server data
– Secure: Your program, running on your server
– Fast: Your program, running on your server
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
57
Final Demo
• The IBM Visual Age IDE
– With the WebSphere Unit Test Environment
• I’ll start up the server
• You start up your web browsers
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
58
MSEC Uses of Java
• Networking Programming Examples
• Java-based Web Servers
– Servlets, Java Server Pages, JDBC, etc.
• Other programming assignments
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
59
Summary
• Java is the coolest technology that you’ll
learn about in the MSEC program.
• Here’s why:
√ Runs “everywhere”
√ Programming Language with the “Right Stuff”
9-Jan-2002
UTC Flex Java Intro
Copyright (C) 2002 J. L. Eppinger
60