Curriculum
- 8 Sections
- 119 Lessons
- 25 Weeks
Expand all sectionsCollapse all sections
- Course Overview/*! CSS Used from: Embedded */ *, ::after, ::before { box-sizing: border-box; border-width: 0; border-style: solid; border-color: #e5e7eb; } ::after, ::before { --tw-content: ''; } h2 { font-size: inherit; font-weight: inherit; } a { color: inherit; text-decoration: inherit; } h2, p { margin: 0; } :disabled { cursor: default; } *, ::before, ::after { --tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; } .mx-auto { margin-left: auto; margin-right: auto; } .mb-2 { margin-bottom: 0.5rem; } .mb-4 { margin-bottom: 1rem; } .mb-6 { margin-bottom: 1.5rem; } .mr-2 { margin-right: 0.5rem; } .max-w-screen-sm { max-width: 640px; } .max-w-screen-xl { max-width: 1280px; } .rounded-lg { border-radius: 0.5rem; } .bg-primary-700 { --tw-bg-opacity: 1; background-color: rgb(29 78 216 / var(--tw-bg-opacity)); } .bg-white { --tw-bg-opacity: 1; background-color: rgb(255 255 255 / var(--tw-bg-opacity)); } .px-4 { padding-left: 1rem; padding-right: 1rem; } .px-5 { padding-left: 1.25rem; padding-right: 1.25rem; } .py-2.5 { padding-top: 0.625rem; padding-bottom: 0.625rem; } .py-8 { padding-top: 2rem; padding-bottom: 2rem; } .text-center { text-align: center; } .text-4xl { font-size: 3rem; line-height: 2.5rem; } .text-sm { font-size: 0.875rem; line-height: 1.25rem; } .font-extrabold { font-weight: 800; } .font-light { font-weight: 300; } .font-medium { font-weight: 500; } .leading-tight { line-height: 1.25; } .tracking-tight { letter-spacing: -0.025em; } .text-gray-500 { --tw-text-opacity: 1; color: rgb(107 114 128 / var(--tw-text-opacity)); } .text-gray-900 { --tw-text-opacity: 1; color: rgb(17 24 39 / var(--tw-text-opacity)); } .text-white { --tw-text-opacity: 1; color: rgb(255 255 255 / var(--tw-text-opacity)); } .hover\:bg-primary-800:hover { --tw-bg-opacity: 1; background-color: rgb(30 64 175 / var(--tw-bg-opacity)); } .focus\:outline-none:focus { outline: 2px solid transparent; outline-offset: 2px; } .focus\:ring-4:focus { --tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color); --tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(4px + var(--tw-ring-offset-width)) var(--tw-ring-color); box-shadow: var(--tw-ring-offset-shadow), var(--tw-ring-shadow), var(--tw-shadow, 0 0 #0000); } .focus\:ring-primary-300:focus { --tw-ring-opacity: 1; --tw-ring-color: rgb(147 197 253 / var(--tw-ring-opacity)); } @media (min-width: 640px) { .sm\:py-16 { padding-top: 4rem; padding-bottom: 4rem; } } @media (min-width: 768px) { .md\:text-lg { font-size: 1.5rem; line-height: 1.75rem; } } @media (min-width: 1024px) { .lg\:px-6 { padding-left: 1.5rem; padding-right: 1.5rem; } } .imgdata { width: 35% } @media (max-width: 767px) { .imgdata { width: 40% } } .course-payment { display: none !important; } .thim-course-landing-button { display: none !important; }0
- NumericalTCS-NQT Instructions : You will be given 26 Question with a time limit of 40 mins | Questions - 26 | Time - 40 mins48
- 2.1Profit and Loss [ Notes ]
- 2.2Profit & Loss [Formula]
- 2.3Profit and Loss [Practice Problems]
- 2.4Geometry Formula and Concept
- 2.5Geometry Formula and Concept [Practice problem]
- 2.6HCF and LCM [ Handmade Notes ]
- 2.7HCF and LCM [ Formula ]
- 2.8HCF and LCM [ Practice Problems ]
- 2.9Percentage Problem [Formula]
- 2.10Percentage Problem [ Notes ]
- 2.11Percentage Problem [Practice Problems]
- 2.12Alligation and Mixtures [Formula]
- 2.13Alligation and Mixtures [Handmade Notes]
- 2.14Alligation and Mixtures [Practice Problems]
- 2.15Time and Work[Formula]
- 2.16Time and Work [Handmade Notes]
- 2.17Time and Work [Practice Problems]
- 2.18Ages [ Formula ]
- 2.19Ages Problem [Handmade Notes]
- 2.20Ages Problem [Practice Problems]
- 2.21Sequence and Series
- 2.22Sequence and Series [Practice problem ]
- 2.23Divisibility
- 2.24Divisibility [Practice problem ]
- 2.25Ratio and Proportion
- 2.26Ratio and Proportion [Practice problem ]
- 2.27Probability [ Formula and Solved Examples ]
- 2.28Probability [Practice problem ]
- 2.29Perimeter, Area and Volume
- 2.30Perimeter, Area and Volume [Practice problem ]
- 2.31Your Feedback Means Alot to Us ❤️2 Minutes
- 2.32Average Formula and Concept
- 2.33Average Formula and Concept [Practice problem ]
- 2.34Speed Time And Distance
- 2.35Speed Time And Distance [Practice problem ]
- 2.36Mean Median Mode
- 2.37Mean Median Mode [Practice problem ]
- 2.38Decimals and Fractions
- 2.39Decimals and Fractions Formula [Practice problem ]
- 2.40Pie Chart
- 2.41Pie Chart Practice Problem
- 2.42Bar Chart
- 2.43Bar Chart Practice Problem
- 2.44Your Feedback Means Alot to Us ❤️2 Minutes
- 2.45Profit and Loss [Notes]
- 2.46Percentage Problem [Notes]
- 2.47Ratio and Proportion [Notes]
- 2.48Average [Notes]
- VerbalTCS-NQT Instructions : You will be given 24 Question with a time limit of 30 mins | Questions - 24 | Time - 15 mins + 30 mins10
- 3.1Sentence Completion [ Notes + Solved Examples + Practice Problem ]
- 3.2Spotting Erorrs [ Notes + Solved Examples + Practice Problem ]
- 3.3Reading Comprehension [ Notes + Solved Examples + Practice Problem ]
- 3.4Meanings [ Notes + Solved Examples + Practice Problem ]
- 3.5Tenses [ Notes + Solved Examples + Practice Problem ]
- 3.6Idioms And Phrases [ Notes + Solved Examples + Practice Problem ]
- 3.7Formal And Informal Sentences
- 3.8Formal And Informal Sentences [Practice problem]
- 3.9Sentence Arrangement
- 3.10Sentence Arrangement [Practice problem]
- Aptitude Series for Placement [ Free Sample Videos ]Note: TCS NQT Study Material Doesn't Contain videos we provide only Study Materials in this course , these videos are sample videos of our Aptitude Series16
- 4.1📌 Watch this Video to Crack your Aptitude2 Minutes
- 4.2Train Problems Part #119 Minutes
- 4.3Train Problems Part #211 Minutes
- 4.4Train Problems Part #319 Minutes
- 4.5Train Problems [Handmade Notes]
- 4.6📌 Practice Problems [Quiz] – Train Problems
- 4.7Calendar Basics13 Minutes
- 4.8📌 Take the Complete Aptitude Series Now [ Videos Lectures – Quant , Verbal and Reasoning ] 📌
- 4.9Calendar Problem [Handmade Notes]
- 4.10📌 Practice Problems [Quiz] – Calendar
- 4.11Time and Work Part #120 Minutes
- 4.12Time and Work Part #213 Minutes
- 4.13Time and Work Part #312 Minutes
- 4.14Time and Work [Handmade Notes]
- 4.15📌 Practice Problems [Quiz] – Time and Work
- 4.16📌 Take the Complete Aptitude Series Now [ Videos Lectures – Quant , Verbal and Reasoning ] 📌
- ReasoningTCS-NQT Instructions : You will be given 30 Question with a time limit of 50 mins | Questions - 30 | Time - 50 mins21
- 5.1Blood Relation [ Formulas + Notes ]
- 5.2Blood Relation [ Practice Problem ]
- 5.3Number Series [ Formulas + Notes ]
- 5.4Number Series [ Practice Problem ]
- 5.5Seating Arrangement [ Formulas + Notes ]
- 5.6Seating Arrangement [ Practice Problem ]
- 5.7Syllogism [ Formulas + Notes ]
- 5.8Syllogism [ Practice Problem ]
- 5.9Data Sufficiency [ Formulas + Notes ]
- 5.10Data Sufficiency [ Practice Problem ]
- 5.11Clocks [Formula]
- 5.12Clock Problem [Handmade Notes]
- 5.13Clock Problem [Practice Problems]
- 5.14Calendar Problem [Formula]
- 5.15Calendar Problem [Handmade Notes]
- 5.16Calendar Problem [Practice Problems]
- 5.17Number Series [Notes]
- 5.18Syllogism [Notes]
- 5.19Clock Problem [Notes]
- 5.20Coding decoding [Notes]
- 5.21Coding decoding [Practice problem]
- Programming LogicTCS-NQT Instructions : You will be given 10 Question with a time limit of 15 mins | Questions - 10 | Time - 15 mins6
- CodingTCS-NQT Instructions : You will be given 2 Question with a time limit of 15 mins and 30 mins | Questions - 2 | Time - 15 mins + 30 mins16
- 7.1Introduction to java
- 7.2[Programs] Introduction To Java
- 7.3Flow Control
- 7.4[Programs] Flow Control
- 7.5[Programs] Pyramids & Patterns
- 7.6Arrays
- 7.7[Programs] Arrays
- 7.8Java Methods, Class & Object
- 7.9[Programs] Java Methods, Class & Object
- 7.10Strings, Recursion, Modifiers & Keywords
- 7.11[Programs] Strings, Recursion, Modifiers & Keywords
- 7.12Object-Oriented Programming System
- 7.13[Programs] Object Oriented Programming System
- 7.14Exception Handling & Files
- 7.15[Program] Exception Handling & Files
- 7.16TCS-NQT Mock Coding Questions
- Previous Year Paper and Solution2

Introduction To Java

Introduction To Java
Programming languages enable humans to write instructions that a computer can perform. With
precise instructions, computers coordinate applications and systems that run the modern world.
Sun Microsystems released the Java programming language in 1995. Java is known for being
simple, portable, secure, and robust. Though it was released over twenty years ago, Java
remains one of the most popular programming languages today.
One reason people love Java is the Java Virtual Machine, which ensures the same Java code
can be run on different operating systems and platforms. Sun Microsystems’ slogan for Java
was “write once, run everywhere”.
Programming languages are composed of syntax, the specific instructions which Java
understands. We write syntax in files to create programs, which are executed by the computer
to perform the desired task.
Let’s start with the universal greeting for a programming language.

Hello World In Java
A "Hello, World!" is a simple program that outputs Hello, World! on the screen. Since it's a very
simple program, it's often used to introduce a new programming language to a newbie.
Let's explore how Java "Hello, World!" program works.
Program:
// Your First Program
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
"Hello, World!" Program Working
// Your First Program
In Java, any line starting with // is a comment. Comments are intended for users reading the
code to understand the intent and functionality of the program. It is completely ignored by the
Java compiler (an application that translates a Java program to Java bytecode that a computer
can execute). To learn more, visit Java comments.
class HelloWorld { ... }
In Java, every application begins with a class definition. In the program, HelloWorld is the name
of the class, and the class definition is:

class HelloWorld {
}
For now, just remember that every Java application has a class definition, and the name of the
class should match the filename in Java.
class HelloWorld {
}
This is the main method. Every application in Java must contain the main method. The Java
compiler starts executing the code from the main method.
For now, just remember that the main function is the entry point of your Java application, and it's
mandatory in a Java program. The signature of the main method in Java is:
public static void main(String[] args) {
}
System.out.println("Hello, World!");
The code above is a print statement. It prints the text Hello, World! to standard output (your
screen). The text inside the quotation marks is called String in Java.

Java JDK, JRE and JVM
JVM (Java Virtual Machine)
JVM is an engine that provides a runtime environment to drive the Java Code or applications. It
converts Java bytecode into machine language. JVM is a part of Java Run Environment (JRE).
It cannot be separately downloaded and installed. To install JVM, you need to install JRE. The
full form of JVM is Java Virtual Machine.
In many other programming languages, the compiler produces machine code for a specific
system. However, Java compiler produces code for a virtual machine which is called a JVM.
Features:
1. It enables you to run applications in a cloud environment or in your device.
2. Java Virtual Machine converts byte code to the machine-specific code.
3. It provides basic java functions like memory management, security, garbage collection,
and more.
4. JVM runs the program by using libraries and files given by Java Runtime Environment.
5. JDK and JRE both contain Java Virtual Machine.
6. It can execute the java program line by line hence it is also called as interpreter.
7. JVM is easily customizable for example, you can allocate minimum and maximum
memory to it.

8. It is independent from hardware and the operating system. So, you can write a java
program once and run anywhere.
JRE (Java Runtime Environment)
JRE is a piece of a software which is designed to run other software. It contains the class
libraries, loader class, and JVM. In simple terms, if you want to run Java program you need
JRE.
If you are not a programmer, you don’t need to install JDK, but just JRE to run Java programs.
Though, all JDK versions comes bundled with Java Runtime Environment, so you do not need
to download and install the JRE separately in your PC. The full form of JRE is Java Runtime
Environment.
Features:
1. Java Runtime Environment is a set of tools using which the JVM actually runs.
2. JRE contains deployment technology, including Java Web Start and Java Plug-in.
3. Developers can easily run the source code in JRE, but he/she cannot write and compile
the Java program.

4. It includes integration libraries like Java Database Connectivity (JDBC), Remote Method
Invocation (RMI), Java Naming and Directory Interface (JNDI), and more.
5. JRE has a JVM and Java HotSpot virtual machine client.
JDK(Java Development Kit)
JDK is a software development environment used for making applets and Java applications.
The full form of JDK is Java Development Kit. Java developers can use it on Windows, macOS,
Solaris, and Linux. JDK helps them to code and run Java programs. It is possible to install more
than one JDK version on the same computer.
Features:
1. It enables you to handle multiple extensions in a single catch block.
2. JDK includes all features that JRE has.
3. It contains development tools such as a compiler, debugger, etc.
4. JDK provides the environment to develop and execute Java source code.
5. It can be installed on Windows, Unix, and Mac operating systems.

Overview Architecture
Variables & Literals In Java
Java Variables
A variable is a location in memory (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier).
Create Variables in Java
Here's how we create a variable in Java,
int speedLimit = 80;
Here, speedLimit is a variable of int data type and we have assigned value 80 to it.
The int data type suggests that the variable can only hold integers.
In the example, we have assigned value to the variable during declaration. However, it's not
mandatory.

You can declare variables and assign variables separately. For example,
int speedLimit;
speedLimit = 80;
Note: Java is a statically-typed language. It means that all variables must be declared before
they can be used.
Change values of variables
The value of a variable can be changed in the program, hence the name variable. For example,
int speedLimit = 80;
speedLimit = 90;
Here, initially, the value of speedLimit is 80. Later, we changed it to 90.
However, we cannot change the data type of a variable in Java within the same scope.
What is the variable scope?
Don't worry about it for now. Just remember that we can't do something like this:
int speedLimit = 80;
float speedLimit;
This is not allowed
Rules for Naming Variables in Java
Java programming language has its own set of rules and conventions for naming variables.
Here's what you need to know:
1. Java is case sensitive. Hence, age and AGE are two different variables. For example,

int age = 24;
int AGE = 25;
System.out.println(age);
// prints 24
System.out.println(AGE);
// prints 25
2. Variables must start with either a letter or an underscore, _ or a dollar, $ sign. For example,
int age;
// valid name and good practice
int _age;
// valid but bad practice
int $age;
// valid but bad practice
3. Variable names cannot start with numbers. For example,
int 1age;
// invalid variables
4. Variable names can't use whitespace. For example,
int my age;
// invalid variables
5. Here, we need to use variable names having more than one word, use all lowercase letters
for the first word and capitalize the first letter of each subsequent word.
For example, myAge.
6. When creating variables, choose a name that makes sense.
For example, score, number, level makes more sense than variable names such as s, n, and l.
If you choose one-word variable names, use all lowercase letters. For example, it's better to use
speed rather than SPEED, or sPEED.
There are 4 types of variables in Java programming language:
● Instance Variables (Non-Static Fields)
● Class Variables (Static Fields)
● Local Variables
● Parameters

Java literals
Literals are data used for representing fixed values. They can be used directly in the code. For
example,
int a = 1;
float b = 2.5;
char c = 'F';
Here, 1, 2.5, and 'F' are literals.
Here are different types of literals in Java.
1. Boolean Literals
In Java, boolean literals are used to initialize boolean data types. They can store two values:
true and false. For example,
boolean flag1 = false;
boolean flag2 = true;
Here, false and true are two boolean literals.
2. Integer Literals
An integer literal is a numeric value(associated with numbers) without any fractional or
exponential part. There are 4 types of integer literals in Java:
1. binary (base 2)
2. decimal (base 10)
3. octal (base 8)
4. hexadecimal (base 16)
For example:

// binary
int binaryNumber = 0b10010;
// octal
int octalNumber = 027;
// decimal
int decNumber = 34;
// hexadecimal
int hexNumber = 0x2F; // 0x represents hexadecimal
// binary
int binNumber = 0b10010; // 0b represents binary
In Java, binary starts with 0b, octal starts with 0, and hexadecimal starts with 0x.
Note: Integer literals are used to initialize variables of integer types like byte, short, int, and long.
3. Floating-point Literals
A floating-point literal is a numeric literal that has either a fractional form or an exponential form.
For example,
class Main {
public static void main(String[] args) {
double myDouble = 3.4;
float myFloat = 3.4F;
// 3.445*10^2
double myDoubleScientific = 3.445e2;
System.out.println(myDouble);
// prints 3.4
System.out.println(myFloat);
// prints 3.4
System.out.println(myDoubleScientific);
// prints 344.5
}
}
Note: The floating-point literals are used to initialize float and double type variables.

4. Character Literals
Character literals are unicode characters enclosed inside single quotes. For example,
char letter = 'a';
Here, a is the character literal.
We can also use escape sequences as character literals. For example,
\b (backspace)
\t (tab)
\n (new line)
5. String literals
A string literal is a sequence of characters enclosed inside double-quotes. For example,
String str1 = "Adwait";
String str2 = "LastMomentTuitions";
Here, Adwait and LastMomentTuitions are two string literals.
Java Data Types (Primitive)
Java Data Types
As the name suggests, data types specify the type of data that can be stored inside variables in
Java. Java is a statically-typed language. This means that all variables must be declared before
they can be used.
int speed;
Here, speed is a variable, and the data type of the variable is int.
The int data type determines that the speed variable can only contain integers.There are 8 data
types predefined in Java, known as primitive data types.
Note: In addition to primitive data types, there are also referenced types (object type).

8 Primitive Data Types
boolean type
1. The boolean data type has two possible values, either true or false.
2. Default value: false.
3. They are usually used for true/false conditions.
4.
Example 1: Java boolean data type
class Main {
public static void main(String[] args) {
boolean flag = true;
System.out.println(flag);
// prints true
}
}
byte type
● The byte data type can have values from -128 to 127 (8-bit signed two's complement
integer).
● If it's certain that the value of a variable will be within -128 to 127, then it is used instead
of int to save memory.
● Default value: 0
Example 2: Java byte data type
class Main {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range);
// prints 124
}
}

