needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

Similar documents
1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

PIC 20A Streams and I/O

Programming Languages and Techniques (CIS120)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lecture 8 (7.5?): Javascript

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

CS193j, Stanford Handout #26. Files and Streams

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Darshan Institute of Engineering & Technology for Diploma Studies

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Chapter 10. IO Streams

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Objec&ves. Review. Standard Error Streams

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Software 1. Java I/O

CSC 1214: Object-Oriented Programming

Object-Oriented Programming Design. Topic : Streams and Files

Software Practice 1 - File I/O

Programming Languages and Techniques (CIS120)

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Performing input and output operations using a Byte Stream

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

Byte and Character Streams. Reading and Writing Console input and output

Special Topics: Programming Languages

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

HST 952. Computing for Biomedical Scientists Lecture 8

Exceptions and Working with Files

I/O Streams. Object-oriented programming

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Character Stream : It provides a convenient means for handling input and output of characters.

Selected Java Topics

Object-Oriented Programming in the Java language

File Processing in Java

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系

IT101. File Input and Output

ITI Introduction to Computer Science II

Introduction Unit 4: Input, output and exceptions

Lab 5: Java IO 12:00 PM, Feb 21, 2018

File Operations in Java. File handling in java enables to read data from and write data to files

Programming. Syntax and Semantics

1 Shyam sir JAVA Notes

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

WOSO Source Code (Java)

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Example: Copying the contents of a file

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

copy.dept_change( CSE ); // Original Objects also changed

CSC Java Programming, Fall Java Data Types and Control Constructs

Programming Language Basics

Lecture 11.1 I/O Streams

Programming Language Concepts: Lecture 2

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

20 Most Important Java Programming Interview Questions. Powered by

Brief Summary of Java

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Reading and Writing Files

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Core Java Syllabus. Overview

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

3. Java - Language Constructs I

Programming Languages and Techniques (CIS120)

C17: I/O Streams and File I/O

Object Oriented Software Design

Object Oriented Software Design

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

Programming Languages and Techniques (CIS120)

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

1.00 Lecture 30. Sending information to a Java program

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Lesson 3: Accepting User Input and Using Different Methods for Output

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

Programming Languages and Techniques (CIS120)

COP 3330 Final Exam Review

Sri Vidya College of Engineering & Technology Question Bank

Java Threads and intrinsic locks

Chapter 1 Getting Started

Special Exercise Unit: Introduction to Java

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

Short Notes of CS201

2 rd class Department of Programming. OOP with Java Programming

Getting started with Java

Index COPYRIGHTED MATERIAL

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Transcription:

Java history invented mainly by James Gosling ([formerly] Sun Microsystems) 1990: Oak language for embedded systems needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine 1993: run in a browser instead of a microwave renamed "Java" Java Virtual Machine (JVM) runs in browser 1994: Netscape supports Java in their browser enormous hype: a viable threat to Microsoft 1997-2002: Sun sues Microsoft multiple times over Java MSFT guilty of anti-competitive actions mostly settled by 4/04 significant language changes in Java 1.5 (9/04) generics, auto box/unbox, for loop, annotations,... Java 1.6 (== 6.0) 12/06 is mostly incremental changes Java vs. C and C++ no preprocessor import instead of #include constants use static final declaration C-like basic types, operators, expressions sizes, order of evaluation are specified object-oriented everything is part of some class objects all derived from Object class klunky mechanisms for converting basic <-> object references instead of pointers for objects null references, garbage collection, no destructors == is object identity, not content identity all arrays are dynamically allocated int[] a; // a is now null a = new int[100]; strings are more or less built in C-like control flow, but labeled break and continue instead of goto exceptions: try { catch(exception) { threads for parallelism within a single process

