The Java Language Specification

Transcription

The Java LanguageSpecificationJava SE 17 EditionJames GoslingBill JoyGuy SteeleGilad BrachaAlex BuckleyDaniel SmithGavin Bierman2021-08-09

Specification: JSR-392 Java SE 17Version: 17Status: Final ReleaseRelease: September 2021Copyright 1997, 2021, Oracle America, Inc.All rights reserved.The Specification provided herein is provided to you only under the Limited License Grantincluded herein as Appendix A. Please see Appendix A, Limited License Grant.

Table of Contents1 Introduction 11.11.21.31.41.51.61.7Organization of the Specification 2Example Programs 6Notation 6Relationship to Predefined Classes and Interfaces 7Preview Features 7Feedback 9References 102 Grammars 112.12.22.32.4Context-Free Grammars 11The Lexical Grammar 11The Syntactic Grammar 12Grammar Notation 123 Lexical Structure 173.13.23.33.43.53.63.73.83.93.103.113.12Unicode 17Lexical Translations 18Unicode Escapes 19Line Terminators 22Input Elements and Tokens 22White Space 24Comments 25Identifiers 26Keywords 28Literals 313.10.1 Integer Literals 313.10.2 Floating-Point Literals 383.10.3 Boolean Literals 413.10.4 Character Literals 423.10.5 String Literals 433.10.6 Text Blocks 453.10.7 Escape Sequences 513.10.8 The Null Literal 52Separators 52Operators 524 Types, Values, and Variables 534.1The Kinds of Types and Values 53iii

The Java Language rimitive Types and Values 544.2.1Integral Types and Values 554.2.2Integer Operations 554.2.3Floating-Point Types and Values 574.2.4Floating-Point Operations 604.2.5The boolean Type and boolean Values 62Reference Types and Values 634.3.1Objects 654.3.2The Class Object 674.3.3The Class String 684.3.4When Reference Types Are the Same 68Type Variables 69Parameterized Types 714.5.1Type Arguments of Parameterized Types 724.5.2Members and Constructors of Parameterized Types 75Type Erasure 76Reifiable Types 76Raw Types 78Intersection Types 82Subtyping 834.10.1 Subtyping among Primitive Types 834.10.2 Subtyping among Class and Interface Types 834.10.3 Subtyping among Array Types 844.10.4 Least Upper Bound 854.10.5 Type Projections 88Where Types Are Used 90Variables 954.12.1 Variables of Primitive Type 954.12.2 Variables of Reference Type 964.12.3 Kinds of Variables 984.12.4 final Variables 1004.12.5 Initial Values of Variables 1024.12.6 Types, Classes, and Interfaces 1035 Conversions and Contexts 1075.1ivKinds of Conversion 1105.1.1Identity Conversion 1105.1.2Widening Primitive Conversion 1115.1.3Narrowing Primitive Conversion 1125.1.4Widening and Narrowing Primitive Conversion 1155.1.5Widening Reference Conversion 1155.1.6Narrowing Reference Conversion 1165.1.6.1Allowed Narrowing Reference Conversion 1165.1.6.2Checked and Unchecked Narrowing ReferenceConversions 1185.1.6.3Narrowing Reference Conversions at Run Time 1195.1.7Boxing Conversion 122

