Java Tokens:
A token is the smallest element of a program that is meaningful to the compiler. These tokens define the structure of the Java language. When you submit a Java program to the Java compiler, the compiler parses the text and extracts individual tokens.
Java tokens can be broken into five categories: identifiers, keywords, literals, operators, and separators. The Java compiler also recognizes and subsequently removes comments and whitespaces.
Identifiers :
“Identifiers means a sequence of uppercase(A,B,C,……,Y,Z) and lowercase(a,b,c,…..,y,z) letters, numbers(0,1 ,2,……,9), or the underscore(_) and dollar-sign($) characters and must not begin with a number.” Identifiers are tokens that represent names. These names can be assigned to variables, methods, and classes to uniquely identify them to the compiler.
Keywords :
“It is a special type of reserved word for a specific purpose which can not be use as a identifier means cannot be used as names for a variable, class, or method.”
There are 49 reserved keywords currently defined in the Java language
Valid and invalid Java identifiers.
Valid | Invalid |
HelloWorld | Hello World (uses a space) |
Hi_JAVA | Hi JAVA! (uses a space and punctuation mark) |
value3 | 3value(begins with a number) |
Tall | short (this is a Java keyword) |
$age | #age (does not begin with any other symbol except _ $ ) |
Separators :
Separators are used to inform the Java compiler of how things are grouped in the code. For example, items in a list are separated by commas much like lists of items in a sentence. The most commonly used separator in Java is the semicolon. As you have seen, it is used to terminate statements. Java Seperator are ; , {} () [] .
Java Comments
The java comments are statements that are not executed by the compiler and interpreter. The comments can be used to provide information or explanation about the variable, method, class or any statement. It can also be used to hide program code for specific time.
Type | Syntax | Usage | Example |
Single-line | // comment | All characters after the // up to the end of the line are ignored. | //This is a Single-line style comment. |
Multiline | /* comment */ | All characters between /* and */ are ignored. | /* This is a Multiline style comment. |
Documentation | /** comment */ | Same as /* */, except that the comment can be used with the javadoc tool to create automatic documentation. | /** This is a javadoc style comment. */ |
Java – Basic Datatypes
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory.
Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables.
There are two data types available in Java −
- Primitive Data Types
- Reference/Object Data Types
Primitive Data Types
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. Let us now look into the eight primitive data types in detail.
Data Type | Default Value | Default size | Range |
boolean | false | 1 bit | true and false |
char | ‘\u0000’ | 2 byte | \u0000 to \uffff(0 to 65535) |
byte | 0 | 1 byte | -128 .. 127 |
short | 0 | 2 byte | -32,768 .. 32,767 |
int | 0 | 4 byte | -2,147,483,648 .. 2,147,483,647 |
long | 0L | 8 byte | -9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807 |
float | 0.0f | 4 byte | 3.40282347 x 1038, 1.40239846 x 10-45 |
double | 0.0d | 8 byte | 1.7976931348623157 x 10308, 4.9406564584124654 x 10-324 |
Java – Variable Types
Variable provides us with named storage that our programs can manipulate. Each variable in Java has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
You must declare all variables before they can be used. Following is the basic form of a variable declaration −
data type variable [ = value][, variable [ = value] …] ;
Here data type is one of Java’s datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.
Following are valid examples of variable declaration and initialization in Java −
int a, b, c; // Declares three ints, a, b, and c.
int a = 10, b = 10; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI.
char a = ‘a’; // the char variable a iis initialized with value ‘a’