ebook img

Java Precisely, third edition (The MIT Press) PDF

212 Pages·2016·2.69 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java Precisely, third edition (The MIT Press)

Java Precisely Third Edition Peter Sestoft Java Precisely Third Edition TheMITPress Cambridge,Massachusetts London,England (cid:2)c 2016MassachusettsInstituteofTechnology Allrightsreserved.Nopartofthisbookmaybereproducedinanyformbyanyelectronicormechanical means(includingphotocopying,recording,orinformationstorageandretrieval)withoutpermissionin writingfromthepublisher. MITPressbooksmaybepurchasedatspecialquantitydiscountsforbusinessorsalespromotionaluse. ThisbookwassetinTimesbytheauthorusingLATEXandwasprintedandboundintheUnitedStatesof America. LibraryofCongressCataloging-in-PublicationData Names:Sestoft,Peter. Title: Javaprecisely/PeterSestoft. Description:Thirdedition. |Cambridge,MA:TheMITPress,[2016]|Includesbibliographicalreferences andindex. Identifiers:LCCN2015038522|ISBN9780262529075(pbk.:alk. paper) Subjects:LCSH:Java(Computerprogramlanguage) Classification: LCCQA76.73.J38S4352015|DDC005.13/3–dc23LCrecordavailableat http://lccn.loc.gov/2015038522 10 9 8 7 6 5 4 3 2 1 Contents Preface xi NotationalConventions xii 1 RunningJava:Compilation,Loading,andExecution 2 2 NamesandReservedNames 2 3 JavaNamingConventions 2 4 CommentsandProgramLayout 2 5 Types 4 5.1 PrimitiveTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5.2 ReferenceTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5.3 ArrayTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 5.4 Boxing:WrappingPrimitiveTypesasReferenceTypes . . . . . . . . . . . . . . . . . . . . . 4 5.5 SubtypesandCompatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5.6 SignaturesandSubsumption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5.7 TypeConversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 6 Variables,Parameters,Fields,andScope 8 6.1 ValuesBoundtoVariables,Parameters,orFields . . . . . . . . . . . . . . . . . . . . . . . . 8 6.2 VariableDeclarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 6.3 ScopeofVariables,Parameters,andFields . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 7 Strings 10 7.1 StringFormatting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 8 Arrays 16 8.1 ArrayCreationandAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 8.2 ArrayInitializers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 8.3 MultidimensionalArrays. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 8.4 TheUtilityClassArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 9 Classes 22 9.1 ClassDeclarationsandClassBodies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 9.2 Top-LevelClasses,NestedClasses,MemberClasses,andLocalClasses . . . . . . . . . . . . 22 9.3 ClassModifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 9.4 TheClassModifierspublic,final,abstract . . . . . . . . . . . . . . . . . . . . . . . . . 24 9.5 Subclasses,Superclasses,ClassHierarchy,Inheritance,andOverriding . . . . . . . . . . . . 24 9.6 FieldDeclarationsinClasses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 9.7 TheMemberAccessModifiersprivate,protected,public . . . . . . . . . . . . . . . . . 26 9.8 MethodDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 9.9 ParameterArraysandVariable-ArityMethods . . . . . . . . . . . . . . . . . . . . . . . . . 30 vi Contents 9.10 ConstructorDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 9.11 NestedClasses,MemberClasses,LocalClasses,andInnerClasses . . . . . . . . . . . . . . 32 9.12 AnonymousClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 9.13 InitializerBlocks,FieldInitializers,andInitializers . . . . . . . . . . . . . . . . . . . . . . . 32 10 ClassesandObjectsintheComputer 34 10.1 WhatIsaClass? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 10.2 WhatIsanObject? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 10.3 InnerObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 11 Expressions 36 11.1 TableofExpressionForms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 11.2 ArithmeticOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 11.3 LogicalOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 11.4 BitwiseOperatorsandShiftOperators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 11.5 AssignmentExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 11.6 ConditionalExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 11.7 ObjectCreationExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 11.8 InstanceTestExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 11.9 FieldAccessExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 11.10 TheCurrentObjectReferencethis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 11.11 TypeCastExpression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 11.12 MethodCallExpressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 11.13 LambdaExpressions(Java8.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 11.14 MethodReferenceExpressions(Java8.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 12 Statements 52 12.1 ExpressionStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 12.2 BlockStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 12.3 TheEmptyStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 12.4 ChoiceStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 12.5 LoopStatements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 12.6 Returns,LabeledStatements,Exits,andExceptions . . . . . . . . . . . . . . . . . . . . . . 60 12.7 TheTry-with-ResourcesStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 12.8 TheassertStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 13 Interfaces 66 13.1 InterfaceDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 13.2 ClassesImplementingInterfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 13.3 DefaultandStaticMethodsonInterfaces(Java8.0) . . . . . . . . . . . . . . . . . . . . . . . 68 13.4 AnnotationTypeDeclarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 14 EnumTypes 70 15 Exceptions,CheckedandUnchecked 72 16 Compilation,SourceFiles,ClassNames,andClassFiles 74 Contents vii 17 PackagesandJarFiles 74 18 MathematicalFunctions 76 19 StringBuildersandStringBuffers 78 20 Threads,ConcurrentExecution,andSynchronization 80 20.1 ThreadsandConcurrentExecution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 20.2 LocksandthesynchronizedStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 20.3 OperationsonThreads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 20.4 OperationsonLockedObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 20.5 TheJavaMemoryModelandVisibilityAcrossThreads . . . . . . . . . . . . . . . . . . . . 86 21 GenericTypesandMethods 88 21.1 Generics:Safety,Generality,andEfficiency . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 21.2 GenericTypes,TypeParameters,andTypeInstances . . . . . . . . . . . . . . . . . . . . . . 88 21.3 HowCanTypeInstancesBeUsed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 21.4 GenericClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 21.5 ConstraintsonTypeParameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 21.6 HowCanTypeParametersBeUsed? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 21.7 GenericInterfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 21.8 GenericMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 21.9 WildcardTypeArguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 21.10 TheRawType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 21.11 TheImplementationofGenericTypesandMethods . . . . . . . . . . . . . . . . . . . . . . 100 22 GenericCollectionsandMaps 102 22.1 InterfaceCollection<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 22.2 InterfaceList<T>andItsImplementationsLinkedList<T>andArrayList<T> . . . . . . . . . 105 22.3 InterfaceSet<T>andItsImplementationsHashSet<T>andLinkedHashSet<T>. . . . . . . . 106 22.4 InterfaceSortedSet<T>andImplementationTreeSet<T> . . . . . . . . . . . . . . . . . . . 106 22.5 InterfaceMap<K,V>andImplementationHashMap<K,V> . . . . . . . . . . . . . . . . . . 108 22.6 InterfaceSortedMap<K,V>andImplementationTreeMap<K,V> . . . . . . . . . . . . . . . 110 22.7 GoingthroughaCollection:InterfacesIterator<T>andIterable<T> . . . . . . . . . . . . . . 112 22.8 InterfaceListIterator<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 22.9 Equality,HashCodes,andComparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 22.10 TheComparator<T>Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 22.11 TheUtilityClassCollections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 22.12 ChoosingtheRightCollectionClassorMapClass . . . . . . . . . . . . . . . . . . . . . . . 120 23 FunctionalInterfaces(Java8.0) 122 23.1 FunctionalProgramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 23.2 GenericFunctionalInterfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 23.3 Primitive-TypeSpecializedFunctionalInterfaces . . . . . . . . . . . . . . . . . . . . . . . . 124 23.4 CovarianceandContravarianceinFunctionalInterfaces . . . . . . . . . . . . . . . . . . . . 126 23.5 InterfaceFunction<T,R> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 23.6 InterfaceUnaryOperator<T>. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 viii Contents 23.7 InterfacesPredicate<T>andBiPredicate<T,U> . . . . . . . . . . . . . . . . . . . . . . . . . 128 23.8 InterfacesConsumer<T>andBiConsumer<T,U> . . . . . . . . . . . . . . . . . . . . . . . . 128 23.9 InterfaceSupplier<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 23.10 InterfaceBiFunction<T,U,R> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 23.11 InterfaceBinaryOperator<T> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 24 StreamsforBulkData(Java8.0) 132 24.1 CreatingStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 24.2 StreamBuilders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 24.3 MethodsonStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 24.4 NumericStreams:DoubleStream,IntStream,andLongStream . . . . . . . . . . . . . . . . . 140 24.5 SummaryStatisticsforNumericStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 24.6 CollectorsonStreams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 25 ClassOptional<T>(Java8.0) 146 26 InputandOutput 148 26.1 CreatinganIOStreamfromAnotherOne . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 26.2 KindsofInputandOutputMethods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 26.3 Imports,Exceptions,ThreadSafety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 26.4 SequentialCharacterInput:Readers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 26.5 SequentialCharacterOutput:Writers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 26.6 PrintingPrimitiveDatatoaCharacterStream:PrintWriter . . . . . . . . . . . . . . . . . . . 154 26.7 TheAppendableInterfaceandtheCharSequenceInterface . . . . . . . . . . . . . . . . . . . 154 26.8 ReadingPrimitiveDatafromaCharacterStream:StreamTokenizer . . . . . . . . . . . . . . 156 26.9 SequentialByteInput:InputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 26.10 SequentialByteOutput:OutputStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 26.11 BinaryInput-OutputofPrimitiveData:DataInputandDataOutput . . . . . . . . . . . . . . . 160 26.12 SerializationofObjects:ObjectInputandObjectOutput . . . . . . . . . . . . . . . . . . . . 162 26.13 BufferedInputandOutput . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 26.14 RandomAccessFiles: RandomAccessFile . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 26.15 Files,Directories,andFileDescriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 26.16 ThreadCommunication:PipedInputStreamandPipedOutputStream . . . . . . . . . . . . . . 168 26.17 SocketCommunication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 27 Reflection 172 27.1 ReflectiveUseofTypes:TheClass<T>Class . . . . . . . . . . . . . . . . . . . . . . . . . . 172 27.2 Reflection: TheFieldClass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 27.3 Reflection: TheMethodClassandtheConstructor<T>Class . . . . . . . . . . . . . . . . . . 174 27.4 ExceptionsThrownWhenUsingReflection . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 28 MetadataAnnotations 176 29 WhatIsNewinJava8.0 178 References 180 Contents ix Index 181

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.