Download Java

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
Java
• some slides from Gaddis’ book (our textbook)
• supplemented/modified by Kaminski
© 2012 Pearson Education, Inc. All rights reserved.
2 types of Java programs
• Application
– Stand-alone program (no web browser)
– Relaxed security since user runs program locally
• Applet
– Small app embedded in a webpage
- needs a Java enabled web browser to run app
– Enhanced security since
user goes to a web page & applet runs itself
© 2012 Pearson Education, Inc. All rights reserved.
1-2
Why Java (vs. other languages)?
Java is “cross platform”
So portable
– Program written for 1 type of device/HW/OS
runs on ANY OTHER device/HW/OS
without rewriting/recompiling program
© 2012 Pearson Education, Inc. All rights reserved.
1-3
Normal Compiler
1. Programmer writes program
– in high-level lang. (C, C#, COBOL,…)
– using text editor or IDE (Integrated Development Environment)
– saves it as a source code file
= set of programming language statements
2. Compiler translates source code into
machine language = executable code
(SomeProgram.exe)
for a specific CPU / OS
© 2012 Pearson Education, Inc. All rights reserved.
[simplistically]
1-4
Compiler = a program
• IPO (Input / Processing / Output)
• translates:
– Input data: source code file
– Output data: machine language file
• also finds syntax errors
spelling, grammar, structure errors
that violate rules of that language
.
© 2012 Pearson Education, Inc. All rights reserved.
1-5
the Java “compiler” (& the JVM)
• Java compiler translates Java source file into
a file containing byte code instructions
• Byte code instructions are
the “machine language”
of the Java Virtual Machine (JVM)
& can NOT be executed directly by a CPU
© 2012 Pearson Education, Inc. All rights reserved.
1-6
Java Virtual Machine (JVM)
• JVM = a program that emulates a HW CPU
• JVM executes each byte code instruction,
as it’s read (unlike a compiler)
– an interpreter (vs. a true compiler)
• Java = an interpreted language
© 2012 Pearson Education, Inc. All rights reserved.
1-7
Program Development Process
Text editor
(or IDE)
Saves Java statements
Source code
(.java)
Java compiler
(javac)
Produces
Byte code
(.class)
Java
Virtual Machine
(java)
Results in
Program
Execution
© 2012 Pearson Education, Inc. All rights reserved.
1-8
So Java programs are Portable
• Portable = program written for 1 type of computer
runs on a wide variety of computers
(with little/no modification) (e.g., applets)
• Java byte code runs on the JVM (on a computer),
NOT on any particular CPU
• So “compiled” Java .class programs highly portable
• Specific JVM’s exist for many platforms:
•Windows
•Mac
•Linux
© 2012 Pearson Education, Inc. All rights reserved.
•Unix
•etc.
1-9
Portability
• Programs in other languages portable by:
re-compiling program
for each different platform / CPU
– so, many different .exe files required
– (what about applets for web?)
• Java provides a JVM for each platform
– so only 1 .class
(byte code) file
works everywhere
– Byte code program runs on ANY JVM
© 2012 Pearson Education, Inc. All rights reserved.
1-10
You need JDK on your laptop
• JDK (Java Development Kit)
– software used to write Java programs
• different editions of JDK:
– Java SE - Standard Edition
– Java EE - Enterprise Edition
– Java ME - Micro Edition
• free download from Oracle
© 2012 Pearson Education, Inc. All rights reserved.
1-11
2 ways to compile Java program
1. command-prompt (B&W) window
– javac is Java compiler (for specific JVM)
– to compile: javac SomeProgram.java
2. IDE automates (& hides) this
– icon to build (instead of compile)
– automatic build when program is run
© 2012 Pearson Education, Inc. All rights reserved.
1-12