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