Java Tokens
Java Tokens
• The Java compiler breaks the line of code into text (words) is
called Java tokens. These are the smallest element of the
Java program
• public class Demo
• {
• public static void main(String args[])
• {
• System.out.println("javatpoint");
• }
• }
• In the above code snippet, public, class, Demo, {, static, void, main, (,
String, args, [, ], ), System, ., out, println, javatpoint, etc. are the Java
tokens.
• Types of Tokens
• Java token includes the following:
• Keywords
• Identifiers
• Literals
• Operators
• Separators
• Comments
• Keywords: These are the pre-defined reserved words of any
programming language. Each keyword has a special meaning. It is
always written in lower case.
• Java provides the following keywords:
• Data Types in Java
• Data types specify the different sizes and values that can be stored in
the variable. There are two types of data types in Java:
1.Primitive data types: The primitive data types include boolean, char,
byte, short, int, long, float and double.
2.Non-primitive data types: The non-primitive data types include
Classes, Interfaces, and Arrays.
• Literals: In programming literal is a notation that represents a fixed value
(constant) in the source code.
• It can be categorized as an integer literal, string literal, Boolean literal, etc.
It is defined by the programmer.
• Once it has been defined cannot be changed.
• Integer
• Floating Point
• Character
• String
• Boolean
Operators: In operators are the special symbol that tells the compiler to perform a
special operation. Java provides different types of operators that can be classified
according to the functionality they provide.
• Arithmetic Operators
• Assignment Operators
• Relational Operators
• Unary Operators
• Logical Operators
• Ternary Operators
• Bitwise Operators
• Shift Operators
• Square Brackets []: It is used to define array elements. A pair of square brackets
represents the single-dimensional array, two pairs of square brackets represent the two-
dimensional array.
• Parentheses (): It is used to call the functions and parsing the parameters.
• Curly Braces {}: The curly braces denote the starting and ending of a code block.
• Comma (,): It is used to separate two values, statements, and parameters.
• Assignment Operator (=): It is used to assign a variable and constant.
• Semicolon (;): It is the symbol that can be found at end of the statements. It separates
the two statements.
• Period (.): It separates the package name form the sub-packages and class. It also
separates a variable or method from a reference variable.
• Comments: Comments allow us to specify information about the program inside our
Java code. Java compiler recognizes these comments as tokens but excludes it form
further processing. The Java compiler treats comments as whitespaces. Java provides
the following two types of comments:
• Line Oriented: It begins with a pair of forwarding slashes (//).
• Block-Oriented: It begins with /* and continues until it founds */.
Java Constant
• class ClassA {
• public static final double PI = 3.14;
•}
KIA,
{
Car c;
c = Car.Nano;
System.out.println("Value of c: " + c);
Nano,
switch (c) {
case Maruti:
System.out.println("Maruti car");
break;
case KIA:
Maruti,
System.out.println("KIA car");
break;
case Nano:
System.out.println("Nano car");
}
break;
}
}
}
• Java Variables
• A variable is a container which holds the value while the Java program
is executed. A variable is assigned with a data type.
• Variable is a name of memory location
• A variable is the name of a reserved area allocated in memory
• 2) Instance Variable
• A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static.
• It is called an instance variable because its value is instance-specific and is not shared among
instances.
• 3) Static variable
• A variable that is declared as static is called a static variable. It cannot be local. You can create
a single copy of the static variable and share it among all the instances of the class. Memory
allocation for static variables happens only once when the class is loaded in the memory.
public class A
{
static int m=100;//static variable
void method()
{
int n=90;//local variable
}
public static void main(String args[])
{
int data=50;//instance variable
}
}//end of clas
• Java Simple for Loop
• A simple for loop is the same as C/C++. We can initialize the variable,
check condition and increment/decrement value. It consists of four parts:
1.Initialization: It is the initial condition which is executed once when the
loop starts. Here, we can initialize the variable, or we can use an already
initialized variable. It is an optional condition.
2.Condition: It is the second condition which is executed each time to test
the condition of the loop. It continues execution until the condition is
false. It must return boolean value either true or false. It is an optional
condition.
3.Increment/Decrement: It increments or decrements the variable value.
It is an optional condition.
4.Statement: The statement of the loop is executed each time until the
second condition is false.
Syntax
Example:
NestedForExample.java
public class NestedForExample {
public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}
PyramidExample.java
public class PyramidExample {
public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
public class PyramidExample2 {
public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
public class Main {
System.out.println();
}
}
}
• public class Main {
12
for (int i = rows; i >= 1; --i) {
1 for (int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Example 6: Program to print full pyramid using *
public class Pyramid {
while (k != 2 * i - 1) {
System.out.print("* ");
++k;
}
System.out.println();
}
}
}
Example 7: Program to print pyramid using numbers
Syntax:
labelname:
for(initialization; condition; increment/decrement){
//code to be executed
}
LabeledForExample.java
//A Java program to demonstrate the use of labeled for loop
public class LabeledForExample {
public static void main(String[] args) {
//Using Label for outer and for loop
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break aa;
}
System.out.println(i+" "+j);
}
}
}
}
•
If you use break bb;, it will break inner loop only which is the default behaviour of any loop.
public class LabeledForExample2 {
public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break bb;
}
System.out.println(i+" "+j);
}
}
}
}
• What is a Switch Case in Java?
• Switch case in Java is essential for avoiding redundant if-else statements in a
program.
switch(variable_name)
{
case value1:
//code to be executed if variable_name=value1
break;
case value2:
//code to be executed if variable_name=value2
break;
default:
//code to be executed if variable_name=value3
}
package com.dataflair.switchcase; {
import java.util.*; case 1:
System.out.println("The value of the variable =
"+variable);
public class SwitchStatement { break;
case 2:
public static void main(String[] args) { System.out.println("The value of the variable
="+variable);
break;
Scanner sc = new Scanner(System.in); case 3:
System.out.println("The value of the variable
="+variable);
int variable;
break;
System.out.println("Please enter the variable default:
value 1,2,3 ");
System.out.println("The value of the variable is neither 1
nor 2 nor 3");
variable=sc.nextInt(); }
}
switch(variable) }
switch(variable)
package com.dataflair.switchcase; {
import java.util.*; case 1:
System.out.println("The value of the variable =
"+variable);
public class SwitchStatement {
case 2:
System.out.println("The value of the variable =
public static void main(String[] args) { "+variable);
case 3:
Scanner sc = new Scanner(System.in); System.out.println("Value of the variable =
"+variable);
default:
int variable;
• System.out.println("The value of the variable
System.out.println("Please enter the is neither 1 nor 2 nor 3");
variable value");
• }
• }
variable=sc.nextInt(); • }
package com.dataflair.java.switchcase;
public class SwitchStrings
{
public static void main(String[] args) {
String course="java";
switch(course)
{
case "python":
System.out.println("Python was made by Guido Van Rossum");
break;
case "java":
System.out.println("Java was made by James Gosling! It is one of Shraman's favourites!");
break;
case "c++":
System.out.println("C ++ was made by Bjarne Stroustrup");
break;
}
}
}
package com.dataflair.switchcase;
import java.util.*;
{
case 6:
public class NestedSwitch System.out.println("That is old school!");
{ break;
public static void main(String[] args) { case 8:
Scanner sc = new Scanner(System.in); System.out.println("Wow, that’s great! Tons
of new features!");
break;
String course="java";
switch(course)
}
{
break;
case "python":
case "c++":
System.out.println("Python was made by Guido
Van Rossum"); System.out.println("C ++ was made by Bjarne
Stroustrup");
break;
break;
case "java":
System.out.println("What version of Java are you
using?"); }
int version=sc.nextInt(); }
switch(version) }
Java instanceof Operator
// Main class
public class GFG {
// Main class
public class GFG {
// Double datatype
double d = 100.04;
// Print statements
System.out.println("Double value " + d);
// i % 256
b = (byte)i;
• Type Promotion in Expressions
• While evaluating expressions, the intermediate value may exceed the
range of operands and hence the expression value will be promoted.
Some conditions for type promotion are:
1.Java automatically promotes each byte, short, or char operand to int
when evaluating an expression.
2.If one operand is long, float or double the whole expression is
promoted to long, float, or double respectively.
/ Java program to Illustrate Type promotion in Expressions
// Main class
class GFG {
// The Expression
double result = (f * b) + (i / c) - (d * s);
// Main class
class GFG {
System.out.println(Math.abs(a));
System.out.println(Math.abs(d));
System.out.println(Math.abs(f));
}
}