CH-2 Theory All and Program
CH-2 Theory All and Program
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 calculation and functions be it logical, arithmetic, relational etc. They
are classified based on the functionality they provide. Here are a few types:
Addition(+): This operator is a binary operator and is used to add two operands.
Subtraction(-): This operator is a binary operator and is used to subtract two operands.
class Subtraction {
public static void main(String[] args)
{
int num1 = 20, num2 = 10, sub = 0;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
sub = num1 - num2;
System.out.println("Subtraction = " + sub);
}
} 1.8 Variable name in java OR Naming in java:
Output: In variable name, use only character (A to Z, a to z) ,digit (0 to 9) and
num1 = 20 underscores ‘_’
num2 = 10 Variable name cannot include with space character.
Subtraction = 10 Variable name does no begin with digit.
Variable name always start with characters.
Upper case and lower case count as different variables.
Keywords not allows as a variable name.
Multiplication(*): This operator is a binary operator and is used to multiply two operands.
class Multiplication {
public static void main(String[] args)
{
int num1 = 20, num2 = 10, mult = 0;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
mult = num1 * num2;
System.out.println("Multiplication = " + mult);
}
}
Output:
num1 = 20
num2 = 10
Multiplication = 200
Division(/): This is a binary operator that is used to divide the first operand(dividend) by the
second operand(divisor) and give the quotient as result.
class Division {
public static void main(String[] args)
{
int num1 = 20, num2 = 10, div = 0;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
div = num1 / num2;
System.out.println("Division = " + div);
}
}
Output:
num1 = 20
num2 = 10
Division = 2
Modulus(%): This is a binary operator that is used to return the remainder when the first
operand(dividend) is divided by the second operand(divisor).
class Modulus {
public static void main(String[] args)
{
int num1 = 5, num2 = 2, mod = 0;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
mod = num1 % num2;
System.out.println("Remainder = " + mod);
}
}
Output: num1 = 5
num2 = 2
1.11 Keyword in java
Keywords are important part of java. Java language has number of keywords reserved. Let’s
we see it below table.
abstract long import transient
char throw protected native
catch private class null
boolean package throws const
default static byte new
finally break else case
do double float extends
implements this Final int
if volatile public instanceof
return interface short assert
try void continue const
for while goto
Switch synchronized super
class ArithmeticOp
{
public static void main(String[] args)
{
int a,b,add,sub,mul,div,mod,avg;
a=16;
b=3;
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
avg=(a+b)/2;
System.out.println("addition =" +add);
System.out.println("subtraction =" +sub);
System.out.println("multiplication =" +mul);
System.out.println("division =" +div);
System.out.println("modules =" +mod);
System.out.println("average =" +avg);
}
}
Output:
addition =19
subtraction =13
multiplication =48
division =5
modules =1
average =9
COMPUTER – CLASS-IX
Java Assignment Operator
These operators are used to assign values to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment
operator is a value.
1. “=”: This is the simplest assignment operator which is used to assign the value
on the right to the variable on the left. This is the basic definition of assignment
operator and how does it functions.
class Assignment {
public static void main(String[] args)
{
int num;
String name;
num = 10;
name = "CJCS";
System.out.println("num is assigned: " + num);
System.out.println("name is assigned: " + name);
}
}
Output:
num is assigned: 10
name is assigned: CJCS
2. “+=”: This operator is a compound of ‘+’ and ‘=’ operators. It operates by
adding the current value of the variable on left to the value on the right and then
assigning the result to the operand on the left.
class Assignment {
public static void main(String[] args)
{
int num1 = 10, num2 = 20; " + num1);
System.out.println("num2 = " + num2);
num1 += num2;
System.out.println("num1 = " + num1);
}
}
Output:
num1 = 10
num2 = 20
num1 = 30
3. “-=”: This operator is a compound of ‘-‘ and ‘=’ operators. It operates by
subtracting the value of the variable on right from the current value of the variable on
the left and then assigning the result to the operand on the left.
class Assignment {
public static void main(String[] args)
{
int num1 = 10, num2 = 20;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
num1 -= num2;
System.out.println("num1 = " + num1);
}
}
Output:
num1 = 10
num2 = 20
num1 = -10
4. “*=”: This operator is a compound of ‘*’ and ‘=’ operators. It operates by
multiplying the current value of the variable on left to the value on the right and then
assigning the result to the operand on the left.
class Assignment {
public static void main(String[] args)
{
int num1 = 10, num2 = 20;
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
num1 *= num2;
System.out.println("num1 = " + num1);
}
}
Output:
num1 = 10
num2 = 20
num1 = 200
class Relational {
public static void main(String[] args)
{
int var1 = 5, var2 = 10, var3 = 5;
System.out.println("Var1 = " + var1);
System.out.println("Var2 = " + var2);
System.out.println("Var3 = " + var3);
System.out.println("var1 == var2: "+ (var1 == var2));
System.out.println("var1 == var3: "+ (var1 == var3));
}
}
Output:
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: false
var1 == var3: true
2. ‘Not equal to’ Operator(!=): This operator is used to check whether the two given operands are
equal or not. It functions opposite to that of the equal-to operator. It returns true if the operand at
the left-hand side is not equal to the right-hand side, else false.
class Relational {
public static void main(String[] args)
{
int var1 = 5, var2 = 10, var3 = 5;
Output:
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: true
var1 == var3: false
3. ‘Greater than’ operator(>): This checks whether the first operand is greater than the second
operand or not. The operator returns true when the operand at the left-hand side is greater than the
right-hand side.
class Relational {
public static void main(String[] args)
{
int var1 = 30, var2 = 20, var3 = 5;
Output:
Var1 = 30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false
4. ‘Less than’ Operator(<): This checks whether the first operand is less than the second operand or
not. The operator returns true when the operand at the left-hand side is less than the right-hand
side. It functions opposite to that of the greater than operator.
class Relational {
public static void main(String[] args)
{
int var1 = 10, var2 = 20, var3 = 5;
Output:
Var1 = 10
Var2 = 20
Var3 = 5
var1 < var2: true
var2 < var3: false
5. 'Greater than or equal to' operator(>=): This checks whether the first operand is greater than or
equal to the second operand or not. The operator returns true when the operand at the left-hand
side is greater than or equal to the right-hand side.
class Relational {
public static void main(String[] args)
{
int var1 = 20, var2 = 20, var3 = 10;
Output:
Var1 = 20
Var2 = 20
Var3 = 10
var1 >= var2: true
var2 >= var3: false
6. 'Less than or equal to' Operator(<): This checks whether the first operand is less than or equal to
the second operand or not. The operator returns true when the operand at the left-hand side is less
than or equal to the right-hand side.
class Relational {
public static void main(String[] args)
{
int var1 = 10, var2 = 10, var3 = 9;
class Logical {
public static void main(String[] args)
{
int a = 10, b = 20, c = 20, d = 0;
Output:
Var1 = 10
Var2 = 20
Var3 = 20
The sum is: 50
2. 'Logical OR' Operator(||): This operator returns true when one of the two conditions under
consideration are satisfied or are true. If even one of the two yields true, the operator results true.
To make the result false, both the constraints need to return false.
class Logical {
public static void main(String[] args)
{
int a = 10, b = 1, c = 10, d = 30;
System.out.println("Var1 = " + a);
System.out.println("Var2 = " + b);
System.out.println("Var3 = " + c);
System.out.println("Var4 = " + d);
if (a > b || c == d)
System.out.println("One or both"+ " the conditions are true");
else
System.out.println("Both the"+ " conditions are false");
}
}
Output:
Var1 = 10
Var2 = 1
Var3 = 10
Var4 = 30
One or both the conditions are true
3. 'Logical NOT' Operator(!): Unlike the previous two, this is a unary operator and returns true when
the condition under consideration is not satisfied or is a false condition. Basically, if the condition is
false, the operation returns true and when the condition is true, the operation returns false.
class Logical {
public static void main(String[] args)
{
int a = 10, b = 1;
################################################################
Bitwise operators in Java
Bitwise OR (|) –
This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e,
if either of the bits is 1, it gives 1, else it gives 0.
For example,
a = 5 = 0101 (In Binary)
b = 7 = 0111 (In Binary)
~ 0101
________
1010 = 10 (In decimal)
Note – Compiler will give 2’s complement of that number, i.e., 2’s compliment of 10 will be -6.
public class operators {
public static void main(String[] args)
{
Examples
int a = 5;
int b = 7;
System.out.println("a&b = " + (a & b));
System.out.println("a|b = " + (a | b));
System.out.println("a^b = " + (a ^ b));
System.out.println("~a = " + ~a);
a &= b;
System.out.println("a= " + a);
}
}
Output :
a&b = 5
a|b = 7
a^b = 2
~a = -6
a= 5
Chapter 1
Bitwise operators modify variables considering the bit patterns that represent the values they store.
operator description Unary complement (bit
~
& Bitwise AND inversion)
| Bitwise inclusive OR << Shift bits left
^ Bitwise exclusive OR(XOR) >> Shift bits right
Example 2: bitwise OR
class Demobitwiseor Description:
{ a=10 and b=7 first convert into binary
public static void main(String[] args) value.
{ 00001010 this is 10 value binary
int a,b,c; 00000111 this is 5 value binary
a=10;
b=7; Now OR operator apply to those above
c=a | b; binary value. And we will get value of
System.out.println("ans is =" +c); c variable.
} 00001010
} 00000111
OR 00001111 this value is convert
into decimal
answer is C=15
Output:
ans is =15
__________________________________________________________________________________
Chapter 1
c=a ^ b; B 00000111
System.out.println("ans is =" +c); XOR (c) 00001101
} So after perform XOR operation, the
} result is assign in to variable C, so here
c variables value becomes 14.
C=13.
Output:
ans is =13
__________________________________________________________________________________
Chapter 1
c = (a>b)? a: b;
System.out.println("maximum number is=" +c);
}
}
Output:
maximum number is=10
The increase operator (++) and the decrease operator (--) increase or reduce by one
the value stored in a variable. They are equivalent to +=1 and to -=1, respectively.
Example 1 Example 2
x = 3; x = 3;
y = ++x; y = x++;
// x contains 4, y contains 4 // x contains 4, y contains 3
Example 1:
// demo of prefix-increment and postfix- increment operator
class helloprogram
{
public static void main(String[] args)
{
int a,b,c;
a=10; //a=10, b=?, c=?
a++; //a=11, b=?,c=?
System.out.println("value of a="+a);
b=a++; //a=12, b=11,c=?
c=++a; //a=13, b=11,c=13?
System.out.println("value of a="+a); //a=13 print
System.out.println("value of b="+b); //b=11 print
System.out.println("value of c="+c); //c= 13 print
}
}
Output:
value of a=11
value of a=13
value of b=11
value of c=13
Example 2:
// demo of prefix-decrement and postfix- decrement operator
class helloprogram
{
public static void main(String[] args)
{
int a,b,c;
a=10; //a=10, b=? , c=?
a--; //a=9, b=? , c=?
__________________________________________________________________________________
Chapter 1
System.out.println("value of a="+a);
b=a--; //a=8, b=9, c=?
c=--a; //a=7, b=9, c=7?
System.out.println("value of a="+a); //a=7 print
System.out.println("value of b="+b); //b=9 print
System.out.println("value of c="+c); //c=7 print
}
}
Output:
value of a=9
value of a=7
value of b=9
value of c=7
__________________________________________________________________________________
COMPUTER – CLASS-IX
PRACTICE PROGRAMS (DO PRACTICE INTO ROUGH COPY)
Output:
num1 + num2: 120
num1 - num2: 80
num1 * num2: 2000
num1 / num2: 5
num1 % num2: 0
num2 = num1;
System.out.println("= Output: "+num2);
num2 += num1;
System.out.println("+= Output: "+num2);
num2 -= num1;
System.out.println("-= Output: "+num2);
num2 *= num1;
System.out.println("*= Output: "+num2);
num2 /= num1;
System.out.println("/= Output: "+num2);
num2 %= num1;
System.out.println("%= Output: "+num2);
}
}
Output:
= Output: 10
+= Output: 20
-= Output: 10
*= Output: 100
/= Output: 10
%= Output: 0
Output:
num1 and num2 are not equal
num1 and num2 are not equal
num1 is not greater than num2
num1 is less than num2
num1 is less than num2
num1 is less than or equal to num2
6) Bitwise Operators There are six bitwise Operators: &, |, ^, ~, <<, >>
Example of Bitwise Operators
public class BitwiseOperatorDemo {
public static void main(String args[]) {
int num1 = 11; /* 11 = 00001011 */
int num2 = 22; /* 22 = 00010110 */
int result = 0;
result = ~num1;
System.out.println("~num1: "+result);
7) Ternary Operator
variable num1 = (expression) ? value if true : value if false
class AreaOfCircle
{
public static void main(String args[])
{
//Area = (width*height)/2
double area=(b*h)/2;
System.out.println("Area of Triangle is: " + area);
}
}
double area=l*b;
System.out.println("Area of Rectangle is: " + area);
}
}
double a= s.nextDouble();
double b= s.nextDouble();
double area=(b/4)*Math.sqrt((4*a*a)-(b*b));
double area=(d1*d2) ;
System.out.println("Area of Parallelogram is: " + area);
}
}
double area=(d1*d2)/2;
System.out.println("Area of Rhombus is: " + area);
}
}
What is operator precedence?
The operator precedence represents how two expressions are bind together. In an
expression, it determines the grouping of operators with operands and decides how an
expression will evaluate.
While solving an expression two things must be kept in mind the first is a precedence and
the second is associativity.
Precedence
Precedence is the priority for grouping different types of operators with their operands.
It is meaningful only if an expression has more than one operator with higher or lower
precedence. The operators having higher precedence are evaluated first. If we want to
evaluate lower precedence operators first, we must group operands by using parentheses
and then evaluate.
Associativity
We must follow associativity if an expression has more than two operators of the same
precedence. In such a case, an expression can be solved either left-to-right or right-to-
left, accordingly.
1. 1 + 5 * 3
You might be thinking that the answer would be 18 but not so. Because the multiplication
(*) operator has higher precedence than the addition (+) operator. Hence, the expression
first evaluates 5*3 and then evaluates the remaining expression i.e. 1+15. Therefore, the
answer will be 16.
1. x + y * z / k
System.out.println("Name: "+name);
System.out.println("Gender: "+gender);
System.out.println("Roll No: "+roll);
System.out.println("Mobile Number: "+mobileNo);
System.out.println("HSC Percent: "+percent);
if(bn==true) {
System.out.println("You Can Vote");
}
else if(bn==false) {
System.out.println("You Can't Vote");
}
}
}
Basic Math methods
Method Description
Math.pow() It returns the value of first argument raised to the power to second argument.
Math.ceil() It is used to find the smallest integer value that is greater than or equal to the argument or
mathematical integer.
Math.floor() It is used to find the largest integer value which is less than or equal to the argument and is equal
to the mathematical integer of a double value.
Provided by java.lang.Math class, Math.PI constant is used to carry out multiple mathematical and
scientific calculations like finding the area & circumference of a circle or the surface area and volume
of a sphere.
Math.E The Math.E property represents Euler's number, the base of natural logarithms, e, which is
approximately 2.718.
𝙼𝚊𝚝𝚑.𝙴 = e ≈ 2.718
Example-1
int i = 7;
int j = -9;
double x = 72.3;
double y = 0.34;
// The absolute value of a number is equal to the number if the number is positive or zero and equal
to the negative of the number if the number is negative.
// The "ceiling" of a number is the smallest integer greater than or equal to the number. Every integer
is its own //ceiling.
// The "floor" of a number is the largest integer less than or equal to the number.
// Comparison operators
Output: