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:
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:
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:
- Identifiers must begin with a letter, dollar sign or underscore.
- Apart from the first character, an identifier can have any combination of characters.
- Identifiers in Java are case sensitive.
- Java Identifiers can be of any length.
- Identifier name cannot contain white spaces.
- Any identifier name must not begin with a digit but can contain digits within.
- Most importantly, keywords can’t be used as identifiers in Java.
//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:
- Integer
- Floating Point
- Character
- String
- Boolean
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:
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.
Symbol | Description |
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.