Arithmetic, Assignment, Comparison, Logical, and Bitwise operators in Java
1. Arithmetic Operators
Perform basic mathematical operations.
● Operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulus).
Example:
int a = 10, b = 5;
[Link](a + b); // Output: 15
[Link](a - b); // Output: 5
[Link](a * b); // Output: 50
[Link](a / b); // Output: 2
[Link](a % b); // Output: 0
2. Assignment Operators
Assign or update the value of a variable.
● Operators: =, +=, -=, *=, /=, %=.
Example:
int a = 10;
a += 5; // Equivalent to a = a + 5
[Link](a); // Output: 15
a *= 2; // Equivalent to a = a * 2
[Link](a); // Output: 30
3. Comparison (Relational) Operators
Compare two values and return a boolean (true or false).
● Operators: == (equal to), != (not equal to), < (less than), > (greater than), <= (less
than or equal to), >= (greater than or equal to).
Example:
int a = 10, b = 20;
[Link](a < b); // Output: true
[Link](a >= b); // Output: false
[Link](a == b); // Output: false
[Link](a != b); // Output: true
4. Logical Operators
Operate on boolean values.
● Operators: && (logical AND), || (logical OR), ! (logical NOT).
Example:
boolean x = true, y = false;
[Link](x && y); // Output: false
[Link](x || y); // Output: true
[Link](!x); // Output: false
5. Bitwise Operators
Operate on bits of integer values.
● Operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), <<
(left shift), >> (right shift), >>> (unsigned right shift).
Example:
int a = 5, b = 3; // Binary: a = 0101, b = 0011
[Link](a & b); // Output: 1 (Binary: 0001)
[Link](a | b); // Output: 7 (Binary: 0111)
[Link](a ^ b); // Output: 6 (Binary: 0110)
[Link](~a); // Output: -6 (Inverts bits)
[Link](a << 1); // Output: 10 (Binary: 1010)
[Link](a >> 1); // Output: 2 (Binary: 0010)
❖ Control Statements In Java (Juss Like Python Remember The simple codes)
Control statements in Java are used to control the flow of execution based on conditions or
loops. They can be broadly categorized into three types: Decision-making, Looping, and
Branching statements.
1. Decision-Making Statements
These statements allow the program to take decisions based on a condition.
a) if Statement :- Executes a block of code if a specified condition is true.
int a = 10;
if (a > 5) {
[Link]("a is greater than 5");
b) if-else Statement:- Executes one block of code if the condition is true, otherwise
executes another block.
int a = 10;
if (a < 5) {
[Link]("a is less than 5");
} else {
[Link]("a is greater than or equal to 5");
c) if-else-if :- Checks multiple conditions sequentially.
int marks = 85;
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 80) {
[Link]("Grade B");
} else {
[Link]("Grade C");
}
d) switch Statement:- Allows selection of one among multiple blocks of code based
on a variable's value.
int day = 3;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid day");
2. Looping Statements :- These repeat a block of code as long as the
condition is true.
a) for Loop:- Executes a block of code a fixed number of times.
for (int i = 1; i <= 5; i++) {
[Link](i);
}
b) while Loop:- Executes a block of code while a condition is true.
int i = 1;
while (i <= 5) {
[Link](i);
i++;
c) do-while Loop:- Executes a block of code at least once, and then continues while
the condition is true.
int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
3. Branching Statements :- These are used to alter the normal flow of
control.
a) break Statement:- Exits a loop or switch block prematurely.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
[Link](i);
// Output: 1 2
b) continue Statement :- Skips the current iteration of a loop and moves to the next
iteration.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
[Link](i);
// Output: 1 2 4 5
c) return Statement:- Exits from a method and optionally returns a value.
public int add(int a, int b) {
return a + b; // Returns the sum of a and b
These control statements work together to define the logic and flow of a Java program.
❖ What are the differences between object method and static method
❖ write about java command line arguments write same with an java example program
Java Command Line Arguments
Command-line arguments in Java are inputs passed to the program at the time of execution.
These arguments are passed to the main method as a String array (String[] args).
Each argument is separated by a space and stored as a string in the array.
Features of Command Line Arguments
1. They allow the user to provide inputs when running the program.
2. The arguments are always passed as strings. If numeric values are needed, they
must be converted.
3. The array args stores the arguments sequentially, starting from index 0.
Here’s a simple Java program to demonstrate the use of command-line arguments:
Example with Numeric Conversion
Execution
java AddNumbers 10 20
Output
Sum of 10 and 20 is: 30
❖ Tokens or Elemnts in java
Tokens are the building blocks of a Java program. These include:
● Keywords: Reserved words.
● Identifiers: Names for variables, methods, classes.
● Literals: Constants like numbers, strings.
● Operators: Symbols for operations.
● Separators: Characters to structure the program.
● Comments: Explanations for code.
Each token contributes to writing a structured and functional Java program.
Types of Tokens
1. Keywords
○ Reserved words in Java with special meanings.
○ Examples: class, if, else, while, return.
Example:
if (true) {
[Link]("Hello!");
2. Identifiers
○ Names given to variables, methods, classes, etc.
○ Must start with a letter, $, or _.
Example:
int age = 25; // 'age' is an identifier
○
3. Literals
○ Fixed values in the program.
○ Types:
■ Numbers: 10, 3.14
■ Characters: 'A'
■ Strings: "Java"
■ Booleans: true, false
Example:
String name = "John"; // "John" is a string literal
○
4. Operators
○ Symbols used for operations.
○ Examples: +, -, *, /, ==, &&.
Example:
int sum = 5 + 10; // '+' is an operator
○
5. Separators
○ Characters used to structure the program.
○ Examples: {, }, (, ), ;.
Example:
public class Test {
public static void main(String[] args) {
[Link]("Hello");
}
○
6. Comments
○ Notes in the code, ignored by the compiler.
○ Types:
■ Single-line: //
■ Multi-line: /* */
Example:
// This is a comment
○ /* This is a multi-line comment */
—---------------------------------------------------------
❖ Inheritance
Inheritance in Java
Inheritance is a fundamental concept in Java that allows a class to
acquire properties and methods of another class. It promotes code
reusability and establishes a relationship between classes.
● Base Class (Parent Class): The class whose properties are
inherited.
● Derived Class (Child Class): The class that inherits from the
base class.
From types of inheritance visit this following link
[Link]
Draw the fig present in this link and use these below points as the
definitions to it
● Single Inheritance: One class inherits from another.
● Multilevel Inheritance: A chain of inheritance.
● Hierarchical Inheritance: Multiple classes inherit from the
same class.
● Multiple Inheritance: Achieved through interfaces, as Java
doesn't allow multiple inheritance with classes.
And write the examples present in that link.
—---------------------------------------------------------------
❖ What is Method Overriding in Java?
Method overriding is a feature in Java that allows a subclass to
provide a specific implementation of a method that is already
defined in its parent class. The method in the subclass should have
the same name, return type, and parameters as the method in the
parent class.
The purpose of method overriding is to provide the specific
implementation of a method in the child class while maintaining the
same method signature.
Key Points:
● The method in the child class must have the same signature
(name, return type, and parameters) as the method in the
parent class.
● Method overriding happens at runtime (dynamic polymorphism).
● You can override a method in the child class if the method in
the parent class is not declared as final, static, or private.
Syntax
Example Program:
❖ What is a Constructor in Java?
A constructor in Java is a special type of method used to initialize
objects. It is called when an instance of an object is created.
Constructors have the same name as the class and do not have a
return type (not even void).
Key Points:
1. Initialization: Constructors are used to set initial values
for object attributes.
2. Automatic Invocation: A constructor is automatically invoked
when a new object of the class is created.
3. No Return Type: Constructors do not return any value.
4. Same Name as Class: The constructor must have the same name as
the class.
Types of Constructors:
1. Default Constructor (No-argument constructor)
2. Parameterized Constructor (Constructor with arguments)
Syntax:
1. Default Constructor:
A default constructor is provided automatically by Java if no
constructors are defined by the user. It initializes objects with
default values.
class MyClass {
int x;
// Default constructor (provided automatically if no constructor
is defined)
MyClass() {
x = 10; // initializing the value of x
}
2. Parameterized Constructor:
A parameterized constructor allows passing values to initialize an
object at the time of creation.
class MyClass {
int x;
// Parameterized constructor
MyClass(int value) {
x = value; // initializing the value of x with passed value
Example