140 likes | 266 Views
This article provides an in-depth overview of Java reserved words, which are specific terms that the Java programming language uses for its syntax. It covers essential keywords such as 'abstract', 'interface', 'instanceof', 'throw', and 'throws', explaining their meanings and practical implementations. Additionally, it touches on unique keywords like 'goto', which is reserved for potential future use. Exploring Java's reserved words is crucial for developers to write clear and effective code while avoiding conflicts in naming. Join us as we demystify these important elements of Java.
E N D
Reserved words TOKEN : { < ABSTRACT: "abstract" > | < BOOLEAN: "boolean" > | < BREAK: "break" > | < BYTE: "byte" > | < CASE: "case" > | < CATCH: "catch" > | < CHAR: "char" > | < CLASS: "class" > | < CONST: "const" > | < CONTINUE: "continue" > | < _DEFAULT: "default" > | < DO: "do" > | < DOUBLE: "double" > | < ELSE: "else" > | < EXTENDS: "extends" > | < FALSE: "false" > | < FINAL: "final" > | < FINALLY: "finally" > | < FLOAT: "float" > | < FOR: "for" > | < GOTO: "goto" > | < IF: "if" > | < IMPLEMENTS: "implements" > | < IMPORT: "import" > | < INSTANCEOF: "instanceof" > | < INT: "int" > | < INTERFACE: "interface" > | < LONG: "long" > | < NATIVE: "native" > | < NEW: "new" > | < NULL: "null" > | < PACKAGE: "package"> | < PRIVATE: "private" > | < PROTECTED: "protected" > | < PUBLIC: "public" > | < RETURN: "return" > | < SHORT: "short" > | < STATIC: "static" > | < SUPER: "super" > | < SWITCH: "switch" > | < SYNCHRONIZED: "synchronized" > | < THIS: "this" > | < THROW: "throw" > | < THROWS: "throws" > | < TRANSIENT: "transient" > | < TRUE: "true" > | < TRY: "try" > | < VOID: "void" > | < VOLATILE: "volatile" > | < WHILE: "while" > }
Abstract classes • abstract class is a class that is declared abstract • May or may not include abstract methods. • Abstract classes cannot be instantiated. abstract class GraphicObject { int x, y; ... void moveTo(int newX, int newY) { ... } abstract void draw(); abstract void resize(); }
goto • In the Java programming language, goto is a reserved word. • The Java programming language does not have a goto statement. • It is a word reserved for possible incorporation in a future version.
instanceof • The instanceof keyword can be used to test if an object is of a specified type. public class MainClass { public static void main(String[] a) { String s = "Hello"; if (s instanceof java.lang.String) System.out.println("is a String"); } }
Difference between throw and throws in Java • throw is used to actually throw the exception. • throws is declarative for the method. • One declares it, and the other one does it • They are not interchangeable. public void myMethod (int param) throws MyException { if (param < 10) { throw new MyException("Too low!"); } //Blah, Blah, Blah... }
transient • When an object is transferred through the network, the object needs to be 'serialized'. Serialization converts the object state to serial bytes. Those bytes are sent over the network and the object is recreated from those bytes. Member variables marked by the java transient keyword are not transferred, they are lost on purpose. public class Foo { private String saveMe; private transient String dontSaveMe; private transient String password; . . }
volatile • It marks a member variable not to be used in optimization, during compilation. • Optimizing compilers may insert code to gain run-time performance. • This can cause problems when a variable can be changed by many threads. • Those variables that can be changed by more than one thread should be set to volatile. • E.g.,private volatile changingVar;
Numeric literals TOKEN : { < INTEGER_LITERAL: <DECIMAL_LITERAL> (["l","L"])? | <HEX_LITERAL> (["l","L"])? | <OCTAL_LITERAL> (["l","L"])? > | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])? | "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])? | (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])? | (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
Separators TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < LBRACKET: "[" > | < RBRACKET: "]" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > }
Operators TOKEN : { < ASSIGN: "=" > | < GT: ">" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < INCR: "++" > | < DECR: "--" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > | < LSHIFT: "<<" > | < RSIGNEDSHIFT: ">>" > | < RUNSIGNEDSHIFT: ">>>" > | < PLUSASSIGN: "+=" > | < MINUSASSIGN: "-=" > | < STARASSIGN: "*=" > | < SLASHASSIGN: "/=" > | < ANDASSIGN: "&=" > | < ORASSIGN: "|=" > | < XORASSIGN: "^=" > | < REMASSIGN: "%=" > | < LSHIFTASSIGN: "<<=" > | < RSIGNEDSHIFTASSIGN: ">>=" > | < RUNSIGNEDSHIFTASSIGN: ">>>=" > }
CompilationUnit void CompilationUnit() : { } { [ PackageDeclaration() ] ( ImportDeclaration() )* ( TypeDeclaration() )* <EOF> } What is the smallest legal Java program?
Declarations void PackageDeclaration() : { } { "package" Name() ";" } void ImportDeclaration() : { } { "import" Name() [ "." "*" ] ";" } // Eg, import java.io.*; void TypeDeclaration() : { } { LOOKAHEAD( ( "abstract" | "final" | "public" )* "class" ) ClassDeclaration() | InterfaceDeclaration() | ";" } void ClassDeclaration() : { } { ( "abstract" | "final" | "public" )* "class" <IDENTIFIER> [ "extends" Name() ] [ "implements" NameList() ] "{" ( ClassBodyDeclaration() )* "}" }
extends vs. implements • extends • the new class extends a superclass • automatically inherits all of the superclass's methods • override any of them (except the ones defined "final") • implements • implements an interface • the class must override all of the interface's methods, since the interface contains only definitions, not code.