The Java Language Specification5.25.35.45.55.65.1.8Unboxing Conversion 1235.1.9Unchecked Conversion 1255.1.10 Capture Conversion 1255.1.11 String Conversion 1275.1.12 Forbidden Conversions 1285.1.13 Value Set Conversion 128Assignment Contexts 129Invocation Contexts 134String Contexts 136Casting Contexts 136Numeric Contexts 1436 Names 1496.16.26.36.46.5Declarations 150Names and Identifiers 158Scope of a Declaration 1606.3.1Scope for Pattern Variables in Expressions 1646.3.1.1Conditional-And Operator && 1646.3.1.2Conditional-Or Operator 1646.3.1.3Logical Complement Operator ! 1656.3.1.4Conditional Operator ? : 1656.3.1.5Pattern Match Operator instanceof 1666.3.1.6switch Expressions 1676.3.1.7Parenthesized Expressions 1676.3.2Scope for Pattern Variables in Statements 1676.3.2.1Blocks 1686.3.2.2if Statements 1686.3.2.3while Statements 1706.3.2.4do Statements 1706.3.2.5for Statements 1706.3.2.6switch Statements 1716.3.2.7Labeled Statements 171Shadowing and Obscuring 1716.4.1Shadowing 1746.4.2Obscuring 177Determining the Meaning of a Name 1796.5.1Syntactic Classification of a Name According to Context 1806.5.2Reclassification of Contextually Ambiguous Names 1846.5.3Meaning of Module Names and Package Names 1866.5.3.1Simple Package Names 1866.5.3.2Qualified Package Names 1866.5.4Meaning of PackageOrTypeNames 1866.5.4.1Simple PackageOrTypeNames 1866.5.4.2Qualified PackageOrTypeNames 1876.5.5Meaning of Type Names 1876.5.5.1Simple Type Names 1876.5.5.2Qualified Type Names 188v

The Java Language Specification6.5.66.66.7Meaning of Expression Names 1896.5.6.1Simple Expression Names 1896.5.6.2Qualified Expression Names 1936.5.7Meaning of Method Names 1956.5.7.1Simple Method Names 195Access Control 1976.6.1Determining Accessibility 1986.6.2Details on protected Access 2026.6.2.1Access to a protected Member 2036.6.2.2Access to a protected Constructor 203Fully Qualified Names and Canonical Names 2057 Packages and Modules 2097.17.27.37.47.57.67.7Package Members 210Host Support for Modules and Packages 211Compilation Units 214Package Declarations 2167.4.1Named Packages 2167.4.2Unnamed Packages 2177.4.3Package Observability and Visibility 217Import Declarations 2187.5.1Single-Type-Import Declarations 2197.5.2Type-Import-on-Demand Declarations 2217.5.3Single-Static-Import Declarations 2227.5.4Static-Import-on-Demand Declarations 223Top Level Class and Interface Declarations 224Module Declarations 2277.7.1Dependences 2307.7.2Exported and Opened Packages 2327.7.3Service Consumption 2337.7.4Service Provision 2347.7.5Unnamed Modules 2357.7.6Observability of a Module 2358 Classes 2378.1viClass Declarations 2398.1.1Class Modifiers 2398.1.1.1abstract Classes 2408.1.1.2sealed, non-sealed, and final Classes 2428.1.1.3strictfp Classes 2438.1.1.4static Classes 2438.1.2Generic Classes and Type Parameters 2448.1.3Inner Classes and Enclosing Instances 2468.1.4Superclasses and Subclasses 2518.1.5Superinterfaces 2538.1.6Permitted Direct Subclasses 2568.1.7Class Body and Member Declarations 258

