2
8/28/2016 1 © 2012 Pearson Education, Inc. All rights reserved. Java some slides from Gaddis’ book (our textbook) supplemented/modified by Kaminski © 2012 Pearson Education, Inc. All rights reserved. 1-2 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-3 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-4 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 [simplistically] © 2012 Pearson Education, Inc. All rights reserved. 1-5 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-6 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

Java - homepages.wmich.edukaminski/1110/Java.pdf · • Java compiler translates Java source file into ... (unlike a compiler) – an interpreter (vs. a true compiler) ... Java.ppt

Embed Size (px)

Citation preview

Page 1: Java - homepages.wmich.edukaminski/1110/Java.pdf · • Java compiler translates Java source file into ... (unlike a compiler) – an interpreter (vs. a true compiler) ... Java.ppt

8/28/2016

1

© 2012 Pearson Education, Inc. All rights reserved.

Java

• some slides from Gaddis’ book (our textbook)

• supplemented/modified by Kaminski

© 2012 Pearson Education, Inc. All rights reserved. 1-2

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 sinceuser goes to a web page & applet runs itself

© 2012 Pearson Education, Inc. All rights reserved. 1-3

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-4

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 [simplistically]

© 2012 Pearson Education, Inc. All rights reserved. 1-5

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-6

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

Page 2: Java - homepages.wmich.edukaminski/1110/Java.pdf · • Java compiler translates Java source file into ... (unlike a compiler) – an interpreter (vs. a true compiler) ... Java.ppt

8/28/2016

2

© 2012 Pearson Education, Inc. All rights reserved. 1-7

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-8

Program Development Process

Text editor

(or IDE)Source code

(.java)

Saves Java statements

Java compiler

(javac)

Byte code

(.class)

Produces

Java

Virtual Machine

(java)

Program

Execution

Results in

© 2012 Pearson Education, Inc. All rights reserved. 1-9

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:

•Unix

•etc.

•Windows

•Mac

•Linux© 2012 Pearson Education, Inc. All rights reserved. 1-10

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-11

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-12

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