short type
● The short data type in Java can have values from -32768 to 32767 (16-bit signed two's
complement integer).
● If it's certain that the value of a variable will be within -32768 and 32767, then it is used
instead of other integer data types (int, long).
● Default value: 0
●
Example 3: Java short data type
class Main {
public static void main(String[] args) {
short temperature;
temperature = -200;
System.out.println(temperature);
// prints -200
}
}
int type
● The int data type can have values from -231 to 231-1 (32-bit signed two's complement
integer).
● If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a
minimum value of 0 and a maximum value of 232-1.
● Default value: 0
Example 4: Java int data type
class Main {
public static void main(String[] args) {
int range = -4250000;
System.out.println(range);
// print -4250000
}
}
long type
● The long data type can have values from -263 to 263-1 (64-bit signed two's complement
integer).

● If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum
value of 0 and a maximum value of 264-1.
● Default value: 0
●
Example 5: Java long data type
class LongExample {
public static void main(String[] args) {
long range = -42332200000L;
System.out.println(range);
// prints -42332200000
}
}
Notice the use of L at the end of -42332200000. This represents that it's an integer of the long
type.
double type
● The double data type is a double-precision 64-bit floating-point.
● It should never be used for precise values such as currency.
● Default value: 0.0 (0.0d)
Example 6: Java double data type
class Main {
public static void main(String[] args) {
double number = -42.3;
System.out.println(number);
// prints -42.3
}
}
float type
● The float data type is a single-precision 32-bit floating-point.
● It should never be used for precise values such as currency.
● Default value: 0.0 (0.0f)
Example 7: Java float data type

