Every language has some reserved words that cannot be used as identifiers called Keywords. Java has 57 such keywords. To get complete list go here – Java Keywords.
Few interesting things:
- const and goto are reserved keywords even though they are not used by the language.
- theory around volatile is bit complex. First we need to know that threads can cache data in processor cache memory for performance reasons rather than reading from / writing to main memory every time. In a single-core / multi-core processor, if multiple threads are working on the same variable with each thread using a different cache, they might not see the changes by other threads and will result in race conditions. To avoid this, if a variable is declared as volatile then threads will NOT cache that variable and is always read from / updated to the main memory.
There is one more concept called crossing the memory barrier. This means that when a memory barrier is crossed, JVM syncs all cached copies of variables to main memory and invalidates any cache that is marked as dirty in main memory. This is important because when a volatile variable is accessed, all the other variables that are cached are synced to main memory first. From Java 5 onwards, accessing a volatile variable will result in crossing a memory barrier.
- there is a keyword called strictfp as one of the reserved words. For performance reasons JVM can offload floating point calculations to the platform its running on. Since different platforms use different precisions for floating points, we would get different results when calculating floating points on these platforms. When this keyword is applied to a class, all the calculations inside the class will use strict floating point math by java runtime, resulting in same value on all the platforms. This keyword can be applied to a class, interface or a non abstract method.