Java Tokens
Java Tokens
In Java, the program contains classes and methods. Further, the methods
contain the expressions and statements required to perform a specific
operation. These statements and expressions are made up of tokens. In
other words, we can say that the expression and statement is a set
of tokens. The tokens are the small building blocks of a Java program that
are meaningful to the Java compiler.
Types of Tokens
Java token includes the following:
o Keywords
o Identifiers
o Literals
o Operators
o Separators
o 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:
w
01. abstract 02. boolean 03. byte 04. break 05.
1. PhoneNumber
2. PRICE
3. radius
4. a
5. a1
6. _phonenumber
7. $circumference
8. jagged_array
9. 12radius //invalid
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. Java provides five types of literals are as
follows:
o Integer
o Floating Point
o Character
o String
o Boolean
Literal Type
23 int
9.86 double
"javatpoint" String
Operators: In programming, 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.
There are eight types of operators in Java, are as follows:
o Arithmetic Operators
o Assignment Operators
o Relational Operators
o Unary Operators
o Logical Operators
o Ternary Operators
o Bitwise Operators
o Shift Operators
Operator Symbols
Arithmetic +,-,/,*,%
Unary ++ , - - , !
Assignment = , += , -= , *= , /= , %= , ^=
Logical && , ||
(Condition) ? (Statement1) :
Ternary
(Statement2);
Bitwise &,|,^,~
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.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
The Boolean data type specifies one bit of information, but its "size" can't be
defined precisely.
Example:
Example:
The short data type can also be used to save memory just like byte data
type. A short data type is 2 times smaller than an integer.
Example:
The int data type is generally used as a default data type for integral values
unless if there is no problem about memory.
Example:
Example:
Example:
1. float f1 = 234.5f
Example:
double d1 = 12.3
Example:
Variable
A variable is the name of a reserved area allocated in memory. In other
words, it is a name of the memory location. It is a combination of "vary +
able" which means its value can be changed.
Types of Variables
o local variable
o instance variable
o static variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You
can use this variable only within that method and the other methods in the
class aren't even aware that the variable exists.
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.
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.
JVM (Java Virtual Machine) Architecture
JVM (Java Virtual Machine) is an abstract machine. It is a specification that
provides runtime environment in which java bytecode can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is
platform dependent).
What is JVM
It is:
What it does
The JVM performs following operation:
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JVM provides definitions for the:
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
JVM Architecture
Let's understand the internal architecture of JVM. It contains classloader,
memory area, execution engine etc.
1) Classloader
Classloader is a subsystem of JVM which is used to load class files. Whenever
we run the java program, it is loaded first by the classloader. There are three
built-in classloaders in Java.
1. Bootstrap ClassLoader:
2. Extension ClassLoader:
3. System/Application ClassLoader:
2) Class(Method) Area
Class(Method) Area stores per-class structures such as the runtime constant
pool, field and method data, the code for methods.
3) Heap
It is the runtime data area in which objects are allocated.
4) Stack
Java Stack stores frames. It holds local variables and partial results, and plays
a part in method invocation and return.
7) Execution Engine
It contains:
1. A virtual processor
2. Interpreter:
3. Just-In-Time(JIT) compiler:
JVM
JVM (Java Virtual Machine) is an abstract machine. It is called a virtual
machine because it doesn't physically exist. It is a specification that provides
a runtime environment in which Java bytecode can be executed. It can also
run those programs which are written in other languages and compiled to
Java bytecode.
JVMs are available for many hardware and software platforms. JVM, JRE, and
JDK are platform dependent because the configuration of each OS is different
from each other. However, Java is platform independent. There are three
notions of the JVM: specification, implementation, and instance.
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
JRE
JRE is an acronym for Java Runtime Environment. It is also written as Java
RTE. The Java Runtime Environment is a set of software tools which are used
for developing Java applications. It is used to provide the runtime
environment. It is the implementation of JVM. It physically exists. It contains a
set of libraries + other files that JVM uses at runtime.
JDK
JDK is an acronym for Java Development Kit. The Java Development Kit (JDK)
is a software development environment which is used to develop Java
applications and applets. It physically exists. It contains JRE + development
tools.
Decision-Making statements:
As the name suggests, decision-making statements decide which statement
to execute and when. Decision-making statements evaluate the Boolean
expression and control the program flow depending upon the result of the
condition provided. There are two types of decision-making statements in
Java, i.e., If statement and switch statement.
1) If Statement:
In Java, the "if" statement is used to evaluate a condition. The control of the
program is diverted depending upon the specific condition. The condition of
the If statement gives a Boolean value, either true or false. In Java, there are
four types of if-statements given below.
1. Simple if statement
2. if-else statement
3. if-else-if ladder
4. Nested if-statement
if(condition)
{
statement 1; //executes when condition is true
}
Consider the following example in which we have used the if statement in the
java code.
Student.java
x + y is greater than 20
2) if-else statement
The if-else statement is an extension to the if-statement, which uses another
block of code, i.e., else block. The else block is executed if the condition of
the if-block is evaluated as false.
Syntax:
if(condition)
{
statement 1; //executes when condition is true
}
else
{
statement 2; //executes when condition is false
}
Consider the following example.
Student.java
Output:
x + y is greater than 20
3) if-else-if ladder:
The if-else-if statement contains the if-statement followed by multiple else-if
statements. In other words, we can say that it is the chain of if-else
statements that create a decision tree where the program may enter in the
block of code where the condition is true. We can also define an else
statement at the end of the chain.
if(condition 1)
{
statement 1; //executes when condition 1 is true
}
else if(condition 2)
{
statement 2; //executes when condition 2 is true
}
else
{
statement 2; //executes when all the conditions are false
}
Consider the following example.
Student.java
Delhi
4. Nested if-statement
n nested if-statements, the if statement can contain a if or if-else statement
inside another if or else-if statement.
if(condition 1)
{
statement 1; //executes when condition 1 is true
if(condition 2)
{
statement 2; //executes when condition 2 is true
}
else
{
statement 2; //executes when condition 2 is false
}
}
Consider the following example.
Student.java
if(address.endsWith("India"))
{
if(address.contains("Meerut"))
{
System.out.println("Your city is Meerut");
}
else if(address.contains("Noida"))
{
System.out.println("Your city is Noida");
}
Else
{
System.out.println(address.split(",")[0]);
}
}
else
{
System.out.println("You are not living in India");
}
}
}
Output:
Delhi
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch
statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched. The switch
statement is easier to use instead of if-else-if statements. It also enhances
the readability of the program.
Loop Statements
In programming, sometimes we need to execute the block of code repeatedly
while some condition evaluates to true. However, loop statements are used
to execute the set of instructions in a repeated order. The execution of the
set of instructions depends upon a particular condition.
In Java, we have three types of loops that execute similarly. However, there
are differences in their syntax and condition checking time.
1. for loop
2. while loop
3. do-while loop
Calculation.java