© 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application –...

18
© 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application Stand-alone program (run without a web browser) Relaxed security since user runs program locally Applet Small app embedded in a webpage - requires a Java enabled web browser to run app Enhanced security since user goes to a web page & applet runs itself

description

© 2012 Pearson Education, Inc. All rights reserved. 1-3 Normal Compiler 1.Programmer writes program – in high-level progr. lang. (C, C#, COBOL,…) – using text editor or IDE (Integrated Development Environment)  source code file = set of programming language statements 2.Compiler translates source code into machine language (=executable code: SomeProgram.exe )

Transcript of © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application –...

Page 1: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

2 types of Java programs

• Application– Stand-alone program (run without a web browser)– Relaxed security since user runs program locally

• Applet– Small app embedded in a webpage - requires a

Java enabled web browser to run app– Enhanced security since user goes to a web page &

applet runs itself

Page 2: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Why Java (vs. other languages)?

• Java is “cross platform”

• Portability– Program written for 1 type of device/HW/OS

runs on ANY OTHER device/HW/OSwithout rewriting/recompiling program

Page 3: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Normal Compiler1. Programmer writes program

– in high-level progr. lang. (C, C#, COBOL,…)– using text editor

or IDE (Integrated Development Environment)

source code file = set of programming language statements

2. Compiler translates source code intomachine language(=executable code: SomeProgram.exe)

Page 4: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Compiler = a program

• IPO (Input / Processing / Output)

• processes:– Input data: source code file– Output data: machine language file

• finds syntax errors– ~ spelling, grammar, structure errors– that violate rules of that programming language

.

Page 5: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

A typical Compiler vs. the Java compiler (& the JVM)

• Most compilers translate source code file into executable file (= machine code for a specific CPU / OS)

• Java compiler translates Java source file intoa file containing byte code instructions

• Byte code instructions arethe “machine language” of the Java Virtual Machine (JVM) & can NOT be executed directly by a CPU

Page 6: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Java Virtual Machine (JVM)

• JVM = a program that emulates a CPU

• JVM executes each byte code instructionas it’s read (unlike a compiler)

– So it’s called an interpreter (vs. a compiler)

• Java = an interpreted language

Page 7: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Program Development Process

Text editor(or IDE)

Source code(.java)

Saves Java statements

Java compiler(javac)

Is read by

Byte code(.class)

Produces

JavaVirtual Machine

(java)

Is interpreted by

ProgramExecution

Results in

Page 8: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Portability

• Portable = program written for 1 type of computerruns 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 programs highly portable• Specific JVMs exist for many platforms:•Unix

•BSD•etc.

•Windows•Mac•Linux

Page 9: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Portability

• most programming languages’ programs:portability by re-compiling program for each different platform/CPU– so, many different .exe files– (what about applets for web?)

• Java provides a JVM for each platform– so only 1 .class (byte code) file for everywhrte– Byte code program runs on ANY JVM

Page 10: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Portability

Java VirtualMachine for Windows

Byte code(.class)

Java VirtualMachine for Linux

Java VirtualMachine for Mac

Java VirtualMachine for Unix

Page 11: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Java Versions

• 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.

• Available for free download

Page 12: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

2 ways to compile Java program

1. In command-prompt (B&W) window– javac is Java compiler (for specific JVM)– to compile: javac SomeProgram.java– must use .java file extension

2. IDE autBomates (& hides) this– called Build (instead of compile)– Automatic build when program is Run

Page 13: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Programming LanguagesCommon Language Elements (all languages)

• Some common concepts– Key words– Operators– Punctuation– Programmer-defined identifiers– Strict syntactic (grammar) rules

Page 14: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Sample Java Program – key words

public class HelloWorld{ public static void main(String[] args) { String message = "Hello World"; System.out.println(message); }}

• Key words: public, class, static, void

– lower case (Java is a case-sensitive)– can’t use these as programmer-defined identifiers

Page 15: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Java – lines vs. statements

• A statement = a complete instruction that causes computer to perform an action.

• Semi-colon at end of every statement– not at end of every line

System.out.println( message);

• This is 1 statement written on 2 lines

Page 16: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Variables

• store data in a program (in memory)• variable name represents a location in memory• also called fields• variables created by programmer – specify: 1)

name2) data TYPE3) starting value (optional)

int age = 18;String name;

Page 17: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Variables

Variable = a name given to a location in memory– 8 locations below

0x0000x0010x0020x0030x0040x0050x0060x007

Page 18: © 2012 Pearson Education, Inc. All rights reserved. 1-1 2 types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.

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

Variables

0x0000x0010x0020x0030x0040x0050x0060x007

JVM(not programmer)decides wherein memory thedeclared variableis stored

72

int length = 72;

Variable called lengthis a symbolic name for memory location 0x003.

Programmer does NOTknow or careit’s in 0x003.