The Java Language Specification8.28.38.48.58.68.78.88.9Class Members 259Field Declarations 2638.3.1Field Modifiers 2688.3.1.1static Fields 2688.3.1.2final Fields 2728.3.1.3transient Fields 2728.3.1.4volatile Fields 2728.3.2Field Initialization 2748.3.3Restrictions on Field References in Initializers 275Method Declarations 2788.4.1Formal Parameters 2808.4.2Method Signature 2838.4.3Method Modifiers 2848.4.3.1abstract Methods 2858.4.3.2static Methods 2868.4.3.3final Methods 2878.4.3.4native Methods 2888.4.3.5strictfp Methods 2888.4.3.6synchronized Methods 2888.4.4Generic Methods 2908.4.5Method Result 2918.4.6Method Throws 2918.4.7Method Body 2938.4.8Inheritance, Overriding, and Hiding 2948.4.8.1Overriding (by Instance Methods) 2958.4.8.2Hiding (by Class Methods) 2988.4.8.3Requirements in Overriding and Hiding 2998.4.8.4Inheriting Methods with Override-EquivalentSignatures 3048.4.9Overloading 304Member Class and Interface Declarations 308Instance Initializers 309Static Initializers 309Constructor Declarations 3108.8.1Formal Parameters 3118.8.2Constructor Signature 3128.8.3Constructor Modifiers 3128.8.4Generic Constructors 3138.8.5Constructor Throws 3138.8.6The Type of a Constructor 3148.8.7Constructor Body 3148.8.7.1Explicit Constructor Invocations 3158.8.8Constructor Overloading 3198.8.9Default Constructor 3198.8.10 Preventing Instantiation of a Class 321Enum Classes 3228.9.1Enum Constants 3238.9.2Enum Body Declarations 324vii

The Java Language Specification8.108.9.3Enum Members 326Record Classes 3308.10.1 Record Components 3318.10.2 Record Body Declarations 3338.10.3 Record Members 3348.10.4 Record Constructor Declarations 3388.10.4.1 Normal Canonical Constructors 3398.10.4.2 Compact Canonical Constructors 3409 Interfaces 3439.19.29.39.49.59.69.7viiiInterface Declarations 3449.1.1Interface Modifiers 3449.1.1.1abstract Interfaces 3459.1.1.2strictfp Interfaces 3459.1.1.3static Interfaces 3459.1.1.4sealed and non-sealed Interfaces 3469.1.2Generic Interfaces and Type Parameters 3469.1.3Superinterfaces and Subinterfaces 3479.1.4Permitted Direct Subclasses and Subinterfaces 3499.1.5Interface Body and Member Declarations 350Interface Members 350Field (Constant) Declarations 3519.3.1Initialization of Fields in Interfaces 354Method Declarations 3549.4.1Inheritance and Overriding 3569.4.1.1Overriding (by Instance Methods) 3579.4.1.2Requirements in Overriding 3589.4.1.3Inheriting Methods with Override-EquivalentSignatures 3599.4.2Overloading 3609.4.3Interface Method Body 360Member Class and Interface Declarations 361Annotation Interfaces 3629.6.1Annotation Interface Elements 3649.6.2Defaults for Annotation Interface Elements 3679.6.3Repeatable Annotation Interfaces 3689.6.4Predefined Annotation Interfaces 3739.6.4.1@Target 3739.6.4.2@Retention 3749.6.4.3@Inherited 3759.6.4.4@Override 3769.6.4.5@SuppressWarnings 3779.6.4.6@Deprecated 3789.6.4.7@SafeVarargs 3809.6.4.8@Repeatable 3819.6.4.9@FunctionalInterface 381Annotations 381

The Java Language Specification9.89.99.7.1Normal Annotations 3829.7.2Marker Annotations 3859.7.3Single-Element Annotations 3859.7.4Where Annotations May Appear 3869.7.5Multiple Annotations of the Same Interface 391Functional Interfaces 392Function Types 39610 Arrays 40110.110.210.310.410.510.610.710.810.9Array Types 402Array Variables 402Array Creation 405Array Access 405Array Store Exception 406Array Initializers 407Array Members 409Class Objects for Arrays 410An Array of Characters Is Not a String 41211 Exceptions 41311.111.211.3The Kinds and Causes of Exceptions 41411.1.1 The Kinds of Exceptions 41411.1.2 The Causes of Exceptions 41511.1.3 Asynchronous Exceptions 416Compile-Time Checking of Exceptions 41711.2.1 Exception Analysis of Expressions 41811.2.2 Exception Analysis of Statements 41911.2.3 Exception Checking 420Run-Time Handling of an Exception 42212 Execution 42712.112.212.312.4Java Virtual Machine Startup 42712.1.1 Load the Class Test 42812.1.2 Link Test: Verify, Prepare, (Optionally) Resolve 42812.1.3 Initialize Test: Execute Initializers 42912.1.4 Invoke Test.main 430Loading of Classes and Interfaces 43012.2.1 The Loading Process 43112.2.2 Class Loader Consistency 431Linking of Classes and Interfaces 43212.3.1 Verification of the Binary Representation 43212.3.2 Preparation of a Class or Interface 43312.3.3 Resolution of Symbolic References 433Initialization of Classes and Interfaces 43412.4.1 When Initialization Occurs 43512.4.2 Detailed Initialization Procedure 437ix

