java - YSU Computer Science & Information Systems

Download Report

Transcript java - YSU Computer Science & Information Systems

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
Client
requested
by browser
applet
browser
applet
copy
downloaded
to browser
Java code
executed on
client computer
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
Platform Independence
• Stage 2: JVM on client runs “byte code”
– Converted to native machine code line-by line and
executed on the fly
Client
browser
– JVM can be:
• Part of browser
• Built into NetBeans
• Run separately from
command line
(java Hello.class)
• Built directly into chip
(mobile devices)
JVM
applet
Line 1
Line 2
Line 3
Line 4
…
convert and
execute
processor
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
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
Safety and Exception Handling
• Java programs cannot “crash” to OS
• Exceptions caught and handled within JVM
– Browser/NetBeans/etc. notified
JVM
applet
int x = 0/0;
ArithmeticException thrown;
– Can handle as needed (error message displayed, etc.)
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
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: /*
*/
//
Simple Types
• Numeric types:
–
–
–
–
int
short
byte
long
32 bits
16 bits
8 bits
64 bits
Defined standard in Java language
– float 32 bits, ~7 digits after decimal
– double 64 bits, ~13 digits after decimal
Simple Types
• Boolean type
– keywords true and false
– Example: boolean b = true;
• Char type
– 16-bit Unicode for
international purposes
– Examples:
char c1 = ‘A’;
char c2 = ‘\u3218’
Tradeoff:
memory vs.
internationalization
– Numeric representation of non-ASCII character
Type Conversion
• int x = 3.7;
– What does this do?
• Weak typing (C/C++)
– Language attempts to convert one type to another
– 3.7 truncated to 3 and stored in x
• Strong typing (Java)
– Must be no possible ambiguity or information loss
– Otherwise syntax error
Type Conversion
• Other examples:
– if (1) {…}
– long y; int x;
x = y;
Condition must be true or false
Possible information loss
• Must explicitly cast to less precise type
– int x = (int)3.7;
• Tradeoff: safety vs. convenience
Constants and Types
• What type is the number 3? The number 3.7?
– Number without decimal is int.
– Number with decimal is double.
• Possible type conversion problems
Example: float y = 3.7;
– Syntax error – trying to store double as float
– Java hack: Follow constant with f
float y = 3.7f;