Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.

Similar presentations


Presentation on theme: "© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various."— Presentation transcript:

1 © 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various devices / OS’s without rewriting/recompiling the program Java is “cross platform”

2 © 2012 Pearson Education, Inc. All rights reserved. 1-2 Java Applications & Applets 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

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

4 © 2012 Pearson Education, Inc. All rights reserved. 1-4 A Compiler is a program 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.

5 © 2012 Pearson Education, Inc. All rights reserved. 1-5 A typical Compiler vs. the Java compiler (& the JVM) Most compilers translate source code into executable file containing machine code for a specific CPU / OS Java compiler translates a 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

6 © 2012 Pearson Education, Inc. All rights reserved. 1-6 Java Virtual Machine JVM = a program that emulates a CPU JVM executes each byte code instruction as it’s read (unlike a compiler) – So it’s called an interpreter Java = an interpreted language

7 © 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 Java Virtual Machine ( java ) Is interpreted by Program Execution Results in

8 © 2012 Pearson Education, Inc. All rights reserved. 1-8 Portability Portable = a program written on one type of computer can run on a wide variety of computers (with little or no modification.) Java byte code runs on the JVM (on a computer), not on any particular CPU So compiled Java programs are highly portable Specific JVMs exist for many platforms : Unix BSD etc. Windows Mac Linux

9 © 2012 Pearson Education, Inc. All rights reserved. 1-9 Portability most programming languages’ programs: portability achieved by compiling program for each different platform/CPU it’ll run on – so many different.exe files Java provides a JVM for each platform so no recompiling for different platforms – so only one.class (byte code) file – Byte code program runs on ANY JVM

10 © 2012 Pearson Education, Inc. All rights reserved. 1-10 Portability Java Virtual Machine for Windows Byte code (.class) Java Virtual Machine for Linux Java Virtual Machine for Mac Java Virtual Machine for Unix

11 © 2012 Pearson Education, Inc. All rights reserved. 1-11 Java Versions JDK (Java Development Kit) – software use to write Java programs different editions of JDK: – Java SE - Standard Edition. – Java EE - Enterprise Edition. – Java ME - Micro Edition. Available for download

12 © 2012 Pearson Education, Inc. All rights reserved. 1-12 Compiling a Java Program javac is the Java compiler Java compiler is a command line utility to compile a program: javac SomeProgram.java must use.java file extension IDE automates (& hides) this – Called Build (instead of compile)

13 © 2012 Pearson Education, Inc. All rights reserved. 1-13 Programming Languages Common Language Elements Some common concepts – Key words – Operators – Punctuation – Programmer-defined identifiers – Strict syntactic rules.

14 © 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

15 © 2012 Pearson Education, Inc. All rights reserved. 1-15 Java – lines vs. statements A statement = a complete instruction that causes the 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

16 © 2012 Pearson Education, Inc. All rights reserved. 1-16 Java Variables Variables store data in a program (in memory) A variable name represents a location in memory Variables also called fields Variables are created by the programmer who specifies 1) name 2) data TYPE 3) starting value (maybe) example: int age = 18; age variable will contain an integer value; it initially stores the value 18

17 © 2012 Pearson Education, Inc. All rights reserved. 1-17 Variables Variable - a name given to a location in memory – 8 locations shown below 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007

18 © 2012 Pearson Education, Inc. All rights reserved. 1-18 Variables 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007 Java Virtual Machine (JVM) (not programmer) decides where in memory the declared variable is stored 72 Here’s a declaration of a varaible called length int length = 72; The variable called length is a symbolic name for the Memory location 0x003. Programmer doesn’t know it’s in 0x003.


Download ppt "© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various."

Similar presentations


Ads by Google