The Java Language Specification12.512.612.712.8Creation of New Class Instances 440Finalization of Class Instances 44312.6.1 Implementing Finalization 44512.6.2 Interaction with the Memory Model 446Unloading of Classes and Interfaces 448Program Exit 44913 Binary Compatibility 45113.113.213.313.413.5xThe Form of a Binary 452What Binary Compatibility Is and Is Not 459Evolution of Packages and Modules 460Evolution of Classes 46113.4.1 abstract Classes 46113.4.2 sealed, non-sealed, and final Classes 46113.4.2.1 sealed Classes 46113.4.2.2 non-sealed Classes 46213.4.2.3 final Classes 46213.4.3 public Classes 46213.4.4 Superclasses and Superinterfaces 46213.4.5 Class Type Parameters 46413.4.6 Class Body and Member Declarations 46413.4.7 Access to Members and Constructors 46613.4.8 Field Declarations 46713.4.9 final Fields and static Constant Variables 46913.4.10 static Fields 47113.4.11 transient Fields 47113.4.12 Method and Constructor Declarations 47113.4.13 Method and Constructor Type Parameters 47213.4.14 Method and Constructor Formal Parameters 47313.4.15 Method Result Type 47313.4.16 abstract Methods 47313.4.17 final Methods 47413.4.18 native Methods 47513.4.19 static Methods 47513.4.20 synchronized Methods 47513.4.21 Method and Constructor Throws 47513.4.22 Method and Constructor Body 47613.4.23 Method and Constructor Overloading 47613.4.24 Method Overriding 47713.4.25 Static Initializers 47713.4.26 Evolution of Enum Classes 47713.4.27 Evolution of Record Classes 478Evolution of Interfaces 47813.5.1 public Interfaces 47813.5.2 sealed and non-sealed Interfaces 47813.5.3 Superinterfaces 47913.5.4 Interface Members 479

The Java Language Specification13.5.513.5.613.5.713.5.8Interface Type Parameters 480Field Declarations 480Interface Method Declarations 480Annotation Interfaces 48114 Blocks, Statements, and Patterns 4.22Normal and Abrupt Completion of Statements 484Blocks 485Local Class and Interface Declarations 486Local Variable Declarations 48814.4.1 Local Variable Declarators and Types 48914.4.2 Local Variable Declaration Statements 491Statements 491The Empty Statement 493Labeled Statements 493Expression Statements 495The if Statement 49614.9.1 The if-then Statement 49614.9.2 The if-then-else Statement 497The assert Statement 497The switch Statement 50014.11.1 Switch Blocks 50014.11.2 The Switch Block of a switch Statement 50314.11.3 Execution of a switch Statement 503The while Statement 50614.12.1 Abrupt Completion of while Statement 507The do Statement 50714.13.1 Abrupt Completion of do Statement 508The for Statement 50914.14.1 The basic for Statement 51014.14.1.1 Initialization of for Statement 51014.14.1.2 Iteration of for Statement 51114.14.1.3 Abrupt Completion of for Statement 51214.14.2 The enhanced for statement 513The break Statement 516The continue Statement 518The return Statement 521The throw Statement 522The synchronized Statement 524The try statement 52514.20.1 Execution of try-catch 52914.20.2 Execution of try-finally and try-catch-finally 53014.20.3 try-with-resources 53214.20.3.1 Basic try-with-resources 53514.20.3.2 Extended try-with-resources 538The yield Statement 538Unreachable Statements 540xi

