9
Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Embed Size (px)

Citation preview

Page 1: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Introduction to Java

CSIS 3701: Advanced Object Oriented Programming

Page 2: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Java Background

Original purpose: web page applets– Executable/dynamic applications running on web page– No longer main use, but affected language design

Server

applet

Client

browserrequested by browser

copy downloaded to browser

applet

Java code executed on client computer

Page 3: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Platform Independence

• Java applet must run on any client– Different OS, architecture, etc. different machine code– Cannot compile applet to single executable used by all

• Stage 1:Java source code compiled to “byte code”– Code for an abstract “Java virtual machine” (JVM)

Hello.java

Source code

(must end in .java)

Hello.class

Byte code stored on server

Page 4: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Platform Independence

• Stage 2: JVM on client runs “byte code”– Converted to native machine code line-by line and

executed on the fly

– JVM can be:• Part of browser• Built into NetBeans• Run separately from

command line (java Hello.class)

• Built directly into chip (mobile devices)

browser

appletLine 1Line 2Line 3Line 4…

Client

JVM

processor

convert and execute

Page 5: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Security and Safety

• Applet = unknown code running on your computer!– How to prevent malicious applets?

• Applets vs. Applications– Applets not allowed access to local files, network, etc.– Application: separate standalone process not run in

browser

Page 6: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Security and the Sandbox

• All Java programs execute in restricted area of memory (the “sandbox”)

• No explicit pointers– int *ptr = 100; // outside sandbox– *ptr = 0; // overwrite that memory

• Array bounds checking– int A[100];– A[1000000] = 0; // outside sandbox

Page 7: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Safety and Exception Handling

• Java programs cannot “crash” to OS• Exceptions caught and handled within JVM

– Browser/NetBeans/etc. notified

– Can handle as needed (error message displayed, etc.)

applet

JVM

int x = 0/0;ArithmeticException thrown;

Page 8: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Tradeoffs

• Many tradeoffs in language design– No “best” choices– Different languages make different choices

• Portability vs. Speed– “On the fly” interpretation slower than direct execution

of machine code

• Safety vs. Speed– Array bounds checking – Exception handling Require extra time

Page 9: Introduction to Java CSIS 3701: Advanced Object Oriented Programming

Basic Java Syntax

• Java syntax mostly same as C++– Java developed by C++ programmers

• Examples– Lines/blocks: ; {}– Control structures: if else for while switch…– Operators: = + - * / ++ -- +=…

== != > < >= <= && ||…– Comments: /* */ //