What are Tokens in Java and how to Implement them?

Last updated on Nov 26,2019 33.5K Views
Sr Research Analyst at Edureka. A techno freak who likes to explore... Sr Research Analyst at Edureka. A techno freak who likes to explore different technologies. Likes to follow the technology trends in market and write...

What are Tokens in Java and how to Implement them?

edureka.co

Often you might have seen large Java applications with thousands of lines of codes, but have you ever wondered what lies at its core? Well, these are tokens, the smallest individual elements, also known as the building blocks of a Java program. Through the medium of this article, I will throw some light on Tokens in Java, which are often neglected but form an integral part of Java programming language.

In Java, a program is a collection of classes and methods, while methods are a collection of various expressions and statements. Tokens in Java are the small units of code which a Java compiler uses for constructing those statements and expressions. Java supports 5 types of tokens which are:

  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Special Symbols

Let’s now talk about each of them one by one.

Keywords

Keywords in Java are predefined or reserved words that have special meaning to the Java compiler. Each keyword is assigned a special task or function and cannot be changed by the user. You cannot use keywords as variables or identifiers as they are a part of Java syntax itself. A keyword should always be written in lowercase as Java is a case sensitive language. Java supports various keywords, some of them are listed below:

01. abstract02. boolean03. byte04. break05. class
06. case07. catch08. char09. continue10. default
11. do12. double13. else14. extends15. final
16. finally17. float18. for19. if20. implements
21. import22. instanceof23. int24. interface25. long
26. native27. new28. package29. private30. protected
31. public32. return33. short34. static35. super
36. switch37. synchronized38. this39. throw40. throws
41. transient42. try43. void44. volatile45. while
46. assert47. const48. enum49. goto50. strictfp

Identifier

Java Identifiers are the user-defined names of variables, methods, classes, arrays, packages, and interfaces. Once you assign an identifier in the Java program, you can use it to refer the value associated with that identifier in later statements. There are some de facto standards which you must follow while naming the identifiers such as:

Example:

//Valid Identifiers
$myvariable  //correct
_variable    //correct
variable     //correct
edu_identifier_name //correct
edu2019var   //correct

//Invalid Identifiers
edu variable    //error
Edu_identifier  //error
&variable       //error
23identifier    //error
switch          //error
var/edu 	    //error
edureka's       //error

Literals

Literals in Java are similar to normal variables but their values cannot be changed once assigned. In other words, literals are constant variables with fixed values. These are defined by users and can belong to any data type. Java supports five types of literals which are as follows:

  1. Integer
  2. Floating Point
  3. Character
  4. String
  5. Boolean

Example:

public class EduLiteral { 
    public static void main(String[] args) 
    { 
        int edu1 = 112; 	// Int literal 
        float edu2 = 31.10; 	// Float literal 
        char edu3 = 'edu' // char literal 
        String edu4 = "Edureka"; // String literal 
        boolean edu5 = true; // Boolean literal 

        System.out.println(edu1); //112
        System.out.println(edu2); //31.40
        System.out.println(edu3); //edu
        System.out.println(edu4); //Edureka
        System.out.println(edu5); //true
    } 
}

Operators

An operator in Java is a special symbol that signifies the compiler to perform some specific mathematical or non-mathematical operations on one or more operands. Java supports 8 types of operators. Below I have listed down all the operators, along with their examples:

OperatorExamples
Arithmetic+ , – , / , * , %
Unary++ , – – , !
Assignment= , += , -= , *= , /= , %= , ^=
Relational==, != , < , >, <= , >=
Logical&& , || 
Ternary(Condition) ? (Statement1) : (Statement2);
Bitwise& , | , ^ , ~
Shift<< , >> , >>>

Special Symbols

Special symbols in Java are a few characters which have special meaning known to Java compiler and cannot be used for any other purpose. In the below table I have listed down the special symbols supported in Java along with their description.

SymbolDescription
Brackets []These are used as an array element reference and also indicates single and multidimensional subscripts
Parentheses()These indicate a function call along with function parameters
Braces{}The opening and ending curly braces indicate the beginning and end of a block of code having more than one statement
Comma ( , )This helps in separating more than one statement in an expression
Semi-Colon (;)This is used to invoke an initialization list
Asterisk (*)This is used to create a pointer variable in Java

With this we come to end of this article on Tokens in Java. If you want to know more about Java you can refer to our other Java Blogs.

Now that you have understood what tokens in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Tokens in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th May,2024

25th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial