Java reserved word summary that you do not know unexpectedly

Overview

Java reserved words range from those you see often to those you don't see very often. Here is a brief introduction to the rare reserved words that I chose based on my own judgment and prejudice.

I won't go into too much detail about each one so that it doesn't get too long.

According to the Java language specifications, "reserved words" are called "keywords". I feel that "reserved words" are more common, so I will use the term "reserved words" in this article.

Target audience

Java beginner It may be known to those who are familiar with Java to some extent (prevention line)

environment

Java11

List of reserved words in Java

First, let's take a look at the list of reserved words. According to the official language specification, as of Java 11, there are 51 reserved words as follows. https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9

abstract   continue   for          new         switch
assert     default    if           package     synchronized
boolean    do         goto         private     this
break      double     implements   protected   throw
byte       else       import       public      throws
case       enum       instanceof   return      transient
catch      extends    int          short       try
char       final      interface    static      void
class      finally    long         strictfp    volatile
const      float      native       super       while
_ (underscore)

I will pick up some of them and introduce them.

Reserved word (keyword)

assert A reserved word for using assertions in Java. An assertion describes the prerequisites that should be met when executing a program, and without fear of misunderstanding, it seems to be a mechanism to check whether the value of a variable is an unexpected value, etc. It is a thing.

Write a conditional expression after assert, and if the evaluation value of the conditional expression is false, an AssertionError will be thrown. For example, it can be used to check whether the value of the method argument is the expected value.

//division
private int divide(int x, int y) {
    assert y != 0;  //Y must not be 0 as it is a division

    return x / y;
}

Also, asserts are not enabled unless you specify -enableassertions (or -ea) at runtime. You can enable or disable it by passing the above option. It can be enabled during development and disabled during production.

const It is a reserved word for declaring constants in C language etc., but it is a reserved word that has no meaning in Java. It doesn't mean anything, but it exists as a reserved word for some reason. Using const has no syntactic meaning, so using it will result in a compilation error.

Note that you should use final instead of const to declare a constant in Java.

//Since it is a reserved word, syntax highlighting is done.
const int num = 1;
//error:
//Illegal start of expression
//  const int num = 1;
//  ^

default A rare reserved word with multiple uses. It was added as the version went up.

--A label indicating the location to be executed when there is no matching item in the switch statement. --Modifier that specifies the default value of the constant in the annotation definition (from Java 5) --Modifier in interface definition to indicate that it is the default method (from Java 8)

Switch statement example

switch(i) {
    case 1:
        System.out.println("one");
        break;
    case 2:
        System.out.println("two");
        break;
    default:
        System.out.println("Other");
}

Annotation example

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Range {

    int min() default Integer.MIN_VALUE;

    int max() default Integer.MAX_VALUE;
}

Example interface default method

public interface Person {
    default String name() {
        return "unknown";
    }
}

goto In C language etc., it is a reserved word used to skip control to an arbitrary place, but like const, it is a reserved word that exists even though it has no meaning in Java. There is no equivalent to goto in Java. Abuse of goto can significantly reduce the readability of the source code, so relatively modern languages often do not have equivalent functionality.

Like const, of course it's a compile error when used.

//After all syntax highlighting is done
goto label;
//error:
//Illegal start of expression
//  goto label;
//  ^

native A reserved word that indicates that the content of the method is implemented in a native language such as C. It is executed using JNI etc.

public native boolean createDirectory(File f);

strictfp This is a reserved word to prevent the floating-point calculation result from changing depending on the execution environment (so that the result is the same in all execution environments). Qualify methods and classes. Conversely, if you do not qualify with strictfp, the floating point calculation result may change depending on the execution environment.

Personally, I have never seen it actually used.

private strictfp double calc(double x, double y) {
    return x * y;
}

transient Qualified fields are not subject to serialization. Serialization is the conversion of the contents of an object so that it can be saved to a file or sent to another machine. Qualifying with transient excludes that field from the serialized data.

public class Person implements Serializable {
    private String firstName;
    private String lastName;
    private transient String fullName;
}

volatile Suppresses single-threaded assumption optimization for qualified fields. I'm sure some of you have seen it, but don't know what it means. I'm not sure either. </ s>

If not qualified with volatile, the compiler may output bytecode that is slightly different from the source for optimization, but volatile suppresses such optimization. In the case of single thread, there is no particular effect, but in the case of multithread, optimization by the compiler may cause inconsistency. Volatile is used to prevent the occurrence of such inconsistencies.

volatile boolean shutdownRequested;
 
...
 
public void shutdown() { shutdownRequested = true; }
 
public void doWork() { 
    while (!shutdownRequested) { 
        // do stuff
    }
}

From https://www.ibm.com/developerworks/jp/java/library/j-jtp06197.html

In the above example, we expect the operation to exit the loop when shutdown () is called by another thread, but it may not work as expected unless shutdownRequested is qualified with volatile.

... It was supposed to be, but when I actually tried it, it worked normally even without volatile. Please tell me who is familiar with it! </ font>

_(underscore) In languages such as Scala, _ is used for placeholders, etc., but Java does not have such a feature. Like const and goto, they are meaningless reserved words (symbols?). _ Became a reserved word relatively recently with Java 9, and may be in anticipation of future enhancements.

int _ = 1;
//error:
//From release 9'_'Is a keyword and cannot be used as an identifier
//  int _ = 1;
//      ^

Extra edition

Reserved words that once existed

widefp The opposite of strictfp, the floating point calculation result becomes environment dependent. Widefp has been removed as it now behaves like that by default.

Things that look like reserved words but are not reserved words

literal

true and false

It is a familiar boolean literal. In fact, these are not reserved words in the Java language specification. Well, it's like a reserved word because it can't be used as an identifier.

null It's a familiar null literal. Like boolean literals, it cannot be used as an identifier, but it is not a reserved word in the language specification.

Restricted keyword

open, module, requires, transitive, exports, opens, to, uses, provides, with Both were added in Java 9 for modular functionality. If you pick up each one, it will be long and I don't understand well </ s>, so I will omit the explanation. Both are words that have a special meaning, but they can be used as variable names. Probably for compatibility.

String module = "module";

Reserved type name

var Used for type inference functionality added from Java 10. This also has a special meaning like the "restricted keywords" above, but it can be used as a variable name, etc. This may not be a reserved word for compatibility.

var var = 1;

Summary

I've seen a lot of things from Java reserved words to things that look like reserved words but aren't reserved words. I would be happy if you could think that there was such a thing.

Reference link

https://docs.oracle.com/javase/specs/jls/se11/html/jls-3.html#jls-3.9 https://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%BC%E3%83%AF%E3%83%BC%E3%83%89_(Java) https://java.keicode.com/lang/assert.php https://www.wdic.org/w/TECH/strictfp https://www.ibm.com/developerworks/jp/java/library/j-jtp06197.html https://www.wdic.org/w/TECH/%E4%BA%88%E7%B4%84%E8%AA%9E%20%28Java%29