OPERATORS
Arithmetic Operators:
Operator |
Meaning |
Example |
+ |
Addition |
3+4 |
- |
Subtraction |
5-7 |
* |
Multiplication |
5*5 |
/ |
Division |
14/7 |
% |
Modulus |
20%7 |
SIMPLE
ARITHMETIC:
File name: ArithmeticTest.java
class ArithmeticTest{
public static void main (String args[]){
short x=6;
int y=4;
float a=12.5f;
float b=7f;
System.out.println("x is"+x+",y is"+y);
System.out.println("x+y="+(x+y));
System.out.println("x-y="+(x-y));
System.out.println("x*y="+(x*y));
System.out.println("x/y="+(x/y));
System.out.println("x%y="+(x%y));
System.out.println ("a is"+a+"b is"+b);
System.out.println("a/b="+(a/b));
}
}
The
Output appears as given below:
The assignment operators used in C and C++ are also used in
Java. A selection of assignment
operators is given.
Expression |
Meaning |
x+=y |
x=x+y |
x-=y |
x=x-y |
x*=y |
x=x*y |
x/=y |
x=x/y |
x=y |
x=y |
Example:
File name: AssignDemo.java
class AssignDemo{
public static void main (String args[]){
int a=1;
int b=2;
int c=3;
a+=5;
b*=4;
c+=a*b;
c%=6;
System. out.println("a="+a);
System. out.println("b="+b);
System. out.println("c="+c);
}
}
Output:
Incrementing and Decrementing :
File name: PrePostFixTest.java
class PrePostFixTest{
public static void main(String args[]){
int x=10;
System.out.println("-------------------------------------------");
System.out.println("Initial value of X:"+x);
System.out.println("X value in Println:"+x++);
System.out.println("After Post increment X:"+x);
System.out.println("-------------------------------------------");
x=10;
System.out.println("Initial value of X:"+x);
System.out.println("X value in Println:"+ ++x);
System.out.println("After Post increment X:"+x);
System.out.println("-------------------------------------------");
}
}
The output appeares as given below:
Comparisons Or
Relational Operator
Relational
operator
operator |
Meaning |
example |
== |
equal |
x == 3 |
!= |
not equal |
x != 3 |
< |
less than |
x<3 |
> |
greater than |
x>3 |
<= |
less than or equal to |
x<=3 |
>= |
greater than or equal
to |
x>=3 |
File name:Comparison.java
class Comparison{
public static void main (String args[]){
int x=6;
int y=4;
System.out.println("x is"+x+",y is"+y);
System.out.println("x<y="+(x<y));
System.out.println("x<=y="+(x<=y));
System.out.println("x>y="+(x>=y));
System.out.println("x==y="+(x==y));
System.out.println("x!=y="+(x!=y));
}
}
Logical Operator
AND,OR,XOR,NOT are the logical operator available
in java.
File name: Logical.java
class Logical{
public static void main (String args[]){
int x=6;
int y=4;
System.out.println((x==6)&&(y==3));
System.out.println((x==6)||(y==3));
System.out.println(!(x<y));
}
}
Output
Bitwise Operator
operator |
meaning |
~ |
bitwise complement |
& |
bitwise AND |
| |
bitwise OR |
^ |
bitwise XOR |
<< |
left shift |
>> |
right shift |
A |
B |
A|B |
A&B |
A^B |
~A |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
Example :
File name: BitwiseDemo.java
class BitwiseDemo
{
public static void main(String args[])
{
int x=10;
int y=20;
int z=30;
x=x | 40;
y>>=1;
z<<=2;
System.out.println("x | 40 :"
+x);
System.out.println("y>>1
:" +y);
System.out.println("z<<2
:" +z);
// reinitialize to its original value.
x=10;
z=30;
x=x^z;
System.out.println("x^z:" +z);
}
}
Output
String-concatenation
operator
Concatenation operator (+)
system.out.println("I"+"love"+"BGMI");
output of the above line is :
I love BGMI
File name: Concatenation.java
class Concatenation{
public static void main(String args[]){
System.out.println("I "+"
love "+" India ");
}
}
Output
Comments
eg 1
//this is an single line comment
/*this is
is an
multiline
comment*/
eg 2
/**
*this example demonstrates
documentation comments.
/**
* this class doesn't contains any
methods except main.
*/
0 comments:
Post a Comment