class Main {
public static void main(String[] args) {
float number = -42.3f;
System.out.println(number);
// prints -42.3
}
}
Notice that we have used -42.3f instead of -42.3in the above program. It's because -42.3 is a
double literal.
To tell the compiler to treat -42.3 as float rather than double, you need to use f or F.
char type
● It's a 16-bit Unicode character.
● The minimum value of the char data type is '\u0000' (0) and the maximum value of the is
'\uffff'.
● Default value: '\u0000'
●
Example 8: Java char data type
class Main {
public static void main(String[] args) {
char letter = '\u0051';
System.out.println(letter);
// prints Q
}
}
Here, the Unicode value of Q is \u0051. Hence, we get Q as the output.
Here is another example:
class Main {
public static void main(String[] args) {
char letter1 = '9';
System.out.println(letter1);
// prints 9
char letter2 = 65;
System.out.println(letter2);
// prints A

}
}
Here, we have assigned 9 as a character (specified by single quotes) to the letter1 variable.
However, the letter2 variable is assigned 65 as an integer number (no single quotes).
Hence, A is printed to the output. It is because Java treats characters as an integer and the
ASCII value of A is 65.
String type
Java also provides support for character strings via java.lang.String class. Strings in Java are
not primitive types. Instead, they are objects. For example,
String myString = "Java Programming";
Here, myString is an object of the String class.
Java Operators
Operators are symbols that perform operations on variables and values. For example, + is an
operator used for addition, while * is also an operator used for multiplication.
Operators in Java can be classified into 5 types:
1. Arithmetic Operators
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Unary Operators
6. Bitwise Operators
Java Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For
example,

