zoukankan      html  css  js  c++  java
  • Java 编程语言中很少被人了解的特性-statement label

    下面的语句会编译报错或者打印什么?

            System.out.print("baidu site :");
            https://www.baidu.com;
            System.out.println(" format");

    很多人会说:会编译出错,中间那行是什么鬼?

    Java 编程语言中很少被人了解的特性-statement label

     

    其实,不会报错,会打印出:

    baidu site : format

    如果改成这样的语句,是不是就不会觉得编译报错了?

            System.out.print("baidu site :");
            https :
                //www.baidu.com;
            System.out.println(" format");

    像不像switch语句中的case

    int q = (n+7)/8;
    switch (n%8) {
     case 0: do { foo(); // Great C hack, Tom,
     case 7: foo(); // but it's not valid here.
     case 6: foo();
     case 5: foo();
     case 4: foo();
     case 3: foo();
     case 2: foo();
     case 1: foo();
     } while (--q > 0);
    }

    上面的语句,":" 是statement label 翻译成标号语句。

    其语法如下:

    LabeledStatement:
    Identifier : Statement
    LabeledStatementNoShortIf:
    Identifier : StatementNoShortIf

    与c和c++不同,java中没有goto语句;标号语句用于出现在标号语句内任何地方的break或者continue语句之上。

    再来一个标句语句作为结尾的练习吧

    class Test {
     char[] value;
     int offset, count;
     int indexOf(TestString str, int fromIndex) {
     char[] v1 = value, v2 = str.value;
     int max = offset + (count - str.count);
     int start = offset + ((fromIndex < 0) ? 0 : fromIndex);
     i:
     for (int i = start; i <= max; i++) {
     int n = str.count, j = i, k = str.offset;
     while (n-- != 0) {
     if (v1[j++] != v2[k++])
     continue i;
     } 
     return i - offset;
     }
     return -1;
     }
    }

    参考资料

    【1】https://docs.oracle.com/javase/specs/jls/se12/html/jls-14.html#jls-14.7

  • 相关阅读:
    [树形dp] Luogu P4516 潜入行动
    [kruskal][Trie] Codeforces 888G Xor-MST
    [线性基] Luogu P4151 最大XOR和路径
    [线段树] Luogu P4560 砖墙
    [递归][重心] Luogu P4886 快递员
    [Trie][贪心][堆] LibreOJ #3048 异或粽子
    [长链剖分][优先队列] LibreOJ #3052 春节十二响
    [支配树] Bzoj P2815 灾难
    [长链剖分][线段树] Bzoj P1758 重建计划
    [dsu on tree] Codeforces 600E Lomsat gelral
  • 原文地址:https://www.cnblogs.com/davidwang456/p/11505556.html
Copyright © 2011-2022 走看看