Dierences from C++ not just a language, but an entire execution environment has: separate interface and implementation inheritance, threads, exception

Similar documents
An Overview of Java. overview-1

Fundamentals of Java Programming

CS506 Web Design and Development Solved Online Quiz No. 01

Java Interview Questions and Answers

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Crash Course in Java

Java from a C perspective. Plan

Building a Multi-Threaded Web Server

JAVA IN A NUTSHELL O'REILLY. David Flanagan. Fifth Edition. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Web Development in Java

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science Java Subset

The C Programming Language course syllabus associate level

Java Programming Fundamentals

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

OBJECT ORIENTED PROGRAMMING LANGUAGE

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

C++ INTERVIEW QUESTIONS

Programmation 2. Introduction à la programmation Java

RMI Client Application Programming Interface

public static void main(string[] args) { System.out.println("hello, world"); } }

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

JAVA INTERVIEW QUESTIONS

Java the UML Way: Integrating Object-Oriented Design and Programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Table of Contents. Java CGI HOWTO

Android Application Development Course Program

CSC 551: Web Programming. Spring 2004

Java SE 8 Programming

Getting Started with the Internet Communications Engine

Programming Languages

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

JAVA - FILES AND I/O

CS11 Java. Fall Lecture 7

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Creating a Simple, Multithreaded Chat System with Java

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Java EE Web Development Course Program

Chapter 1 Java Program Design and Development

IT6503 WEB PROGRAMMING. Unit-I

Network/Socket Programming in Java. Rajkumar Buyya

Java Application Developer Certificate Program Competencies

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Java SE 7 Programming

What is an I/O Stream?

Java Network. Slides prepared by : Farzana Rahman

Lecture 1 Introduction to Android

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

: provid.ir

CSC 551: Web Programming. Fall 2001

Java Programming Language

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Syllabus for CS 134 Java Programming

Socket programming. Complement for the programming assignment INFO-0010

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Coding Standard for Java

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Debugging Java Applications

To Java SE 8, and Beyond (Plan B)

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Computing Concepts with Java Essentials

Object Oriented Software Design II

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Java and Java Virtual Machine Security

INPUT AND OUTPUT STREAMS

JAVA - EXCEPTIONS. An exception can occur for many different reasons, below given are some scenarios where exception occurs.

What are exceptions? Bad things happen occasionally

Java SE 7 Programming

Java SE 7 Programming

Java (12 Weeks) Introduction to Java Programming Language

Moving from CS 61A Scheme to CS 61B Java

Network Communication

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Java CPD (I) Frans Coenen Department of Computer Science

Habanero Extreme Scale Software Research Project

1. Overview of the Java Language

OpenCL Static C++ Kernel Language Extension

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Lecture 5: Java Fundamentals III

Online Recruitment System 1. INTRODUCTION

Web development... the server side (of the force)

CS 209 Programming in Java #1

Quick Introduction to Java

Lesson: All About Sockets

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Assignment 4 Solutions

Built-in Concurrency Primitives in Java Programming Language. by Yourii Martiak and Mahir Atmis

Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Introduction to Programming System Design. CSCI 455x (4 Units)

MA-WA1920: Enterprise iphone and ipad Programming

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

DIPLOMADO DE JAVA - OCA

Transcription:

Java white paper description simple Java for Network Programming object-oriented David L. Levine Washington University,St. Louis levine@cs.wustl.edu distributed interpreted robust secure 1 2 Java white paper description, cont'd Similarities with C++ architecture neutral syntax portable high-performance multithreaded dynamic primitive data types (except that char is Unicode) control ow constructs and operators // and / ::: for comments ::: for javadoc comments 3 4

Dierences from C++ not just a language, but an entire execution environment has: separate interface and implementation inheritance, threads, exception handling does not have: separate class declaration, pointers, globals, structures, unions, enumerated types, typedefs, templates, operator overloading single implementation inheritance multiple interface inheritence Dierences from C++, cont'd no preprocessor no macros, no #dene'd constants, no #include methods are not explicitly declared virtual public and non-nal methods are virtual abstract instead of = 0 for pure virtual methods limited access to underlying system (through System Properties) no getenv() 5 6 References (object and array) object and array variables are references no & or no pointer arithmetic or sizeof eld access is always via \." assignment: use clone() instead of = equality: use equals() instead of == a variable equal to null doesn't refer to any object Java memory management must dynamically allocate instances via operator new except of primitive (not object or array) types no allocation o stack no explicit deallocation all objects are reference counted when its reference count goes to 0, garbage collector can deallocate an object object and array parameters passed by reference, e.g., public static void doublebuer (StringBuer s) f s.append (s); // side-eect: modies s g 7 can manually invokegarbage collector (System.gc()) 8

Dening Java classes Example class with both implementation and interface inheritance. @author Wild Hacker @version 0.1 public class Foo extends Bar implements Baz constructor: accesses private instance member @param x argument of primitive type is passed by value public Foo (int x) super( x ); // calls Bar constructor x += foo_int_; // no side-effect static method: does not access any instance fields @param s argument of object type is passed by reference public static void doublebuffer (StringBuffer s) s.append (s); // side-effect // the class instance is allocated at class load time... private static Foo foo_ = new Foo (); private int foo_int_ = 21; Application Programming Interface (API) package lang Object, Class, Thread, Math System, Runtime, Process Throwable (Exceptions, Errors) shadows of primitive data types String and StringBuer package io streams and les package net 9 sockets, URLs, Internet addresses 10 package util API, cont'd BitSet, Date, Hashtable, Vector, Stack, etc. package applet extend class Applet security restrictions: limited access to environment init() entry point instead of main() java.io package System.out.println (\hello"); le output FileOutputStream PrintStream le input FileInputStream DataInputStream AWT (Abstract Windowing Toolkit) layer streams (System.in is an InputStream) DataInputStream in = new DataInputStream (System.in); String input = in.readline(); 11 12