The Java Language Specification14.30 Patterns 54614.30.1 Kinds of Patterns 54714.30.2 Pattern Matching 54815 Expressions 54915.115.215.315.415.515.615.7Evaluation, Denotation, and Result 549Forms of Expressions 550Type of an Expression 551Floating-point Expressions 552Expressions and Run-Time Checks 556Normal and Abrupt Completion of Evaluation 558Evaluation Order 56015.7.1 Evaluate Left-Hand Operand First 56015.7.2 Evaluate Operands before Operation 56215.7.3 Evaluation Respects Parentheses and Precedence 56215.7.4 Argument Lists are Evaluated Left-to-Right 56315.7.5 Evaluation Order for Other Expressions 56515.8 Primary Expressions 56515.8.1 Lexical Literals 56615.8.2 Class Literals 56715.8.3 this 56815.8.4 Qualified this 56915.8.5 Parenthesized Expressions 56915.9 Class Instance Creation Expressions 57015.9.1 Determining the Class being Instantiated 57215.9.2 Determining Enclosing Instances 57315.9.3 Choosing the Constructor and its Arguments 57615.9.4 Run-Time Evaluation of Class Instance CreationExpressions 58115.9.5 Anonymous Class Declarations 58215.9.5.1 Anonymous Constructors 58315.10 Array Creation and Access Expressions 58415.10.1 Array Creation Expressions 58415.10.2 Run-Time Evaluation of Array Creation Expressions 58615.10.3 Array Access Expressions 58915.10.4 Run-Time Evaluation of Array Access Expressions 58915.11 Field Access Expressions 59215.11.1 Field Access Using a Primary 59215.11.2 Accessing Superclass Members using super 59515.12 Method Invocation Expressions 59715.12.1 Compile-Time Step 1: Determine Type to Search 59815.12.2 Compile-Time Step 2: Determine Method Signature 60115.12.2.1 Identify Potentially Applicable Methods 60715.12.2.2 Phase 1: Identify Matching Arity Methods Applicableby Strict Invocation 60915.12.2.3 Phase 2: Identify Matching Arity Methods Applicableby Loose Invocation 611xii

The Java Language .2015.2115.2215.2315.2415.2515.12.2.4 Phase 3: Identify Methods Applicable by Variable ArityInvocation 61115.12.2.5 Choosing the Most Specific Method 61215.12.2.6 Method Invocation Type 61615.12.3 Compile-Time Step 3: Is the Chosen Method Appropriate? 61715.12.4 Run-Time Evaluation of Method Invocation 62015.12.4.1 Compute Target Reference (If Necessary) 62015.12.4.2 Evaluate Arguments 62215.12.4.3 Check Accessibility of Type and Method 62315.12.4.4 Locate Method to Invoke 62415.12.4.5 Create Frame, Synchronize, Transfer Control 629Method Reference Expressions 63115.13.1 Compile-Time Declaration of a Method Reference 63415.13.2 Type of a Method Reference 64015.13.3 Run-Time Evaluation of Method References 641Postfix Expressions 64415.14.1 Expression Names 64515.14.2 Postfix Increment Operator 64515.14.3 Postfix Decrement Operator -- 646Unary Operators 64615.15.1 Prefix Increment Operator 64815.15.2 Prefix Decrement Operator -- 64815.15.3 Unary Plus Operator 64915.15.4 Unary Minus Operator - 64915.15.5 Bitwise Complement Operator 65015.15.6 Logical Complement Operator ! 650Cast Expressions 651Multiplicative Operators 65215.17.1 Multiplication Operator * 65315.17.2 Division Operator / 65415.17.3 Remainder Operator % 655Additive Operators 65715.18.1 String Concatenation Operator 65815.18.2 Additive Operators ( and -) for Numeric Types 660Shift Operators 662Relational Operators 66315.20.1 Numerical Comparison Operators , , , and 66315.20.2 The instanceof Operator 665Equality Operators 66715.21.1 Numerical Equality Operators and ! 66815.21.2 Boolean Equality Operators and ! 66915.21.3 Reference Equality Operators and ! 669Bitwise and Logical Operators 67015.22.1 Integer Bitwise Operators &, , and 67015.22.2 Boolean Logical Operators &, , and 671Conditional-And Operator && 672Conditional-Or Operator 672Conditional Operator ? : 673xiii