a + b;
Here, the + operator is used to add two variables a and b. Similarly, there are various other
arithmetic operators in Java.
Example 1: Arithmetic Operators
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 5;
// addition operator
System.out.println("a + b = " + (a + b));
// subtraction operator
System.out.println("a - b = " + (a - b));
// multiplication operator
System.out.println("a * b = " + (a * b));
// division operator
System.out.println("a / b = " + (a / b));
// modulo operator
System.out.println("a % b = " + (a % b));

}
}
Output
a + b = 17
a - b = 7
a * b = 60
a / b = 2
a % b = 2
In the above example, we have used +, -, and * operators to compute addition, subtraction, and
multiplication operations.
/ Division Operator
Note the operation, a / b in our program. The / operator is the division operator.
If we use the division operator with two integers, then the resulting quotient will also be an
integer. And, if one of the operands is a floating-point number, the result will also be in
floating-point.
(9 / 2) is 4
(9.0 / 2) is 4.5
(9 / 2.0) is 4.5
(9.0 / 2.0) is 4.5
% Modulo Operator
The modulo operator % computes the remainder. When a = 7 is divided by b = 4, the remainder
is 3.
Note: The % operator is mainly used with integers.
Java Assignment Operators
Assignment operators are used in Java to assign values to variables. For example,

