Java provides a rich operator environment. The operators are mainly divided into the following four categories.
1. arithmetic
2. bitwise
3. relational
4. logical
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
The basic arithmetic operators (+,-,*, /) works in the universal style for all numeric values.
N.B. Division operator while applied on integer values no fractional part will be available.
Example BlueJ
class Mod
{
private int x;
private double y;
public void show ()
{
x=13;
y=15.5;
System. out.println( “ X mod by 10=” + x%10);
System. out.println( “ Y
mod by 10=” + y%10);
}
}
Output:
X mod by 10=3
Y mod by 10=5.5
Arithmetic Assignment Operator in BlueJ
Java provides special operators to combine an arithmetic operator with an assignment operator. Assignment operator is used to assign a value or the result of an expression to a variable. All of us know the use of '=' operator. When we use the statement int a=10, this means that the assignment operator will assign the value at its right side on the variable at its left side. In addition to that Java offers a set of shorthand arithmetic assignment operators
. Consider the example: a=a+b;
The above statement adds 'b' to 'a' and assigns the result on 'a’. We can write the above statement as: a+=b; both a=a+b and a+=b are same. Similarly we can use - =, * =, / =, % =
Increment and Decrement Operators in Java
The ++ and – are java’s increment and decrement operators. Let int a=10; if we want to increase ‘a’ by, we can write a=a+1; or a+=1;
The same statement can be written as a++ or ++a. In same style we can write --a or a-- to decrease the value of a by 1. These are called increment and decrement operators.
When the operator is used before the variable, it is called prefix operator and when used after the operator, it is postfix operator.
While used in a single statement, prefix and postfix operator means the same – increment or decrement by 1 but in expression they differ. If postfix operator is used in an expression, the expression will be executed first and after that the effect of the operator will take place. In case of prefix operator, the operator will act first and then the expression.
Example
class Incre
{
private int a=10;
public void show()
{
System. out.println(“ After postfix expression=” + a++);
System. out.println(“ Now a=” + a);
System. out.println(“ After prefix expression=” + ++a);
System. out.println(“ Now a=” + a);
}
}
output
After postfix expression=10
Now a=11
After prefix expression=12
Now a=12
1. arithmetic
2. bitwise
3. relational
4. logical
Arithmetic operators in Java
+ Addition- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/= Division assignment
%= Modulus assignment
-- Decrement
The basic arithmetic operators (+,-,*, /) works in the universal style for all numeric values.
N.B. Division operator while applied on integer values no fractional part will be available.
Modulus / modulo operator
The Modulus operator, % returns the remainder of a division operation. In Java, Modulus operator can be applied to floating point types as well as integer types. (In C and C++, Modulus operator is applicable to integer values only.)Example BlueJ
class Mod
{
private int x;
private double y;
public void show ()
{
x=13;
y=15.5;
System. out.println( “ X mod by 10=” + x%10);
System. out.println( “ Y
mod by 10=” + y%10);
}
}
Output:
X mod by 10=3
Y mod by 10=5.5
Arithmetic Assignment Operator in BlueJ
Java provides special operators to combine an arithmetic operator with an assignment operator. Assignment operator is used to assign a value or the result of an expression to a variable. All of us know the use of '=' operator. When we use the statement int a=10, this means that the assignment operator will assign the value at its right side on the variable at its left side. In addition to that Java offers a set of shorthand arithmetic assignment operators
. Consider the example: a=a+b;
The above statement adds 'b' to 'a' and assigns the result on 'a’. We can write the above statement as: a+=b; both a=a+b and a+=b are same. Similarly we can use - =, * =, / =, % =
Increment and Decrement Operators in Java
The ++ and – are java’s increment and decrement operators. Let int a=10; if we want to increase ‘a’ by, we can write a=a+1; or a+=1;
The same statement can be written as a++ or ++a. In same style we can write --a or a-- to decrease the value of a by 1. These are called increment and decrement operators.
When the operator is used before the variable, it is called prefix operator and when used after the operator, it is postfix operator.
While used in a single statement, prefix and postfix operator means the same – increment or decrement by 1 but in expression they differ. If postfix operator is used in an expression, the expression will be executed first and after that the effect of the operator will take place. In case of prefix operator, the operator will act first and then the expression.
Example
class Incre
{
private int a=10;
public void show()
{
System. out.println(“ After postfix expression=” + a++);
System. out.println(“ Now a=” + a);
System. out.println(“ After prefix expression=” + ++a);
System. out.println(“ Now a=” + a);
}
}
output
After postfix expression=10
Now a=11
After prefix expression=12
Now a=12
No comments:
Post a Comment