1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @summary Verify that definite assignment works (legal code) 27 * @compile DefiniteAssignment1.java 28 * @run main DefiniteAssignment1 29 */ 30 public class DefiniteAssignment1 { 31 public static void main(String[] args) { 32 int a = 0; 33 34 { 35 int x; 36 37 switch(a) { 38 case 0: x = 0; break; 39 default: x = 1; break; 40 } 41 42 if (x != 0) 43 throw new IllegalStateException("Unexpected value."); 44 } 45 46 { 47 int x; 48 49 switch(a) { 50 case 1: x = 1; break; 51 case 0: 52 default: x = 0; break; 53 } 54 55 if (x != 0) 56 throw new IllegalStateException("Unexpected value."); 57 } 58 59 { 60 int x; 61 62 switch(a) { 63 case 1: x = 1; break; 64 case 0: 65 default: x = 0; 66 } 67 68 if (x != 0) 69 throw new IllegalStateException("Unexpected value."); 70 } 71 72 { 73 int x; 74 75 switch(a) { 76 case 0 -> x = 0; 77 default -> x = 1; 78 } 79 80 if (x != 0) 81 throw new IllegalStateException("Unexpected value."); 82 } 83 84 { 85 int x; 86 87 try { 88 switch(a) { 89 case 1: x = 1; break; 90 case 0: 91 default: throw new UnsupportedOperationException(); 92 } 93 94 throw new IllegalStateException("Unexpected value: " + x); 95 } catch (UnsupportedOperationException ex) { 96 //OK 97 } 98 } 99 100 { 101 int x; 102 103 switch(a) { 104 case 0 -> x = 0; 105 default -> throw new IllegalStateException(); 106 } 107 108 if (x != 0) 109 throw new IllegalStateException("Unexpected value."); 110 } 111 } 112 113 enum E { 114 A, B, C; 115 } 116 }