The Java Language Specification
Java SE 7 Edition
James Gosling
Bill Joy
Guy Steele
Gilad Bracha
Alex Buckley
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Copyright 1997, 2013, Oracle and/or its affiliates. All rights reserved.
500 Oracle Parkway, Redwood City, California 94065, U.S.A.
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 the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
This document is provided for information purposes only and the contents hereof are subject to change without notice. This document is not warranted to be error-free, nor subject to any other warranties or conditions, whether expressed orally or implied in law, including implied warranties and conditions of merchantability or fitness for a particular purpose. We specifically disclaim any liability with respect to this document and no contractual obligations are formed either directly or indirectly by this document, except as specified in the Limited License Grant herein at , and may otherwise not be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without our prior written permission.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact U.S. Corporate and Government Sales, (800) 382-3419, .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2012953694
ISBN-13: 978-0-13-326022-9
ISBN-10: 0-13-326022-4
Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
The Specification provided herein is provided to you only under the Limited License Grant included herein as .
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, January 2013.
Table of Contents
Preface to the Java SE 7 Edition
The Java SE 7 Edition of The Java Language Specification describes all the features that have been added to the Java programming language in Java SE 7. It also integrates changes made to the Java programming language under maintenance since the Third Edition in 2005.
Readers may send feedback about errors and ambiguities in The Java Language Specification to .
The majority of new features in this edition were specified by JSR 334, Small Enhancements to the Java Programming Language, led by Joe Darcy with an Expert Group of Joshua Bloch, Bruce Chapman, Alexey Kudravtsev, Mark Mahieu, Tim Peierls, and Olivier Thomann. The origins of these features lie in Project Coin, an OpenJDK project started in 2009 with the goal of Making things programmers do every day easier. The project solicited proposals from the Java community for broadly useful language features that were, in comparison with large features like generics, relatively small in their specification, implementation, and testing.
Thousands of emails and six dozen proposals later, proposals were accepted from Joshua Bloch (the try
-with-resources statement), Derek Foster/Bruce Chapman (improvements to literals), Neal Gafter (multi-catch
and precise rethrow), Bob Lee (simplified variable arity method invocation), and Jeremy Manson (improved type inference for instance creation, a.k.a. the diamond syntax). The popular strings in switch feature was also accepted. Special thanks are due to Tom Ball, Stephen Colebourne, Rmi Forax, Shams Mahmood Imam, James Lowden, and all those who submitted interesting proposals and thoughtful comments to Project Coin. Over the course of the project, there were essential contributions from Mandy Chung, Jon Gibbons, Brian Goetz, David Holmes, and Dan Smith in areas ranging from library support to language specification. Stuart Marks led a coinification effort to apply the features to the Oracle JDK codebase, both to validate their utility and to develop conventions for wider use.
The diamond syntax and precise rethrow give type inference a new visibility in the Java programming language. To a great extent, inference is worthwhile only if it produces types no less specific than those in a manifestly-typed program prior to Java SE 7. Otherwise, new code may find inference insufficient, and migration from manifest to inferred types in existing code will be risky. To mitigate the risk, Joe Darcy and Maurizio Cimadamore measured the effectiveness of different inference schemes on a large corpus of open source Java code. Such quantitative language design greatly improves confidence in the suitability and safety of the final feature. The challenge of growing a mature language with millions of developers is partially offset by the ability of language designers to learn from developers actual code.
The Java SE 7 platform adds features that cater for non-Java languages, effectively expanding the computational model of the platform. Without changes, the Java programming language would be unable to access or even express some of these features. Its static type system comes under particular stress when invoking code written in dynamically typed languages. Consequently, method invocation in the Java programming language has been modified to support method handle invocation as defined by JSR 292, Dynamically Typed Languages on the Java Platform.
The Java Compatibility Kit (JCK) team whose work helps validate this specification are due an enormous vote of thanks: Leonid Arbouzov, Alexey Gavrilov, Yulia Novozhilova, Sergey Reznick, and Victor Rudometov. Many other colleagues at Oracle (past or present) have also given valuable support to this specification: Uday Dhanikonda, Janet Koenig, Adam Messinger, Mark Reinhold, Georges Saab, Bill Shannon, and Bernard Traversat.
The following individuals have all provided many valuable comments which improved this specification: J. Stephen Adamczyk, Peter Ah, Davide Ancona, Michael Bailey, Dmitry Batrak, Joshua Bloch, Kevin Bourrillion, Richard Bosworth, Martin Bravenboer, Martin Buchholz, Didier Cruette, Glenn Colman, Neal Gafter, Jim Holmlund, Ric Holt, Philippe Mulet, Bill Pugh, Vladimir Reshetnikov, John Spicer, Robert Stroud, and Mattias Ulbrich.
This edition is the first to be written in the DocBook XML format. Metadata in the XML markup forms a kind of static type system, classifying each paragraph by its role, such as a definition or an error. The reward is much crisper conformance testing. Many thanks go to Robert Stayton for sharing his considerable DocBook expertise and for helping to render DocBook in the traditional look and feel of