Why Use Java (pdf)

109 downloads 2982 Views 3MB Size Report
1. 1. Organization of Programming Languages-Cheng (Fall 2004). Concurrent and Distributed Programming. Java for C/C++ Programmers. 2. Organization of ...
Concurrent and Distributed Programming Java for C/C++ Programmers

Organization of Programming Languages-Cheng (Fall 2004)

1

Why Use Java? ?

Simple - Java has thrown out many of the complex features of C++ and C resulting in a simpler language (no pointers, no unions, no enumerations)

?

Object-oriented - Java is a single-root, single-inheritance object oriented language

?

Multithreaded - Java has a built-in support for multithreading

?

Distributed - Using Java RMI (remote method invocation) you can access objects on other machines almost as if they were local

?

Portable - programs written in the Java language are platform independent

Organization of Programming Languages-Cheng (Fall 2004)

2

The Java execution environment ?

Like C and C++ programs, Java programs are compiled.

?

Unlike C and C++ programs, Java programs are not compiled down to a platform-specific machine language. Instead, Java programs are compiled down to a platformindependent language called bytecode.

?

Bytecode is similar to machine language, ? but bytecode is not designed to run on any real, physical computer. ? Instead, bytecode is designed to be run by a program, called a Java Virtual Machine (JVM), which simulates a real machine.

Organization of Programming Languages-Cheng (Fall 2004)

1

3

JVM – Java Virtual Machine ?

?

JVM is an interpreter that translates Java bytecode into real machine language instructions that are executed on the underlying, physical machine A Java program needs to be compiled down to bytecode only once; it can then run on any machine that has a JVM installed

Organization of Programming Languages-Cheng (Fall 2004)

4

JVM – Cont.

Organization of Programming Languages-Cheng (Fall 2004)

5

Some Preliminaries ?

?

Two major types of Java programs : ? Stand-alone applications (run atop JVM) ? Applets: runs within a Java-enabled browser. Special programs: ? Server: special application that supports and serves clients on a network ?

?

Ex: web servers, proxy servers, mail servers, print servers

Servlet : special applet running on server side ? ?

run within Java Web servers, configuring or tailoring the server . Ex: java.lang.Object HttpServlet example.servlet.basic.HelloServlet

URL: http://browserinsight2.lunaimaging.com:8090/java_tut/javadoc/example/servlet/basic/ HelloServlet.html Organization of Programming Languages-Cheng (Fall 2004)

2

6

Running Java Programs: Application // file HelloWorld.java public class HelloWorld { public static void main( String [] args) { System.out.println(“Hello World !”); }

}

> javac HelloWorld.java The compilation phase: This command will produce the java bytecode file HelloWord.class > java HelloWorld The execution phase (on the JVM): This command will produce the output “Hello World!”

Organization of Programming Languages-Cheng (Fall 2004)

7

Running Java Programs: Applet ? Program specification: import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } } > javac HelloWorld.java The compilation phase: This command will produce the java bytecode file HelloWord.class ? HTML file to “execute” HelloWorld.class A Hello World Program

bytecode

Here is the output of my program:

Organization of Programming Languages-Cheng (Fall 2004)

8

Java Environment Elements ? ? ? ?

? ? ? ?

Essentials: Objects, strings, threads, numbers, input and output, data structures, system properties, date and time, and so on. Applets: The set of conventions used by applets. Networking: URLs, TCP (Transmission Control Protocol), UDP (User Datagram Protocol) sockets, and IP (Internet Protocol) addresses. Internationalization: Help for writing programs that can be localized for users worldwide. Programs can automatically adapt to specific locales and be displayed in the appropriate language. Security: Both low level and high level, including electronic signatures, public and private key management, access control, and certificates. Software components: Known as JavaBeansT M, can plug into existing component architectures. Object serialization: Allows lightweight persistence and communication via Remote Method Invocation (RMI). Java Database Connectivity (JDBCTM): Provides uniform access to a wide range of relational databases.

Organization of Programming Languages-Cheng (Fall 2004)

3

9

JDK: Java Development Kit

JDK: Java Development Kit SDK: Software Development Kit JRE: Java Run-time Environment

Organization of Programming Languages-Cheng (Fall 2004)

10

Basic Java Language Elements

Organization of Programming Languages-Cheng (Fall 2004)

11

Case Sensitivity ?

?

Case sensitivity: ? String is not the same as string ? MAIN is not the same as main Java keywords are all lower case ? e.g. public class static void

Organization of Programming Languages-Cheng (Fall 2004)

4

12

Naming Conventions ?

Methods and variables start with a leading lowercase letter ? next, push(), index, etc.

?

Classes start with a leading upper-case letter ? String, StringBuffer, Vector, Calculator, etc.

Organization of Programming Languages-Cheng (Fall 2004)

13

Naming Conventions – Cont. ?

Constants are all upper-case : DEBUG, MAX_SCROLL_X, CAPACITY ? final double PI = 3.1415926;

?

Word separation in identifiers is done by capitalization (e.g maxValue), ? except for constants where underscore is used (e.g MAX_SCROLL_X)

Organization of Programming Languages-Cheng (Fall 2004)

14

Comments ?

C++ Like: ? // comment .. ? /* this is a comment */

?

Javadoc Comments: automatically generates documentation from source code ? /** this is javadoc comment */

Organization of Programming Languages-Cheng (Fall 2004)

5

15

Flow control It is like C/C++:

do/while

switch

int i=5; do { // act1 i--; } while(i!=0);

if/else if(x==4) { // act1 } else { // act2 }

for int j; for(int i=0;i