JavaTokens
JavaTokens
class GFG {
public static void main (String[] args) {
/*
This is a Multiline Comment
*/
}}
7. Separators:
Separators are used to separate different parts of the codes. It tells the
compiler about completion of a statement in the program. The most
commonly and frequently used separator in java is semicolon (;).
int variable; //here the semicolon (;) ends the declaration of the
variable
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
System.out.println("GFG!"); //Here the semicolon (;) used to end the
print statement
}
}
1. int quantity;
2. boolean flag;
3. String message; Also, we can initialize a value to a variable. For example:
6.
When command-line arguments are supplied to JVM, JVM wraps these and
supplies them to args[]. It can be confirmed that they are wrapped up in an
args array by checking the length of args using args.length.
Internally, JVM wraps up these command-line arguments into the args[ ]
array that we pass into the main() function. We can check these arguments
using args.length method. JVM stores the first command-line argument at
args[0], the second at args[1], the third at args[2], and so on.
The Java variable declaration creates a new variable with required properties. The programming
language requires four basic things to declare a variable in the program.
1. Data-type
2. Variable name
3. Initial value
4. Semicolon
Variable name: The Java variable declaration requires a unique name. We prefer to declare
small and understandable variable names.
Initial value: Java language requires the initial value of the variable. Declare
variable with initial value does not necessary in the main class. We must assign the
initial value in the default constructor. The "final variable" needs to declare the
initial value.
Semicolon: The semicolon represents the end of the variable declaration
statement.
Variable Declaration
There are two ways to declare a variable in Java. The first method is to assign the
initial value to the variable. The second method declares variable without initial
value.
1. Data_type variable_name;
o For example: String my_name;
o We do not need to initialize data with a given variable.
o Assign value in any method and display it as an output.
o The way of declaration works inside and outside of the default method.
o The variable data is displayed inside of the default method of the class.
Examples
Java Variable Declaration Example: With Initialization
Create several variables with the different data formats. Here, we can use int,
String, Boolean and other data types.
CreateVariable.java
Type casting
Convert a value from one data type to another data type is known as type casting.
Widening Casting
Widening casting is done automatically when passing a smaller size type to a larger size
type:
Example
public class Main {
int myInt = 9;
System.out.println(myInt); // Outputs 9
Narrowing Casting
Narrowing casting must be done manually by placing the type in parentheses () in front of
the value:
Example
public class Main {
There are four scopes for variables in Java: local, instance, class, and method
parameters. Examining each of these scopes in more detail will be helpful.
Local Variables:
Local variables are those that are declared inside of a method, constructor, or code
block. Only the precise block in which they are defined is accessible. The local
variable exits the block's scope after it has been used, and its memory is freed.
Temporary data is stored in local variables, which are frequently initialised in the
block where they are declared. The Java compiler throws an error if a local variable
is not initialised before being used. The range of local variables is the smallest of all
the different variable types.
Example:
1. public SumExample
2. {
3. public void calculateSum() {
4. int a = 5; // local variable
5. int b = 10; // local variable
6. int sum = a + b;
7. System.out.println("The sum is: " + sum);
8. } // a, b, and sum go out of scope here
9. }
Output:
Example:
Example:
Method Parameters:
Variables that are supplied to a method when it is invoked are known as method
parameters. They serve as inputs for method execution and are used to receive
values from the caller. The scope of method parameters is restricted to the method
in which they are defined, making them local to that method. The values of the
arguments given are allocated to the respective parameters when a method is
called.
Example:
1. public void printName(String name) { // name is a method parameter
2. System.out.println("Hello, " + name + "!");
3. }
Writing clear and effective Java code requires a solid understanding of variable
scope. Here are some essential ideas to bear in mind:
The minimal scope necessary for variables to serve their purpose should be
expressed. As a result, there are fewer naming conflicts, and the code is easier to
comprehend.
Literals in Java
In Java, literal is a notation that represents a fixed value in the source code. In
lexical analysis, literals of a given type are generally known as tokens. In this
section, we will discuss the term literals in Java.
Literals
In Java, literals are the constant values that appear directly in the program. It can
be assigned directly to a variable. Java has various types of literals. The following
figure represents a literal.
1. Integer Literal
2. Character Literal
3. Boolean Literal
4. String Literal
Integer Literals
Integer literals are sequences of digits. There are three types of integer literals:
o Floating-point literals for float type end with F or f. For example, 6f, 8.354F,
etc. It is a 32-bit float literal.
o Floating-point literals for double type end with D or d. It is optional to write D
or d. For example, 6d, 8.354D, etc. It is a 64-bit double literal.
o It can also be represented in the form of the exponent.
Floating:
Decimal:
Boolean Literals
Boolean literals are the value that is either true or false. It may also have values 0
and 1. For example, true, 0, etc.
import java.io.*;
array[i] = i * 2;
}
System.out.print("Array: ");
System.out.println();
Output
Array: 0 2 4 6 8 10 12 14 16 18
Operators:
constitute the basic building block to any programming language. Java too
provides many types of operators which can be used according to the need to
perform various calculations and functions be it logical, arithmetic, relational, etc.
They are classified based on the functionality they provide. Here are a few types:
1. Arithmetic Operators
3. Assignment Operator
4. Relational Operators
5. Logical Operators
6. Ternary Operator
7. Bitwise Operators
8. Shift Operator
Arithmetic Operators
Operator 3: Increment(++)
It is used to increment the value of an integer. It can be used in two separate
ways:
3.1: Post-increment operator
When placed after the variable name, the value of the operand is incremented but
the previous value is retained temporarily until the execution of this statement and
it gets updated before the execution of the next statement.
Syntax:
num++
Illustration:
num = 5
num++ = 6
3.2: Pre-increment operator
When placed before the variable name, the operand’s value is incremented
instantly.
Syntax:
++num
Illustration:
num = 5
++num = 6
Operator 4: Decrement ( — )
It is used to decrement the value of an integer. It can be used in two separate
ways:
4.1: Post-decrement operator
When placed after the variable name, the value of the operand is decremented but
the previous values is retained temporarily until the execution of this statement and
it gets updated before the execution of the next statement.
Syntax:
num--
Illustration:
num = 5
num-- = 5 // Value will be decremented before execution of next
statement.
4.2: Pre-decrement operator
When placed before the variable name, the operand’s value is decremented
instantly.
Syntax:
--num
Illustration:
num = 5
--num = 5 //output is 5, value is decremented before execution of
next statement
Operator 5: Bitwise Complement(~)
This unary operator returns the one’s complement representation of the input value
or operand, i.e, with all bits inverted, which means it makes every 0 to 1, and every
1 to 0.
Syntax:
~(operand)
Illustration:
a = 5 [0101 in Binary]
result = ~5
import java.io.*;
// Main class
class GFG {
// Main driver method
// Declaring a variable
int n1 = 6, n2 = -2;
System.out.println(
System.out.println(
Output
First Number = 6
Second Number = -2
6's bitwise complement = -7
-2's bitwise complement = 1
Example program in Java that implements all basic unary operators for user
input:
Java
import java.util.Scanner;
System.out.println(
"The value of result after unary plus is: "
+ result);
result = -num;
System.out.println(
+ result);
result = ++num;
System.out.println(
+ result);
result = num++;
System.out.println(
+ result);
result = --num;
System.out.println(
+ result);
result = num--;
System.out.println(
+ result);
Output