The Java Language Specification15.2615.2715.2815.2915.25.1 Boolean Conditional Expressions 68115.25.2 Numeric Conditional Expressions 68115.25.3 Reference Conditional Expressions 682Assignment Operators 68315.26.1 Simple Assignment Operator 68415.26.2 Compound Assignment Operators 690Lambda Expressions 69615.27.1 Lambda Parameters 69815.27.2 Lambda Body 70215.27.3 Type of a Lambda Expression 70515.27.4 Run-Time Evaluation of Lambda Expressions 707switch Expressions 70815.28.1 The Switch Block of a switch Expression 70915.28.2 Run-Time Evaluation of switch Expressions 710Constant Expressions 71216 Definite Assignment 71516.116.216.3xivDefinite Assignment and Expressions 72116.1.1 Boolean Constant Expressions 72116.1.2 Conditional-And Operator && 72116.1.3 Conditional-Or Operator 72216.1.4 Logical Complement Operator ! 72216.1.5 Conditional Operator ? : 72216.1.6 switch Expressions 72316.1.7 Other Expressions of Type boolean 72616.1.8 Assignment Expressions 72616.1.9 Operators and -- 72616.1.10 Other Expressions 727Definite Assignment and Statements 72816.2.1 Empty Statements 72816.2.2 Blocks 72816.2.3 Local Class and Interface Declarations 73016.2.4 Local Variable Declaration Statements 73016.2.5 Labeled Statements 73016.2.6 Expression Statements 73116.2.7 if Statements 73116.2.8 assert Statements 73116.2.9 switch Statements 73216.2.10 while Statements 73216.2.11 do Statements 73316.2.12 for Statements 73316.2.12.1 Initialization Part of for Statement 73416.2.12.2 Incrementation Part of for Statement 73516.2.13 break, yield, continue, return, and throw Statements 73516.2.14 synchronized Statements 73516.2.15 try Statements 735Definite Assignment and Parameters 737

The Java Language Specification16.416.516.616.716.816.9Definite Assignment and Array Initializers 737Definite Assignment and Enum Constants 738Definite Assignment and Anonymous Classes 738Definite Assignment and Member Classes and Interfaces 739Definite Assignment and Static Initializers 739Definite Assignment, Constructors, and Instance Initializers 74017 Threads and Locks 74317.117.217.317.417.517.617.7Synchronization 744Wait Sets and Notification 74417.2.1 Wait 74517.2.2 Notification 74717.2.3 Interruptions 74717.2.4 Interactions of Waits, Notification, and Interruption 748Sleep and Yield 748Memory Model 74917.4.1 Shared Variables 75217.4.2 Actions 75217.4.3 Programs and Program Order 75317.4.4 Synchronization Order 75417.4.5 Happens-before Order 75517.4.6 Executions 75817.4.7 Well-Formed Executions 75917.4.8 Executions and Causality Requirements 75917.4.9 Observable Behavior and Nonterminating Executions 762final Field Semantics 76417.5.1 Semantics of final Fields 76617.5.2 Reading final Fields During Construction 76717.5.3 Subsequent Modification of final Fields 76717.5.4 Write-Protected Fields 768Word Tearing 769Non-Atomic Treatment of double and long 77018 Type Inference 77118.118.218.3Concepts and Notation 77218.1.1 Inference Variables 77218.1.2 Constraint Formulas 77318.1.3 Bounds 773Reduction 77518.2.1 Expression Compatibility Constraints 77518.2.2 Type Compatibility Constraints 78018.2.3 Subtyping Constraints 78118.2.4 Type Equality Constraints 78318.2.5 Checked Exception Constraints 784Incorporation 78618.3.1 Complementary Pairs of Bounds 78718.3.2 Bounds Involving Capture Conversion 788xv

