Java in a nutshell by david flanagan 6th

418 7 0
Java in a nutshell by david flanagan 6th

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

6t h ■■ Understand basic techniques used in object-oriented design ■■ Examine concurrency and memory, and how they’re intertwined ■■ Work with Java collections and handle common data formats ■■ Delve into Java’s latest I/O APIs, including asynchronous channels ■■ Use Nashorn to execute JavaScript on the Java Virtual Machine ■■ Become familiar with development tools in OpenJDK David Flanagan, senior staff frontend software engineer at Mozilla, has written several books for O’Reilly, including JavaScript: The Definitive Guide, jQuery Pocket Reference, The Ruby Programming Language, and previous editions of Java in a Nutshell Twitter: @oreillymedia facebook.com/oreilly PROGR AMMING/JAVA US $59.99 Benjamin J Evans is the cofounder and Technology Fellow of jClarity, a startup that delivers performance tools to help development & ops teams He is a Java Champion; JavaOne Rockstar; coauthor of The Well-Grounded Java Developer (Manning); and a regular public speaker on the Java platform, performance, concurrency, and related topics on Explore generics, enumerations, annotations, and lambda expressions va ■■ —Kevlin Henney consultant, author, speaker, editor of 97 Things Every Programmer Should Know Java Java in a Nutshell A DESKTOP QUICK REFERENCE CAN $62.99 ISBN: 978-1-449-37082-4 iti Learn object-oriented programming, using basic Java syntax ” Ja ■■ rs Get up to speed on language details, including Java changes references, this latest edition is still the simplest and most definitive way to cut through to the answers you need Evans & Flanagan ■■ ve The second section is a reference to core concepts and APIs that shows you how to perform real programming work in the Java environment a world of blogged “ Inopinions and javadoc’d SIXTH EDITION Java in a Nutshell The latest edition of Java in a Nutshell is designed to help experienced Java programmers get the most out of Java and 8, but it’s also a learning path for new developers Chock full of examples that demonstrate how to take complete advantage of modern Java APIs and development best practices, the first section of this thoroughly updated book provides a fast-paced, no-fluff introduction to the Java programming language and the core runtime aspects of the Java platform Ed Co Java in a Nutshell Benjamin J Evans & David Flanagan 6t h ■■ Understand basic techniques used in object-oriented design ■■ Examine concurrency and memory, and how they’re intertwined ■■ Work with Java collections and handle common data formats ■■ Delve into Java’s latest I/O APIs, including asynchronous channels ■■ Use Nashorn to execute JavaScript on the Java Virtual Machine ■■ Become familiar with development tools in OpenJDK David Flanagan, senior staff frontend software engineer at Mozilla, has written several books for O’Reilly, including JavaScript: The Definitive Guide, jQuery Pocket Reference, The Ruby Programming Language, and previous editions of Java in a Nutshell Twitter: @oreillymedia facebook.com/oreilly PROGR AMMING/JAVA US $59.99 Benjamin J Evans is the cofounder and Technology Fellow of jClarity, a startup that delivers performance tools to help development & ops teams He is a Java Champion; JavaOne Rockstar; coauthor of The Well-Grounded Java Developer (Manning); and a regular public speaker on the Java platform, performance, concurrency, and related topics on Explore generics, enumerations, annotations, and lambda expressions va ■■ —Kevlin Henney consultant, author, speaker, editor of 97 Things Every Programmer Should Know Java Java in a Nutshell A DESKTOP QUICK REFERENCE CAN $62.99 ISBN: 978-1-449-37082-4 iti Learn object-oriented programming, using basic Java syntax ” Ja ■■ rs Get up to speed on language details, including Java changes references, this latest edition is still the simplest and most definitive way to cut through to the answers you need Evans & Flanagan ■■ ve The second section is a reference to core concepts and APIs that shows you how to perform real programming work in the Java environment a world of blogged “ Inopinions and javadoc’d SIXTH EDITION Java in a Nutshell The latest edition of Java in a Nutshell is designed to help experienced Java programmers get the most out of Java and 8, but it’s also a learning path for new developers Chock full of examples that demonstrate how to take complete advantage of modern Java APIs and development best practices, the first section of this thoroughly updated book provides a fast-paced, no-fluff introduction to the Java programming language and the core runtime aspects of the Java platform Ed Co Java in a Nutshell Benjamin J Evans & David Flanagan JAVA IN A NUTSHELL Sixth Edition Benjamin J Evans and David Flanagan Java in a Nutshell by Benjamin J Evans and David Flanagan Copyright © 2015 Benjamin J Evans and David Flanagan All rights reserved Printed in the United States of America Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472 O’Reilly books may be purchased for educational, business, or sales promotional use Online editions are also available for most titles (http://www.safaribooksonline.com) For more infor‐ mation, contact our corporate/institutional sales department: 800-998-9938 or corpo‐ rate@oreilly.com Editors: Mike Loukides and Meghan Blanchette Production Editor: Matthew Hacker Copyeditor: Charles Roumeliotis Proofreader: Jasmine Kwityn Indexer: Ellen Troutman Zaig Interior Designer: David Futato Cover Designer: Ellie Volckhausen Illustrator: Rebecca Demarest February 1996: First Edition May 1997: Second Edition November 1999: Third Edition March 2002: Fourth Edition March 2005: Fifth Edition October 2014: Sixth Edition Revision History for the Sixth Edition 2014-10-10: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781449370824 for release details Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trade‐ marks of O’Reilly Media, Inc Java in a Nutshell, the cover image of a Javan tiger, and related trade dress are trademarks of O’Reilly Media, Inc Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps While the publisher and the authors have used good faith efforts to ensure that the informa‐ tion and instructions contained in this work are accurate, the publisher and the authors dis‐ claim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work Use of the information and instructions contained in this work is at your own risk If any code samples or other technol‐ ogy this work contains or describes is subject to open source licenses or the intellectual prop‐ erty rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights 978-1-449-37082-4 [LSI] This book is dedicated to all who teach peace and resist violence Table of Contents Foreword xi Preface xiii Part I Introducing Java Introduction to the Java Environment The Language, the JVM, and the Ecosystem A Brief History of Java and the JVM The Lifecycle of a Java Program Java Security Comparing Java to Other Languages Answering Some Criticisms of Java 11 11 13 Java Syntax from the Ground Up 17 Java Programs from the Top Down Lexical Structure Primitive Data Types Expressions and Operators Statements Methods Introduction to Classes and Objects Arrays Reference Types Packages and the Java Namespace Java File Structure Defining and Running Java Programs Summary 18 18 22 30 46 66 72 77 84 88 93 94 95 vii Object-Oriented Programming in Java 97 Overview of Classes Fields and Methods Creating and Initializing Objects Subclasses and Inheritance Data Hiding and Encapsulation Abstract Classes and Methods Modifier Summary 97 100 106 110 121 128 132 The Java Type System 135 Interfaces Java Generics Enums and Annotations Nested Types Lambda Expressions Conclusion 136 142 151 155 171 174 Introduction to Object-Oriented Design in Java 177 Java Values Important Methods of java.lang.Object Aspects of Object-Oriented Design Exceptions and Exception Handling Safe Java Programming 177 178 183 193 195 Java’s Approach to Memory and Concurrency 197 Basic Concepts of Java Memory Management How the JVM Optimizes Garbage Collection The HotSpot Heap Finalization Java’s Support for Concurrency Working with Threads Summary Part II 197 201 203 206 208 218 219 Working with the Java Platform Programming and Documentation Conventions 223 Naming and Capitalization Conventions Practical Naming Java Documentation Comments Conventions for Portable Programs viii | Table of Contents 223 225 226 235 Working with Java Collections 239 Introduction to Collections API Lambda Expressions in the Java Collections Conclusion 239 258 266 Handling Common Data Formats 267 Text Numbers and Math Java Date and Time Conclusion 267 275 280 287 10 File Handling and I/O 289 Classic Java I/O Modern Java I/O NIO Channels and Buffers Async I/O Networking 289 295 298 301 304 11 Classloading, Reflection, and Method Handles 311 Class Files, Class Objects, and Metadata Phases of Classloading Secure Programming and Classloading Applied Classloading Reflection Dynamic Proxies Method Handles 311 313 315 317 320 325 326 12 Nashorn 331 Introduction to Nashorn Executing JavaScript with Nashorn Nashorn and javax.script Advanced Nashorn Conclusion 331 332 340 342 347 13 Platform Tools and Profiles 349 Command-Line Tools VisualVM Java Profiles Conclusion 349 362 367 372 Index 373 Table of Contents | ix primitive data types, 22-28 primitive type conversions, 28 punctuation, 22 reserved words, 20 statements, 46-65 Unicode character set, 18 Java Language Specification (JLS), java object, 342 Java platform backwards compatibility, 140 command-line tools, 349-362 graphical tool, VisualVM, 362-367 Java programming conventions for portable programs, 235 documentation comments, 226-235 naming and capitalization conven‐ tions, 223-226 Java programming environment, 3-15 Java language, JVM (Java Virtual Machine), Java programs contents of, 18 defining and running, 94 lexical structure, 18 lifecycle of, Java SE, 367 java., package names beginning with, 88 java.awt.List, 91 java.awt.peer package, 236 java.io.IOException objects, 71 java.io.ObjectInputStream class, 74 java.io.ObjectStreamConstants, 183 java.io.PrintStream, 187 java.lang package, 90 annotations in, 153 java.lang.annotation.Annotation, 153 java.lang.Cloneable, 182 (see also Cloneable interface) java.lang.Comparable (see Comparable interface) java.lang.Enum, 152 java.lang.Error, 193 java.lang.Exception, 193 java.lang.Iterable, 247 java.lang.Object class, 77, 112 (see also Object class) java.lang.reflect, 71 java.lang.Throwable, 193 386 | Index java.lang.UnsupportedOperationExcep‐ tion, 185 java.net package, 304 java.nio.channels package, 300 java.nio.file package, 295, 304 java.time package, 281 java.time.chrono package, 281 java.time.format package, 281 java.time.temporal package, 281 java.time.zone package, 281 Java.type(), 343 java.util package Map interface implementations, 251 Set implementations, 242 java.util.AbstractList, 184 java.util.Arrays class, 82 java.util.concurrent, 209, 242 Map implementations, 251 java.util.Formatter, 237 java.util.function package, 259 interfaces, 265 java.util.function.Function, 185 java.util.Iterator, 247 java.util.Iterator interface, 159 java.util.List, 91, 184 java.util.RandomAccess, 142 java.util.regex package, 272 java.util.stream package, 263 javac, 9, 350 -g switch, 351 -source and -target options, 351 and @deprecated doc-comment tag, 230 class initialization method, generation of, 109 code generated for a constructor, 108 common switches, 350 compilation and, 10 compile-time typing, 150 constructor chaining and the default constructor, 114 creation of bytecode that uses virtual method lookup, 119 field initialization code, generation of, 108 lint capability, 351 nested types, treatment of, 169 JavaClass object, 343 javadoc, 20, 355 common switches, 355 doc-comment tags recognized by, 228 inline doc-comment tags, 231 version information in its documenta‐ tion, 229 javap disassembler, 169, 361 JavaPackage object, 343 JavaScript, 331 (see also Nashorn) Java compared to, 12 javaw command, 353 javax., package names beginning with, 89 javax.net package, 304 javax.script, 340-342 key classes and interfaces, 341 with Nashorn, 340 jdeps tool, 356 jinfo tool, 359 JIT compilation, 10 JIT compilers HotSpot JVM, 353 jjs (Nashorn shell), 333 executing JavaScript, 333 jjs command and options, 334 scripting with, 335 comments, 335 inline command execution, 336 inline documents, 337 shebang syntax for starting scripts, 339 special variables, 336 string interpolation, 336 JLS ( Java Language Specification), jmap tool, 360 join() Thread class, 216 jps tool, 357 jrunscript, 333 jstack tool, 360 jstat tool, 358 jstatd tool, 358 JVisualVM (see VisualVM) JVMs (Java Virtual Machines), as interpreters, 10 defined, non-Java languages on, 331 other languages running on, 11 restrictions on, 317 runtime typing, 150 security checks implemented by, 316 K Kanjii character (in Java identifier), 21 KISS principle, 308 L labeled statements, 48 following break statement, 58 lambda expressions, 46, 67, 76, 171-174 conversion by javac to interface type, 172 defined, 76 functional programming with, 173 in Java Collections, 258-266 filters, 259 forEach, 260 maps, 260 reduce, 261 regular expressions and, 274 Streams API, 262, 266 JavaScript functions and, 344 method references, 173 last in, first-out (LIFO) queues, 252 Latin-1 character set, 19 escaping in char literals, 24 lazy evaluation, 264 left shift operator (

Ngày đăng: 16/10/2021, 15:37

Mục lục

    Changes in the Sixth Edition

    Contents of This Book

    Conventions Used in This Book

    Chapter 1. Introduction to the Java Environment

    The Language, the JVM, and the Ecosystem

    What Is the Java Language?

    What Is the JVM?

    What Is the Java Ecosystem?

    A Brief History of Java and the JVM

    The Lifecycle of a Java Program

Tài liệu cùng người dùng

Tài liệu liên quan