Thread synchronization java.lang.thread class states new, runnable, blocked, dead java.lang.thread package always construct with String name always call start() in turn calls run() synchronized keyword synchronized method monitor grants thread exclusive access for invoking object synchronized class (static) method monitor grants thread exclusive access for all class static objects synchronized statement critical section around object or array java.lang.object methods wait() timed wait() no indication of timeout notify() or notifyall() 13 14 Thread scheduling scheduling is implementation dependent cooperative on Solaris 2.x time-sliced on Windows priorities MIN PRIORITY == 1 NORM PRIORITY == 5 MAX PRIORITY == 10 Thread groups a Thread is always constructed in a Thread- Group can specify other than the (default) main Thread can perform operations on all Threads in a ThreadGroup daemonize, suspend, resume, stop ThreadLister utility 15 16

import java.util.hashtable; Example: Debate example threaded program public class Debate entry point @param argv currently unused public static void main (String[] argv) Moderator moderator = Moderator.getModerator (); Example: Moderator Thread import java.util.enumeration; import java.util.hashtable; import java.io.; Debate moderator public class Moderator extends Thread private Moderator( String name ) super (name); accessor for the Singleton moderator public static Moderator getmoderator () return _themoderator; moderator.adddebater ("Clinton", "ten million new jobs"); moderator.adddebater ("Dole", "liberal!"); moderator.start(); add a debater @param name the debater's name @to_say the debater's (scripted) text public void adddebater( String name, String to_say ) Redemopublicrat debater = new Redemopublicrat (name, to_say); _debaters.put ("Mr. " + name, debater); 17 18 Example: Moderator, cont'd Thread run loop public void run() Enumeration debaters = _debaters.elements (); while (debaters.hasmoreelements()) ((Redemopublicrat) debaters.nextelement ()).start(); yield(); // let the debater initialize itself for (int i = 0; i < 10; ++i) debaters = _debaters.keys (); while (debaters.hasmoreelements ()) String name = (String) debaters.nextelement (); Redemopublicrat debator = (Redemopublicrat) _debaters.get (name); System.out.println (getname() + ": " + name); debator.go (); yield(); static private Moderator _themoderator = new Moderator ("Lehrer"); static private Hashtable _debaters = new Hashtable(); 19 import java.io.; Example: Debater public class Redemopublicrat extends Thread public Redemopublicrat( String name, String to_say ) super (name); setdaemon (true); // don't wait for me when debate ends! _sez = to_say; public void run() while (true) waitformoderator(); System.out.println (getname() + ": " + _sez); accept indication (from Moderator) that we can continue public synchronized void go() notify(); private synchronized void waitformoderator() try wait(); catch ( InterruptedException interruption ) // ignore interruption private String _sez; 20

Exceptions and Errors Exceptions must be caught or thrown Errors and RuntimeExceptions need not be handled: they get passed up the call stack all have getmessage() method for retrieving message String Exceptions and Errors, cont'd example of user-dened Exception: public class InvalidDebaterException extends Exception Constructor. @param debater the name of the invalid debater public InvalidDebaterException (String debater) super (debater); all have printstacktrace() method 21 22 Idioms java.net package can have main() for each class, for testing provides passive ServerSocket and active Socket classes tostring() method permits implicit conversion transparent hostname resolution java.io streams can be layered on top of socket's InputStream and OutputStreams all socket operations are blocking there is no select() 23 String, Vector, Hashtable Threads avoid sleep() to avoid polling avoid priorities to not depend on scheduler match every wait() with a notify() to pass primitive types by reference, put into array and pass that 24

Performance declare classes or methods nal to enable inlining Using Java only one (public) class per le use StringBuer for Strings that need to be modied classes are loaded dynamically on demand: timing should be measured after all classes are loaded just-in-time compilers native methods are supported javac foo.java java foo entry point in class foo: public static void main (String[] argv) f: ::g disassembler: javap -c foo.class debugger: jdb foo 25 26 CLASSPATH environment variable or command line argument contains any number of directories colon separated on UNIX, semicolon on Windows directories searched for root of a package hierarchy if package EDU.wustl.cs.544.user.utils is rooted at /home/user/classes/cs544/java, then CLASSPATH should contain.:/home/user/classes/cs544/java Packages name with package statement naming convention: EDU.wustl.cs.544.<: ::> access other packages with import statement import EDU.wustl.cs.544.user.utils.; 27 28

Javadoc for documenting public classes, methods, and data members package listing package tree name index detailed descriptions javadoc [-d directory] package/le names start javadoc comment: end javadoc comment: can contain most HTML tags and special tags Javadoc special tags @version (does not seem to work?) @author (does not seem to work?) @see classname @param name description @exception name description @return description 29 30 Limitations Thread interruption not supported by current implementations ThreadDeath doesn't appear to always be caught Resources http://java.sun.com language reference virtual machine reference ThreadGroups may be leaked http://www.cs.wustl.edu/schmidt/cs544/ on our systems, /usr/bin must be ahead of /pkg/gnu/bin in path 31 32