int age;
age = 5;
Here, = is the assignment operator. It assigns the value on its right to the variable on its left.
That is, 5 is assigned to the variable age.
Let's see some more assignment operators available in Java.
Example 2: Assignment Operators
class Main {
public static void main(String[] args) {
// create variables
int a = 4;
int var;
// assign value using =
var = a;
System.out.println("var using =: " + var);
// assign value using =+
var += a;
System.out.println("var using +=: " + var);
// assign value using =*

var *= a;
System.out.println("var using *=: " + var);
}
}
Output
var using =: 4
var using +=: 8
var using *=: 32
Java Relational Operators
Relational operators are used to check the relationship between two operands. For example,
// check if a is less than b
a < b;
Here, < operator is the relational operator. It checks if a is less than b or not.
It returns either true or false.

Example 3: Relational Operators
class Main {
public static void main(String[] args) {
// create variables
int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b);
// false
// != operator
System.out.println(a != b);
// true
// > operator
System.out.println(a > b);
// false
// < operator
System.out.println(a < b);
// true
// >= operator
System.out.println(a >= b);
// false
// <= operator
System.out.println(a <= b);
// true
}
}
Note: Relational operators are used in decision making and loops.
Java Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in
decision making.

Example 4: Logical Operators
class Main {
public static void main(String[] args) {
// && operator
System.out.println((5 > 3) && (8 > 5));
// true
System.out.println((5 > 3) && (8 < 5));
// false
// || operator
System.out.println((5 < 3) || (8 > 5));
// true
System.out.println((5 > 3) || (8 < 5));
// true
System.out.println((5 < 3) || (8 < 5));
// false
// ! operator
System.out.println(!(5 == 3));
// true
System.out.println(!(5 > 3));
// false
}
}
Examples
●
(5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true.
●
(5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false.
●
(5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true.
●
(5 > 3) && (8 > 5) returns true because the expression (5 > 3) is true.
●
(5 > 3) && (8 > 5) returns false because both (5 < 3) and (8 < 5) are false.
●
!(5 == 3) returns true because 5 == 3 is false.
●
!(5 > 3) returns false because 5 > 3 is true.

Java Unary Operators
Unary operators are used with only one operand. For example, ++ is a unary operator that
increases the value of a variable by 1. That is, ++5 will return 6.
Different types of unary operators are:
Increment and Decrement Operators
Java also provides increment and decrement operators: ++ and -- respectively. ++ increases the
value of the operand by 1, while -- decreases it by 1. For example,
int num = 5;
// increase num by 1
++num;
Here, the value of num gets increased to 6 from its initial value of 5.
Example 5: Increment and Decrement Operators
class Main {
public static void main(String[] args) {
// declare variables
int a = 12, b = 12;

int result1, result2;
// original value
System.out.println("Value of a: " + a);
// increment operator
result1 = ++a;
System.out.println("After increment: " + result1);
System.out.println("Value of b: " + b);
// decrement operator
result2 = --b;
System.out.println("After decrement: " + result2);
}
}
Output
Value of a: 12
After increment: 13
Value of b: 12
After decrement: 11
In the above program, we have used the ++ and -- operator as prefixes (++a, --b). We can also
use these operators as postfix (a++, b++).
There is a slight difference when these operators are used as prefix versus when they are used
as a postfix.
Java Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits. For example,
Bitwise complement Operation of 35
35 = 00100011 (In Binary)
~ 00100011
________
11011100
= 220 (In decimal)
Here, ~ is a bitwise operator. It inverts the value of each bit (0 to 1 and 1 to 0).

The various bitwise operators present in Java are:
Other operators
Besides these operators, there are other additional operators in Java.
Java instanceof Operator
The instanceof operator checks whether an object is an instanceof a particular class. For
example,
class Main {
public static void main(String[] args) {
String str = "Programiz";
boolean result;
// checks if str is an instance of
// the String class
result = str instanceof String;
System.out.println("Is str an object of String? " + result);
}
}

Output
Is str an object of String? true
Here, str is an instance of the String class. Hence, the instanceof operator returns true.
Java Ternary Operator
The ternary operator (conditional operator) is shorthand for the if-then-else statement. For
example,
variable = Expression ? expression1 : expression2
Here's how it works.
● If the Expression is true, expression1 is assigned to the variable.
● If the Expression is false, expression2 is assigned to the variable.
Let's see an example of a ternary operator.
class Java {
public static void main(String[] args) {
int februaryDays = 29;
String result;
// ternary operator
result = (februaryDays == 28) ? "Not a leap year" : "Leap year";
System.out.println(result);
}
}
Output
Leap year
In the above example, we have used the ternary operator to check if the year is a leap year or
not.

Java Basic Input and Output
Java Output
In Java, you can simply use
System.out.println(); or
System.out.print(); or
System.out.printf();
to send output to standard output (screen).
Here,
● System is a class
● out is a public static field: it accepts output data.
Don't worry if you don't understand it. We will discuss class, public, and static in later chapters.
Let's take an example to output a line.
class AssignmentOperator {
public static void main(String[] args) {
System.out.println("Java programming is interesting.");
}
}
Output:
Java programming is interesting.
Here, we have used the println() method to display the string.
Difference between println(), print() and printf()
1. print() - It prints string inside the quotes.
2. println() - It prints string inside the quotes similar like print() method. Then the cursor
moves to the beginning of the next line.
3. printf() - It provides string formatting (similar to printf in C/C++ programming).

Example: print() and println()
class Output {
public static void main(String[] args) {
System.out.println("1. println ");
System.out.println("2. println ");
System.out.print("1. print ");
System.out.print("2. print");
}
}
Output:
1. println
2. println
1. print 2. print
Example: Printing Variables and Literals
class Variables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println(5);
System.out.println(number);
}
}
When you run the program, the output will be:
5

-10.6
Here, you can see that we have not used the quotation marks. It is because to display integers,
variables and so on, we don't use quotation marks.
Example: Print Concatenated Strings
class PrintVariables {
public static void main(String[] args) {
Double number = -10.6;
System.out.println("I am " + "awesome.");
System.out.println("Number = " + number);
}
}
Output:
I am awesome.
Number = -10.6
In the above example, notice the line,
System.out.println("I am " + "awesome.");
Here, we have used the + operator to concatenate (join) the two strings: "I am " and
"awesome.".
And also, the line,
System.out.println("Number = " + number);
Here, first the value of the variable number is evaluated. Then, the value is concatenated to the
string: "Number = ".
Java Input
Java provides different ways to get input from the user. However, in this tutorial, you will learn to
get input from user using the object of Scanner class.
In order to use the object of Scanner, we need to import java.util.Scanner package.

import java.util.Scanner;
Then, we need to create an object of the Scanner class. We can use the object to take input
from the user.
// create an object of Scanner
Scanner input = new Scanner(System.in);
// take input from the user
int number = input.nextInt();
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
System.out.println("You entered " + number);
// closing the scanner object
input.close();
}
}
Output:
Enter an integer: 23
You entered 23
In the above example, we have created an object named input of the Scanner class. We then
call the nextInt() method of the Scanner class to get an integer input from the user.
Similarly, we can use nextLong(), nextFloat(), nextDouble(), and next() methods to get long,
float, double, and string input respectively from the user.
Note: We have used the close() method to close the object. It is recommended to close the
scanner object once the input is taken.
import java.util.Scanner;