Basic data types Java tries to specify some of the unspecified or undefined parts of C and C++ basic types: boolean true / false (no conversion to/from int) byte 8 bit signed char 16 bit unsigned (Unicode character) int 32 bit signed short, long, float, double String is sort of built-in (an Object) "..." is a String holds 16-bit Unicode chars, NOT bytes does NOT have a null terminator; String.length() returns length + is string concatenation operator; += appends immutable: string operations make new strings Classes & objects in Java everything is part of some object all classes are derived from class Object member functions & variables defined inside class internal functions should not be public, variables should never be public every object is an instance of some class created dynamically by calling new class variable: a variable declared static in class only one instance in entire program, exists even if the class is never instantiated the closest thing to a global variable in Java public class RE { static int num_res = 0; public RE(String re) { num_res++;... public static int RE_count() { return num_res;

Class methods most methods associated with an object instance if declared static, amounts to a global function class RE { public boolean equals(re r) { return re.equals(r.re); public static boolean equals(re r1, RE r2) { return r1.re.equals(r2.re); public static void main(string[] args) { RE r1 = new RE(args[0]); RE r2 = new RE(args[1]); if (r1.equals(r2))... // member function if (equals(r1, r2))... // static function if (r1 == r2)... // object equality some classes are entirely static members and class functions, e.g., Math, System, Color can't make a new one: no constructor Scope and visibility only one public class per file public class hello { has to be in hello.java public methods of the class are visible outside the file other methods are not default is file private other classes in a file are visible within the file but not visible outside the file variables of a class are always visible within the class and to other classes in the same file unless private static variables are visible to all class instances class Math { public static double PI = 3.141592654; // etc. double d = Math.cos(Math.PI);

Destruction & garbage collection interpreter keeps track of what objects are currently in use memory can be released when last use is gone release does not usually happen right away has to be garbage-collected garbage collection happens automatically separate low-priority thread does garbage collection no control over when this happens can set object reference to null to encourage it no destructor (unlike C++) can define a finalize() method for a class to reclaim other resources, close files, etc. no guarantee that a finalizer will ever be called garbage collection is a great idea but this does not seem like a great design I/O and file system access byte I/O for raw data read(), write(), InputStream, OutputStream character I/O for Unicode (Reader, Writer) InputReader and OutputWriter InputStreamReader, OutputStreamWriter BufferedReader, BufferedWriter byte-at-a-time I/O System.in,.out,.err like stdin, stdout, stderr read() returns next byte of input, -1 for end of file any error causes an I/O Exception import java.io.*; public class cat1 { public static void main(string args[]) throws IOException { int b; while ((b = System.in.read()) >= 0) System.out.write(b);

Buffered byte I/O to/from files buffering is usually required; too slow otherwise import java.io.*; public class cp2 { public static void main(string[] args) throws IOException { int b; FileInputStream fin = new FileInputStream(args[0]); FileOutputStream fout = new FileOutputStream(args[1]); BufferedInputStream bin = new BufferedInputStream(fin); BufferedOutputStream bout = new BufferedOutputStream(fout); while ((b = bin.read()) > -1) bout.write(b); bin.close(); bout.close(); Exceptions C-style error handling ignore errors -- can't happen return a special value from functions, e.g., -1 from system calls like open(), NULL from library functions like fopen() leads to complex logic error handling mixed with computation repeated code or goto's to share code limited set of possible return values extra info via errno and strerr: global data some functions return all possible values so no possible error return value is available exceptions are the Java solution (also in C++) exception indicates unusual condition or error occurs when program executes a throw statement control unconditionally transferred to catch block if no catch in current function, passes to calling method keeps passing up until caught ultimately caught by system at top level

try { catch { a method can catch exceptions public void foo() { try { // if anything here throws an IO exception // or a subclass, like FileNotFoundException catch (IOException e) { // this code will be executed to deal with it finally { // this is done regardless or it can throw them, to be handled by caller a method must list exceptions it can throw exceptions can be thrown implicitly or explicitly public void foo() throws IOException { // if anything here throws any kind of IO exception // foo will throw an exception, to be handled by its caller With exceptions public class cp2 { public static void main(string[] args) { int b; try { FileInputStream fin = new FileInputStream(args[0]); FileOutputStream fout = new FileOutputStream(args[1]); BufferedInputStream bin = new BufferedInputStream(fin); BufferedOutputStream bout = new BufferedOutputStream(fout); while ((b = bin.read()) > -1) bout.write(b); bin.close(); bout.close(); catch (IOException e) { System.err.println("IOException " + e);

Why exceptions? reduced complexity if a method returns normally, it worked each statement in a try block knows that previous statements worked, without explicit tests if the try exits normally, all the code in it worked error code is grouped in a single place can't unconsciously ignore possibility of errors have to at least think about what exceptions can be thrown public static void main(string args[]) throws IOException { int b; while ((b = System.in.read()) >= 0) System.out.write(b); don't use exceptions for normal flow of control don't use for "normal" unusual conditions e.g., in.read() returns 1 for EOF instead of throwing an exception should a file open that fails throw an exception? Character I/O (char instead of byte) use a different set of functions for char I/O works properly with Unicode ('\u1234' literals) InputStreamReader adapts from bytes to chars OutputStreamWriter adapts from chars to bytes use Buffered(Reader Writer) for speed public class cat3 { public static void main(string[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); String s; while ((s = in.readline())!= null) { out.write(s); out.newline(); out.flush(); // required!!

Unicode (www.unicode.org) universal character encoding scheme ~100,000 characters today UTF-16: 16 bit internal representation encodes all characters used in all languages numeric value, name, case, directionality, expansion mechanism for > 2 16 characters UTF-8: byte-oriented external form variable-length encoding, self-synchronizing within a couple of bytes ASCII compatible: 7-bit characters occupy 1 byte 00000000 0bbbbbbb 0bbbbbbb 00000bbb bbbbbbbb 110bbbbb 10bbbbbb bbbbbbbb bbbbbbbb 1110bbbb 10bbbbbb 10bbbbbb analogous longer encoding for chars in extended set Java supports Unicode char data type is 16-bit Unicode String data type is 16-bit Unicode chars \uhhhh is Unicode character hhhh (h == hex digit); use in "..." and '.' Visibility private, public, protected public class foo { private v; public void f(); // people can use this class // can't see this variable // can use this public method public class, method or variable visible everywhere private method or variable only by methods of the class protected method or variable only by methods of the class, subclasses, and other classes in the same package default visibility ("package" visibility) only visible in class that defines it and other classes in the same package (but not subclasses in other packages) package a group of related and possibly cooperating classes all non-private variables & members visible to all other classes in package loosely, like mutual friends in the C++ sense