Download power point slides

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
IC211
Object Oriented Programming
Overview of Java
Today’s Topics
(Read Chapter 1: An Introduction to Java)
•
•
•
•
•
•
•
History of Java
Java Platforms
Java Development Environment
Types of Java Programs
Common Misconceptions
Similarities/Differences with C++
Hello World Java Style
The “Green Team”
• 1991 - Chartered by Sun to anticipate and plan for the "next
wave" in computing.
– Patrick Naughton, Mike Sheridan, and James Gosling
• Initial conclusion: “Intelligent” consumer devices
Oak (aka Java)
• White paper buzz words
–
–
–
–
–
–
–
–
–
–
Simple
Object oriented
Distributed
Robust
Secure/safe
Portable
Interpreted
High performance
Multi-threaded
Dynamic
Smart appliances did not catch on…
but the Web did!
• World Wide Web popularity in 1993 saved the
project
• Oak – C++ style programming language developed
for the project
• Dynamic content for web pages
• Oak survived and became “Java”
• March 1995 Java formally announced.
Java
•
Simple
– Similar to C++ in syntax so easy to learn
•
Robust
– Interpreter checks for and prevents problems
– Get rid of pointers (sort of)
• Pointer model eliminates possibility of overwriting memory
– Automatic garbage collection (double edge sword)
•
Portable
– Write once, run anywhere
– Portable because of the Java Virtual Machine
– Compile to bytecode, then interpreter executes the bytecode
•
Safe
– Virtual Machine watches the behavior of the Java program.
– Can prevent it from doing certain things:
• Corrupting memory outside its own space
• Read/Write to files without permission
• Over-running the runtime stack (viruses/worms)
•
Object Oriented
– Written from day one as an Object Oriented programming language
– Just because it’s written in Java doesn’t mean it’s object-oriented!
Java Platforms
• Standard Edition (J2SE)
– Basic libraries for applications
– This is what we will be working with
• Enterprise Edition (J2EE)
– Web development, client/server applications
– Requires J2SE
• Micro Edition (J2ME)
– Used for mobile devices
– Smallest footprint
– Subset of functionality
Java Development Environment
• Generally there are five phases
–
–
–
–
–
Editing
Compiling
Loading
Bytecode verification
Execution
Processing a Java Program
Editor
HelloWorld.java
Source Code
Compiler
HelloWorld.class
Byte Code
Java
Virtual
Machine
Class
Loader
Bytecode
Verifier
Interpreter
CPU &
Primary memory
…
Bytecode
• To run a Java program on the Java processor, the source file
(HelloWorld.java) which is a text file, must be translated into bytecodes.
• The resulting file of bytecode (machine language for the Java processor)
is called HelloWorld.class.
• The source file is translated into byte code by the java compiler (javac)
The bytecode file (HelloWorld.class) contains exactly the same bytecodes no
matter what computer system created it. The Java compiler on a Sun machine
will produce the exact same bytecodes as the Java compiler on an Intel based
Windows system.
Java Virtual Machine (JVM)
• The Java Virtual Machine (JVM) is a java interpreter. It is the program
that runs the java program (HelloWorld.class).
• Unlike C++ .exe files which are run on a hardware processor, the JVM is
implemented in software…hence Java Virtual Machine.
• Any computer system can execute Java bytecode programs if it has a
Java interpreter. The Java interpreter has to be specifically written for the
specific processor type of the computer system, but once that is done, the
computer system can become a Java virtual machine. That is, it looks like
a computer with a hardware Java processor chip and can run Java
bytecodes.
• The actual hardware processor is where the program is ultimately run, but
the JVM acts as the intermediary so that no matter what type of machine
produced the bytecode, it will run on the current machine.
When a Java program is translated into bytecode, the bytecode is exactly the
same no matter what computer system is used. This means the bytecodes on a
Sun computer can be sent to an Intel based computer and they will run without a
problem.
Types of Programs
• Four types
– Application
• Standalone program
• Only needs JVM to run
– Applet
•
•
•
•
Runs in a web browser
JVM built into the browser
http://www.snibbe.com/scott/dynamic/gravilux/gravilux.html
http://zebu.uoregon.edu/nsf/cannon.html
– Servlet
• A program that executes on a server machine, usually used to process a request
from a client machine
• Request comes from the client, but is executed on the server
– Bean
• Reusable software programs that you can develop and assemble easily to create
sophisticated applications
• Must adhere to certain specifications
Common Misconceptions
• Same syntax as C++
– On a superficial level…just enough to get you in trouble
• No pointers
– Virtually everything is a pointer…java just uses a different model for pointers
• Portable – write once, run everywhere (JVM)
– Not always…file i/o, user interfaces, etc. Depends on who wrote the JVM and
how well they followed the specification.
• Interpreted…too slow?
– Nothing is free…
– Just-in-time compiler addresses some of it
Similarities to C++
• The language is made up of expressions, that generally look the same as
C++
• Comments are the same.
• Types are the same (or similar).
• Accessing parts of objects is done with the dot '.'
– Remember String data types and .length()?
• while and for loops are the same.
•
•
•
•
Arrays start at 0.
Many of the operators ++, --, etc. are the same.
Assignment is =, equality is ==.
The program starts at main().
Differences from C++
•
•
•
•
•
•
•
•
Multiple files in your project can contain a main() method.
For the most part, one class per file
No header files (.h)
Functions are not called functions, they're called methods.
Parameters are passed by value…but they are pointers!
Two versions of basic data types, i.e., Integer and int
Errors (Exceptions) must be handled (caught) by your program
Automatic garbage collection.
HelloWorld.java
// HelloWorld.java
/*
* An application to print a welcome message.
*/
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Welcome to IC211: Object Oriented Programming");
}
}
Mystery.java
// Mystery.java
public class Mystery
{
public static void main (String[] args)
{
int lo=0;
int hi=1;
System.out.println(lo);
while (hi<56)
{
System.out.println(hi);
hi = lo + hi;
lo = hi - lo;
}
}
}
Mystery.java