class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Getting float input
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);
// Getting double input
System.out.print("Enter double: ");
double myDouble = input.nextDouble();
System.out.println("Double entered = " + myDouble);
// Getting String input
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);
}
}
Output:
Enter float: 2.343
Float entered = 2.343
Enter double: -23.4
Double entered = -23.4
Enter text: Hey!
Text entered = Hey!
Java Expressions
A Java expression consists of variables, operators, literals, and method calls. To know more
about method calls, visit Java methods.
For example,
int score;
score = 90;
Here, score = 90 is an expression that returns an int. Consider another example,

Double a = 2.2, b = 3.4, result;
result = a + b - 3.4;
Here, a + b - 3.4 is an expression.
if (number1 == number2)
System.out.println("Number 1 is larger than number 2");
Here, number1 == number2 is an expression that returns a boolean value. Similarly, "Number 1
is larger than number 2" is a string expression.
Java Statements
In Java, each statement is a complete unit of execution. For example,
int score = 9*5;
Here, we have a statement. The complete execution of this statement involves multiplying
integers 9 and 5 and then assigning the result to the variable score.
In the above statement, we have an expression 9 * 5. In Java, expressions are part of
statements.
Expression statements
We can convert an expression into a statement by terminating the expression with a ;. These
are known as expression statements. For example,
// expression
number = 10
// statement
number = 10;
In the above example, we have an expression number = 10. Here, by adding a semicolon (;), we
have converted the expression into a statement (number = 10;).
Consider another example,
// expression
++number
// statement
++number;
Similarly, ++number is an expression whereas ++number; is a statement.

