What is Token?

TOKENS: A Java program is basically a collection of Classes. A Class is defined as a set of declaration statements and methods containing executable statements. Most statements contain an expression, which describes the actions carried out on data. Smallest individual units in a program are known as Tokens. The Compiler recognizes them for building up expressions and statements. In simple terms, a Java program is a collection of Tokens, Comments, and Whitespaces. Java language includes five types of tokens. They are o Reserved keywords o Identifiers o Literals o Operators o Separators Keywords: Keywords are an essential part of a language definition. They implement specific features of the language. Java language has reserved 60 words as keywords. According to Syntax, Keywords are combined with Operators and Separators. So, Java programmers know the meaning of all these words. Since Keyword has a specific meaning in Java, cannot use them as names for Variables, Classes, Methods and so on. All Keywords are to be written in lower case letters since Java is Case-Sensitive. One can use these words as Identifiers by changing one (or) more letters to upper case. However, it is a bad practice and should be avoided. Java does not use many of C/C++ Keywords and on the other hand, has added as many as 27 new keywords to implement the new features of the language. The keywords that are UNIQUE to Java are shown in boldface. Java Keywords abstract boolean break byte byvalue* case Cast* catch char Const* continue default do double else extends false** final finally float for future* generic* go to* if implements import Inner* Instance of int interface long native new Null** Operator* Outer* package private protected public Rest* return short static super switch synchronized this Thread safe* throw throws transient True** try Var* void volatile while *- Reserved for future **-values are defined by Java Identifiers: Identifiers are a programmer- designed tokens. It is used for naming Classes, Methods, Variables, Object, Labels, Packages and Interfaces in a program. Rules for Identifiers: a. Have alphabets, digits, and underscore (_) and dollar ($) sign characters. b. Must not begin with a digit. c. Uppercase and Lowercase must be important. d. Can be of any length. Identifiers must be meaningful, short enough to be quickly and easily typed and long enough to be descriptive and easily read. Java developers have followed some naming conventions:  Names of all public methods and instance variables start with a leading lowercase letter. Eg: average, sum  When more than one word is used in a name, the second and subsequent words are marked with leading uppercase letters. Eg: day Temperature, first Day of Month, total Marks  All private and local variables use only lowercase letters combined with underscores. Eg: length, batch_ strength  All Classes and Interfaces start with a leading uppercase letter (and each subsequent word with a leading uppercase letter). Eg: Student, Hello Java  Variables that represent constant values use all uppercase letters and underscores between words. Eg: TOTAL, PRINCIPAL_AMOUNT They are like Symbolic Constants in C. It should be remembered that all these are conventions and not rules. We may follow our own conventions as long as we do not break the basic rules of naming conventions. Literals: Literals in Java are a sequence of characters (digits, letters, and other characters) that represent Constant values to be stored in variables. Java language specifies FIVE major types of Literals. They are, o Integer Literals o Floating Point Literals o Character Literals o String Literals o Boolean Literals Each of them has a type associated with it. The type describes how the values behave and how they are stored. Operators: A symbol that takes one/more arguments and operates on them to produce a result. Separators: A symbol used to indicate where groups of code are divided and arranged. They basically define the shape and function of our code. SEPARATOR NAME FUNCTIONS Parentheses ( ) Used to enclose parameters in method definition and invocation (invoking the Methods and Classes) and also used for defining a procedure in expressions, containing expressions for flow control and surrounding cast types. Braces { } Used to contain the values of automatically initialized arrays and to define a block of code for Classes, Methods, and local Scopes. Brackets [ ] Used to declare array types and for dereferencing (the act of reassigning the reference ) array values Semi-Colon; Used to separate statements Comma, Used to separate consecutive identifiers in a variable declaration, also used to chain statements together inside a ‘for’ statement. Period. Used to separate package names from sub-packages and Classes, also used to separate a variable/method from a reference variable. CHARACTER SET: The smallest units of Java language are the characters used to write Tokens. These characters are defined by the UNICODE CHARACTER SET, an emerging standard that tries to create characters for a large number of scripts worldwide. The Unicode is a 16-bit character coding system and currently supports more than 34,000 defined characters derived from 24 languages from America, Europe, Middle East, Africa and Asia (including India). Most of the basic ASCII (American Standard Code for Information Interchange) characters, which includes letters, digits and punctuation marks, used in normal English. COMMENTS: Comments are used in programs to make them more understandable, in a program. Comments in Java are participating in influencing the program to end up more intelligible by setting the detail of code included and appropriate utilization of remarks makes support simpler and discovering bugs effectively. Remarks are disregarded by the compiler while aggregating a code, i.e. they compiler won’t read them. There are three types of comments: • Single – line comments, • Multi-line comments, • Documentation comments. Single Line Comments: A single line comment is similar to a sentence. Two forward slashes are placed at the beginning of a line of text. It is also called as trailing or end-of-line comments. Syntax: //This is single line comment Example: public class sample { public static void main(String[] args) { int a=20;//Here, a is a variable System.out.println(i);// print the value of i } } Multiline comments : Multiline comments are used for large text descriptions of code or to comment out chunks of code while debugging applications. It is also called as a traditional or slash-star comment. Comments are ignored by the compiler. Syntax: /* Multiline comment */ Example: public class sample1 { public static void main(String[] args) { /* Let's declare and print variable in java. */ int a=20; System.out.println(i); } } Documentation Comments: Documentation (Javadoc) comments are written to document the APIs a class reveals to its users. Users of a class, of course, are programmers. The API documentation is developed as part of the source code and kept in source files. By using documentation comments classes, fields, constructors, and methods are documented. Javadoc is a tool which comes as part of JDK. This tool generates HTML documentation from the source files. Syntax: /** documentation comments */ Documentation comments are written in html surrounded by /** and */ and like traditional comments can span multiple lines of code. Example: /** * Represents a student enrolled in the school. * A student can be enrolled in many courses. */ public class Student { /** * The first and last name of this student. */ private String name; /** * Creates a new Student with the given name. * The name should include both first and * last name. */ public Student(String name) { this.name = name; } }
SEE ALL Add a note
Add your Comment

Search Courses

More Courses by admin


    Ajay Skills Academy is an initiative by
    Markose m-Learning Wireless (MMW) Pvt Ltd.

    0 (91) 9443792391

    Our Offices @

    North America



    Ajay Skills Academy - An Initiative by Markose M-Learning (MMW)(P)Ltd.