### Example JVM Startup Command Line Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-12.html Illustrates how to start a Java Virtual Machine from a command line, specifying the main class and its arguments. ```shell java Test reboot Bob Dot Enzo ``` -------------------------------- ### Unique Module Names Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-6.html Demonstrates the convention for naming modules, which should correspond to their principal exported package. If no such package exists, the name should still start with the reversed form of an associated internet domain. ```java com.nighthacks.scrabble org.openjdk.compiler net.jcip.annotations ``` -------------------------------- ### Method Descriptor Example Source: https://docs.oracle.com/javase/specs/jvms/se26/html/jvms-4.html This example shows the method descriptor for a method that takes an int, a double, and a Thread object, returning an Object. ```text (I D Ljava/lang/Thread; ) Ljava/lang/Object; ``` -------------------------------- ### JVM Assembly Instruction Example with Comment Source: https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-3.html A concrete example of a JVM assembly instruction, demonstrating the push of an integer constant. ```assembly 8 _bipush 100_ // Push int constant 100 ``` -------------------------------- ### Java Conversions in Various Contexts Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-5.html Demonstrates casting, string, assignment, numeric promotion, and invocation conversions in Java. This example illustrates how different conversion contexts affect type handling and implicit conversions. ```Java class Test { public static void main(String[] args) { // Casting conversion (5.5) of a float literal to // type int. Without the cast operator, this would // be a compile-time error, because this is a // narrowing conversion (5.1.3): int i = (int)12.5f; // String conversion (5.4) of i's int value: System.out.println("(int)12.5f==" + i); // Assignment conversion (5.2) of i's value to type // float. This is a widening conversion (5.1.2): float f = i; // String conversion of f's float value: System.out.println("after float widening: " + f); // Numeric promotion (5.6) of i's value to type // float. This is a binary numeric promotion. // After promotion, the operation is float*float: System.out.print(f); f = f * i; // Two string conversions of i and f: System.out.println("*" + i + "==" + f); // Invocation conversion (5.3) of f's value // to type double, needed because the method Math.sin // accepts only a double argument: double d = Math.sin(f); // Two string conversions of f and d: System.out.println("Math.sin(" + f + ")==" + d); } } ``` -------------------------------- ### Simple Compilation Unit Example Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html An example of a simple compilation unit featuring an import statement, a static main method, and a class declaration. ```java import p.*; static void main(){ ... } class Test { ... } ``` -------------------------------- ### Example Command Line Invocation Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html Demonstrates how a command line argument is used to specify the initial class for JVM startup. ```bash java Test reboot Bob Dot Enzo ``` -------------------------------- ### Descriptive Class Names in Java Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-6.html Class names should be descriptive nouns or noun phrases, capitalized at the start of each word. Examples include ClassLoader, ProcessBuilder, Thread, Dictionary, and BufferedInputStream. ```java ClassLoader ProcessBuilder Thread Dictionary BufferedInputStream ``` -------------------------------- ### Example Command Line for Compact Compilation Unit Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-12.html Illustrates the command line used to run a program defined by a compact compilation unit. ```shell java HelloWorld ``` -------------------------------- ### Simple Functional Interface Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-9.html A basic example of an interface that meets the criteria for a functional interface. ```java interface Runnable { void run(); } ``` -------------------------------- ### Example of Class Instance Creation and Initialization Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-12.html Demonstrates the creation of a `ColoredPoint` object, which involves initializing its superclass fields and its own fields. The `main` method then accesses an instance variable of the created object. ```java class Point { int x, y; Point() { x = 1; y = 1; } } class ColoredPoint extends Point { int color = 0xFF00FF; } class Test { public static void main(String[] args) { ColoredPoint cp = new ColoredPoint(); System.out.println(cp.color); } } ``` -------------------------------- ### Simple Compilation Unit with Instance Main Method Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html Illustrates a simple compilation unit with an instance main method that prints 'Hello, World!'. This can be invoked directly if the file name matches the class name. ```java void main() { System.out.println("Hello, World!"); } ``` -------------------------------- ### Examples of int Literals Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-3.html Provides various examples of int literals in decimal, octal, and hexadecimal formats. ```java 0 ``` ```java 2 ``` ```java 0372 ``` ```java 0xDada_Cafe ``` ```java 1996 ``` ```java 0x00_FF__00_FF ``` -------------------------------- ### Ambiguous Lambda Syntax Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-15.html An example illustrating a parsing challenge where lambda syntax can be ambiguous with generic types. ```Java foo( (x) < y , z > (w) -> v ) ``` -------------------------------- ### Basic try-with-resources Syntax Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-14.html Illustrates the basic syntax of a try-with-resources statement for managing a single resource. ```Java try (_VariableModifier_ R VariableDeclaratorId_ = _Expression_ ...) _Block_ ``` -------------------------------- ### Project Directory Structure Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-7.html Illustrates a simple file system organization for storing packages and source/binary code within a project's root directory and its subdirectories. ```plaintext com gls jag java wnj ``` -------------------------------- ### Example of Module Declaration with Requires Clause Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-7.html This example shows a module declaration that requires another module, 'M1'. ```Java module M0 { requires M1; } ``` -------------------------------- ### Ordinary Compilation Unit Example Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html An example of an ordinary compilation unit which includes an import statement and a class declaration. ```java import p.*; class Test { ... } ``` -------------------------------- ### Example Instruction Format Source: https://docs.oracle.com/javase/specs/jvms/se26/html/jvms-6.html Illustrates the general format of a JVM instruction description, including mnemonic, operands, and stack transformation. ```text _mnemonic_ _operand1_ _operand2_ ... ``` -------------------------------- ### Package Name to Pathname Transformation Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-7.html Demonstrates how a package name is transformed into a directory path using a file name separator. ```plaintext jag.scrabble.board ``` -------------------------------- ### Example Command Line for Simple Compilation Unit Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html Shows the command line used to execute a simple compilation unit where the file name matches the implicitly declared class name. ```bash java HelloWorld ``` -------------------------------- ### OutOfMemoryError Output Example Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-15.html The expected output when the OutOfMemoryError example program is run. It confirms that the error is detected after the dimension expression evaluation. ```text java.lang.OutOfMemoryError, true ``` -------------------------------- ### Hello World in Simple Compilation Unit Source: https://docs.oracle.com/javase/specs/jls/se24/preview/specs/simple-source-files-instance-main-methods-jls.html A 'Hello World' program demonstrating a simple compilation unit with implicit console I/O imports. ```java void main() { println("Hello, World!"); } ``` -------------------------------- ### Conditional Compilation Example - Initial State Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-14.html This example shows the initial state where DEBUG is true and the corresponding output when compiled and executed. ```Java class Flags { static final boolean DEBUG = true; } class Test { public static void main(String[] args) { if (Flags.DEBUG) System.out.println("DEBUG is true"); } } ``` -------------------------------- ### Package Files in 'util' Directory Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-7.html Displays example source (.java) and compiled (.class) files within the 'util' directory, representing compilation units. ```text BitSet.java Observable.java BitSet.class Observable.class Date.java Observer.java Date.class Observer.class ... ``` -------------------------------- ### Java Library Code Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-6.html Defines a package and an interface with a static field. This code serves as context for name reclassification examples. ```java package org.rpgpoet; import java.util.Random; public interface Music { Random[] wizards = new Random[4]; } ``` -------------------------------- ### Default Initialization of Variables Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-4.html Demonstrates the default initial values for static variables, instance variables, and reference types when a class is prepared and instances are created. ```Java class Point { static int npoints; int x, y; Point root; } class Test { public static void main(String[] args) { System.out.println("npoints=" + Point.npoints); Point p = new Point(); System.out.println("p.x=" + p.x + ", p.y=" + p.y); System.out.println("p.root=" + p.root); } } ``` -------------------------------- ### Set and get an instance variable Source: https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-3.html Illustrates the JVM bytecode for setting and getting an integer instance variable 'i' using 'putfield' and 'getfield'. ```Java void setIt(int value) { i = value; } int getIt() { return i; } ``` ```bytecode Method void setIt(int) 0 _aload_0_ 1 _iload_1_ 2 _putfield #4_ // Field Example.i I 5 _return_ Method int getIt() 0 _aload_0_ 1 _getfield #4_ // Field Example.i I 4 _ireturn_ ``` -------------------------------- ### Array Creation and Access Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-10.html Demonstrates creating an integer array, populating it with values, and then summing its elements. The array length is accessed using the 'length' field. ```Java class Gauss { public static void main(String[] args) { int[] ia = new int[101]; for (int i = 0; i < ia.length; i++) ia[i] = i; int sum = 0; for (int e : ia) sum += e; System.out.println(sum); } } ``` -------------------------------- ### Ordinary Compilation Unit Example Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-7.html An example of an ordinary compilation unit, which includes a package declaration, import declarations, and top-level class declarations. ```java import java.util.Vector; class Test { ... } ``` -------------------------------- ### Method Applicability Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-15.html Demonstrates method applicability and accessibility rules in Java. Shows how the compiler selects the correct method based on inheritance and access modifiers. ```Java class Doubler { static int two() { return two(1); } private static int two(int i) { return 2*i; } } class Test extends Doubler { static long two(long j) { return j+j; } public static void main(String[] args) { System.out.println(two(3)); System.out.println(Doubler.two(3)); // compile-time error } } ``` -------------------------------- ### Basic Text Block Examples Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-3.html Demonstrates various text blocks, including simple strings, strings with line terminators, quoted strings, and empty strings. ```Java String season = """ winter"""; ``` ```Java String period = """ winter """; ``` ```Java String greeting = """ Hi, \"Bob\" """; ``` ```Java String salutation = """ Hi, \"Bob\" """; ``` ```Java String empty = """ """; ``` ```Java String quote = """ "\"" """; ``` ```Java String backslash = """ \\"""; ``` -------------------------------- ### Decimal Floating-Point Literals Examples (float) Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-3.html Examples of various decimal floating-point literals for the 'float' type. These literals are suffixed with 'f' or 'F'. ```java 1e1f 2.f .3f 0f 3.14f 6.022137e+23f ``` -------------------------------- ### Create a new Object instance Source: https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-3.html Demonstrates the JVM bytecode for creating a new instance of java.lang.Object and returning it. ```Java Object create() { return new Object(); } ``` ```bytecode Method java.lang.Object create() 0 _new #1_ // Class java.lang.Object 3 _dup_ 4 _invokespecial #4_ // Method java.lang.Object.()V 7 _areturn_ ``` -------------------------------- ### Compact Compilation Unit Example Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-7.html An example of a compact compilation unit, which includes import declarations and method declarations, implicitly declaring a top-level class. ```java import java.util.Vector; static void main(){ ... } class Test { ... } ``` -------------------------------- ### Decimal Floating-Point Literals Examples (double) Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-3.html Examples of various decimal floating-point literals for the 'double' type. These literals can optionally be suffixed with 'd' or 'D'. ```java 1e1 2. .3 0.0 3.14 1e-9d 1e137 ``` -------------------------------- ### Java Program Output Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-1.html The expected output when running the 'Test' Java program with the arguments 'Hello, world.'. ```text Hello, world. ``` -------------------------------- ### Java Object and Array Creation Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-4.html Demonstrates explicit and implicit creation of class instances and arrays, including String concatenation. ```Java class Point { int x, y; Point() { System.out.println("default"); } Point(int x, int y) { this.x = x; this.y = y; } /* A Point instance is explicitly created at class initialization time: */ static Point origin = new Point(0,0); /* A String can be implicitly created by a + operator: */ public String toString() { return "(" + x + "," + y + ")"; } } class Test { public static void main(String[] args) { /* A Point is explicitly created using newInstance: */ Point p = null; try { p = (Point)Class.forName("Point").newInstance(); } catch (Exception e) { System.out.println(e); } /* An array is implicitly created by an array initializer: */ Point[] a = { new Point(0,0), new Point(1,1) }; /* Strings are implicitly created by + operators: */ System.out.println("p: " + p); System.out.println("a: { " + a[0] + ", " + a[1] + " }"); /* An array is explicitly created by an array creation expression: */ String[] sa = new String[2]; sa[0] = "he"; sa[1] = "llo"; System.out.println(sa[0] + sa[1]); } } ``` -------------------------------- ### Type Safety Violation Example Source: https://docs.oracle.com/javase/specs/jls/se26/html/jls-5.html An example demonstrating how a direct assignment of List to List would undermine the Java type system, leading to potential ClassCastExceptions. ```Java List ls = new ArrayList(); List l = ls; Collections.fill(l, new Object()); // not legal - but assume it was! String s = ls.get(0); // ClassCastException - ls contains // Objects, not Strings. ``` -------------------------------- ### Type of a Variable versus Class of an Object Example Source: https://docs.oracle.com/javase/specs/jls/se24/html/jls-4.html Demonstrates how variables can hold references to objects of different, but compatible, types. This example illustrates polymorphism and interface implementation. ```Java interface Colorable { void setColor(byte r, byte g, byte b); } class Point { int x, y; } class ColoredPoint extends Point implements Colorable { byte r, g, b; public void setColor(byte rv, byte gv, byte bv) { r = rv; g = gv; b = bv; } } class Test { public static void main(String[] args) { Point p = new Point(); ColoredPoint cp = new ColoredPoint(); p = cp; Colorable c = cp; } } ``` -------------------------------- ### Object Instantiation in Java Source: https://docs.oracle.com/javase/specs/jvms/se24/html/jvms-4.html Illustrates the high-level Java code for creating a new class instance with arguments. ```Java new myClass(i, j, k); ```