Declaration Statements
In Java, declaration statements are used for declaring variables.
For example,
Double tax = 9.5;
The statement above declares a variable tax which is initialized to 9.5.
Note: There are control flow statements that are used in decision making and looping in Java.
You will learn about control flow statements in later chapters.
Java Blocks
A block is a group of statements (zero or more) that is enclosed in curly braces { }. For example,
class Main {
public static void main(String[] args) {
String band = "Beatles";
if (band == "Beatles") { // start of block
System.out.print("Hey ");
System.out.print("Jude!");
} // end of block
}
}
Output:
Hey Jude!
In the above example, we have a block if {
}.
Here, inside the block we have two statements:
● System.out.print("Hey ");
● System.out.print("Jude!");
However, a block may not have any statements. Consider the following examples,
class Main {
public static void main(String[] args) {

if (10 > 5) { // start of block
} // end of block
}
}
This is a valid Java program. Here, we have a block if {...}. However, there is no any statement
inside this block.
class AssignmentOperator {
public static void main(String[] args) {
// start of block
} // end of block
}
Here, we have block public static void main() {...}. However, similar to the above example, this
block does not have any statement.
Java Comments
In this tutorial, you will learn about Java comments, why we use them, and how to use
comments in the right way.
In computer programming, comments are a portion of the program that are completely ignored
by Java compilers. They are mainly used to help programmers to understand the code.
For example,
// declare and initialize two variables
int a =1;
int b = 3;
// print the output
System.out.println("This is output");
Here, we have used the following comments,
● declare and initialize two variables
● print the output
Types of Comments in Java
In Java, there are two types of comments:

● single-line comment
● multi-line comment
Single-line Comment
A single-line comment starts and ends in the same line. To write a single-line comment, we can
use the // symbol.
For example,
// "Hello, World!" program example
class Main {
public static void main(String[] args) {
{
// prints "Hello, World!"
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
Here, we have used two single-line comments:
● "Hello, World!" program example
● prints "Hello World!"
The Java compiler ignores everything from // to the end of line. Hence, it is also known as End
of Line comment.
Multi-line Comment
When we want to write comments in multiple lines, we can use the multi-line comment. To write
multi-line comments, we can use the /*
*/ symbol. For example,
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
class HelloWorld {
public static void main(String[] args) {
{
System.out.println("Hello, World!");

}
}
Output:
Hello, World!
Here, we have used the multi-line comment:
/* This is an example of multi-line comment.
* The program prints "Hello, World!" to the standard output.
*/
This type of comment is also known as Traditional Comment. In this type of comment, the Java
compiler ignores everything from /* to */.