The Java Language Specification18.418.5Resolution 788Uses of Inference 79118.5.1 Invocation Applicability Inference 79118.5.2 Invocation Type Inference 79318.5.2.1 Poly Method Invocation Compatibility 79318.5.2.2 Additional Argument Constraints 79518.5.3 Functional Interface Parameterization Inference 80018.5.4 More Specific Method Inference 80119 Syntax 805A Limited License Grant 833xvi

C H A P T E R1IntroductionTHE Javaprogramming language is a general-purpose, concurrent, classbased, object-oriented language. It is designed to be simple enough that manyprogrammers can achieve fluency in the language. The Java programming languageis related to C and C but is organized rather differently, with a number of aspectsof C and C omitted and a few ideas from other languages included. It is intendedto be a production language, not a research language, and so, as C. A. R. Hoaresuggested in his classic paper on language design, the design has avoided includingnew and untested features. The Java programming language is strongly and statically typed. This specificationclearly distinguishes between the compile-time errors that can and must be detectedat compile time, and those that occur at run time. Compile time normally consistsof translating programs into a machine-independent byte code representation.Run-time activities include loading and linking of the classes needed to executea program, optional machine code generation and dynamic optimization of theprogram, and actual program execution.The Java programming language is a relatively high-level language, in that detailsof the machine representation are not available through the language. It includesautomatic storage management, typically using a garbage collector, to avoidthe safety problems of explicit deallocation (as in C's free or C 's delete).High-performance garbage-collected implementations can have bounded pauses tosupport systems programming and real-time applications. The language does notinclude any unsafe constructs, such as array accesses without index checking, sincesuch unsafe constructs would cause a program to behave in an unspecified way.The Java programming language is normally compiled to the bytecode instructionset and binary format defined in The Java Virtual Machine Specification, Java SE17 Edition.1

1.1Organization of the SpecificationINTRODUCTION1.1 Organization of the SpecificationChapter 2 describes grammars and the notation used to present the lexical andsyntactic grammars for the language.Chapter 3 describes the lexical structure of the Java programming language, whichis based on C and C . The language is written in the Unicode character set. Itsupports the writing of Unicode characters on systems that support only ASCII.Chapter 4 describes types, values, and variables. Types are subdivided intoprimitive types and reference types.The primitive types are defined to be the same on all machines and in allimplementations, and are various sizes of two's-complement integers, IEEE 754floating-point numbers, a boolean type, and a Unicode character char type. Valuesof the primitive types do not share state.Reference types are the class types, the interface types, and the array types. Thereference types are implemented by dynamically created objects that are eitherinstances of classes or arrays. Many references to each object can exist. All objects(including arrays) support the methods of the class Object, which is the (single)root of the class hierarchy. A predefined String class supports Unicode characterstrings. Classes exist for wrapping primitive values inside of objects. In many cases,wrapping and unwrapping is performed automatically by the compiler (in whichcase, wrapping is called boxing, and unwrapping is called unboxing). Classesand interfaces may be generic, that is, they may be parameterized by referencetypes. Parameterized types of such classes and interfaces may then be invoked withspecific type arguments.Variables are typed storage locations. A variable of a primitive type holds a valueof that exact primitive type. A variable of a class type can hold a null reference ora reference to an object that is an instance of the named class or any subclass ofthat class. A variable of an interface type can hold a null reference or a referenceto an instance of any class that implements the named interface. A variable of anarray type can hold a null reference or a reference to an array. A variable of classtype Object can hold a null reference or a reference to any object, whether classinstance or array.Chapter 5 describes conversions and numeric promotions. Conversions change thecompile-time type and, sometimes, the value of an expression. These conversionsinclude the boxing and unboxing conversions between primitive types andreference types. Numeric promotions are used to convert the operands of a numericoperator to a common type where an operation can be performed. There are no2

