DOC PREVIEW
UMD CMSC 132 - Stream Input/Output

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 132: Object-Oriented Programming IINelson Padua-PerezWilliam PughDepartment of Computer ScienceUniversity of Maryland, College Park2Stream Input/Output StreamA connection carrying a sequence of dataBytes →→→→ InputStream, OutputStreamCharacters →→→→ FileReader, PrintWriterFrom a source to a destinationKeyboard FileNetworkMemoryBasis for modern I/O3Using StreamsOpening a stream Connects program to external dataLocation of stream specified at openingOnly need to refer to stream Usage1.import java.io.* ; 2.Open stream connection3.Use stream →→→→ read and / or writeCatch exceptions if needed4.Close streamExamples See fileExamples package4Initialization BlockDefinitionBlock of code used to initialize static & instance variables for classMotivationEnable complex initializations for static variablesControl flowExceptionsShare code between multiple constructors for same class5Initialization Block Types Static initialization blockCode executed when class loaded Initialization blockCode executed when each object created (at beginning of call to constructor)Exampleclass foo {static { A = 1; } // static initialization block { A = 2; } // initialization block}6Variable InitializationVariables may be initializedAt time of declarationIn initialization blockIn constructorOrder of initialization1.Declaration, initialization block(in the same order as in the class definition) 2.Constructor7Variable Initialization – Exampleclass Foo {static { A = 1; } // static initialization block static int A = 2; // static variable declarationstatic { A = 3; } // static initialization block { B = 4; } // initialization blockprivate int B = 5; // instance variable declaration{ B = 6; } // initialization blockFoo() { // constructorA = 7;B = 8;} // now A = 7, B = 8} // initializations executed in order of number8AnnotationsAnnotation – Java construct that allow us to add validity constraints to Java ClassesValidity constraint exampleA instance variable cannot assume a negative valueA parameter can not be nullA method in a class must override a method in its superclassSyntax at-sign (@) followed by annotation type and a parenthesized list of element-value pairsExample @DefaultAnnotationForParameters(NonNull.class)You can ignore annotations in the code distribution for class projects9Reviewing Bit-Operationsand x 11010 y 10110x and y 10010orx 11010 y 10110x and y 11110xorx 11010 y 10110x and y 01100Java Bitwise operators& and| or^ exclusive or~ complement10BitSet ClassImplements a vector of bits where the bits of the set are indexed by nonnegative integersMethodsBitSet() – New bit setBitSet(int nbits) – Bit set large enough to represent bits with indices from 0 through nbits – 1and(BitSet set) – Performs logical and between the current object and the set parameter (current object is updated with the result)or(BitSet set) – Performs logical or between the current object and the set parameter (current object is updated with the result)cardinality() – Returns number of bits set to 1flip(int bitIndex) – Sets the bit at the specified indexget(int bitIndex) – Returns true if the bit at bitIndex is set; false otherwiselength() – Index of the highest set bit + 1. It returns zero if the BitSet contains no bits set. size() – Number of bits space used by the BitSet to represent bit valuestoString() – For every bit set, the decimal representation of that index is included in the result.Example (See Computers.java)11Two-Dimensional Arrays of PrimitivesEach row in a two-dimensional array is an arrayThe rows can have different lengthsDefining a primitive array where rows have the same lengthint [ ][ ] data = new int[3][4];Defining a primitive data array where rows have different lengths (ragged array)int [ ][ ] ragged = new int[2][];ragged[0] = new int[3];ragged[1] = new int[1];12Two-Dimensional Arrays of ObjectsEach row in a two-dimensional array is an arrayThe rows can have different lengthsDefining an array where rows have the same lengthString [ ][ ] data = new String[3][4];Important: Keep in mind we have created a two-dimensional array of references to String objects. No String object is present yet. We can also have ragged arraysExample (See


View Full Document

UMD CMSC 132 - Stream Input/Output

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download Stream Input/Output
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Stream Input/Output and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Stream Input/Output 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?