INTRODUCTIONOrganization of the Specification1.1loopholes in the language; casts on reference types are checked at run time to ensuretype safety.Chapter 6 describes declarations and names, and how to determine what namesmean (that is, which declaration a name denotes). The Java programming languagedoes not require classes and interfaces, or their members, to be declared before theyare used. Declaration order is significant only for local variables, local classes, localinterfaces, and the order of field initializers in a class or interface. Recommendednaming conventions that make for more readable programs are described here.Chapter 7 describes the structure of a program, which is organized into packages.The members of a package are classes, interfaces, and subpackages. Packages,and consequently their members, have names in a hierarchical name space; theInternet domain name system can usually be used to form unique package names.Compilation units contain declarations of the classes and interfaces that aremembers of a given package, and may import classes and interfaces from otherpackages to give them short names.Packages may be grouped into modules that serve as building blocks in theconstruction of very large programs. The declaration of a module specifies whichother modules (and thus packages, and thus classes and interfaces) are required inorder to compile and run code in its own packages.The Java programming language supports limitations on external access to themembers of packages, classes, and interfaces. The members of a package may beaccessible solely by other members in the same package, or by members in otherpackages of the same module, or by members of packages in different modules.Similar constraints apply to the members of classes and interfaces.Chapter 8 describes classes. The members of classes are classes, interfaces, fields(variables) and methods. Class variables exist once per class. Class methods operatewithout reference to a specific object. Instance variables are dynamically createdin objects that are instances of classes. Instance methods are invoked on instancesof classes; such instances become the current object this during their execution,supporting the object-oriented programming style.Classes support single inheritance, in which each class has a single superclass.Each class inherits members from its superclass, and ultimately from the classObject. Variables of a class type can reference an instance of the named class orany subclass of that class, allowing new classes to be used with existing methods,polymorphically.Classes support concurrent programming with synchronized methods. Methodsdeclare the checked exceptions that can arise from their execution, which allows3

1.1Organization of the SpecificationINTRODUCTIONcompile-time checking to ensure that exceptional conditions are handled. Objectscan declare a finalize method that will be invoked before the objects are discardedby the garbage collector, allowing the objects to clean up their state.For simplicity, the language has neither declaration "headers" separate from theimplementation of a class nor separate type and class hierarchies.A restricted kind of class, enum classes, supports the definition of small sets ofvalues and their manipulation in a type safe manner. Unlike enumerations in otherlanguages, enum constants are objects and may have their own methods.Another restricted kind of class, record classes, supports the compact expressionof simple objects that serve as aggregates of values.Chapter 9 describes interfaces. The members of interfaces are classes, interfaces,constant fields, and methods. Classes that are otherwise unrelated can implementthe same interface. A variable of an interface type can contain a reference to anyobject that implements the interface.Classes and interfaces support multiple inheritance from interfaces. A class thatimplements one or more interfaces may inherit instance methods from both itssuperclass and its superinterfaces.Annotations are metadata that may be applied to declarations in a program, as wellas to the uses of types in declarations and expressions. The form of an annotationis defined by an annota

Aug 09, 2021 · The Java Language Specification 8.2 Class Members 259 8.3 Field Declarations 263 8.3.1 Field Modifiers 268 8.3.1.1 static Fields 268 8.3.1.2 final Fields 272 8.3.1.3 transient Fields 272 8.3.1.4 volatile Fields 272 8.3.2 Field Initialization 274 8