Java Variables and Operators
Java Variables and Operators
For Example,
int a;
int b, c;
For Example,
int a = 5;
1. Local Variable
2. Instance Variable
3. Static Variable
1. Local Variable
Source Code :
class Sample
void display(){
s.display();
Output :
Value of a : 5
2. Instance Variable
class Sample{
void display(){
s.display();
Output :
Value of a : 5
3. Static Variable
Source Code :
class Sample{
void display(){
System.out.println("Value of a : " + a);
s.display();
Output :
Value of a : 5
Java Operators
Arithmetic Operators
Operators Explaination
For eg.
Source Code :
//Sample.java
class Sample{
public static void main(String args[]) {
int a = 10, b = 5, c;
c=a + b;
c=a - b;
c=a * b;
c=a / b;
c=a % b;
Output
Addition of a and b is 15
Subtraction of a and b is 5
Multiplication of a and b is 50
Division of a and b is 2
Remainder of a and b is 0
Relational Operators
Operators Explaination
Source Code :
//Sample.java
class Sample
int a = 6, b = 5;
else{
else{
else{
System.out.println("a is less than b");
else{
if(a == b){
else{
Output
a is greater than b
a is greater than b
a is greater than b
a is greater than b
a is not equal to b
Logical Operators
Operators Explaination
For eg.
Source Code :
//Sample.java
class Sample{
System.out.println("Condition is true.");
else{
System.out.println("Condition is false.");
System.out.println("Condition is true.");
}
else{
System.out.println("Condition is false.");
System.out.println("Condition is true.");
else{
System.out.println("Condition is false.");
Output
Condition is true.
Condition is true.
Condition is true.
Bitwise Operators
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Operation on AND(a&b)
20 00010100
12 00001100
4 00000100
Operation on OR(a|b)
20 00010100
12 00001100
28 00011100
Operation on XOR(a^b)
20 00010100
12 00001100
24 00011000
Binary Left Shift( << ) and Right Shift( >> )
Left Shift(<<) for e.g. a=20; /* 0001 0100 */ a << 2 में numeric value के binary
value में हर binary number को 2 binary numbers left side से shift करता है | for
e.g.a=20; /* 0001 0100 */ तो इसका 0101 0000 मतिब 80 हो जायेगा |
Right Shift(>>) for e.g. a=20; /* 0001 0100 */ ये Left shift से कबिकुि उिट है |
Right Shift a>> 2 में numeric value के binary value में हर binary number को 2
binary numbers right side से shift करता है | for e.g.a=20; /* 0001 0100 */ तो
इसका 0000 0101 मतिब 5 हो जायेगा |
Operation on Complement( ~ )
~12 00001100
243 11110011
class Sample{
int c;
c=a&b;
c=a|b;
c=a^b;
c=a<<2;
c=a>>2;
}
Output
value of c is 4
value of c is 28
value of c is 24
value of c is 80
value of c is 5
Assignment Operators
Operators Examples
= (assignment) c=a+b
class Sample{
b = a + b;
b += a;
b -= a;
b *= a;
b /= a;
b %= a;
b &= 2;
b ^= 2;
b <<= 2;
b >>= 2;
Output
value of b is 32
value of b is 52
value of b is 32
value of b is 640
value of b is 32
value of b is 12
value of b is 0
value of b is 2
value of b is 0
value of b is 0
value of b is 0
Increment and Decrement Operators
Operators Same as
for eg.
Source Code :
class Sample{
int a=20;
Output
Value of a : 21
Value of a : 20
Value of a : 21
Value of a : 20
Conditional Operators
Conditional Operator में अगर पहिा expression true होता है, तो वो दूसरा
expression output में print करता है |
अगर Conditional Operator में पहिा expression false होता है, तो वो तीसरा
expression output में print करता है |
Syntax for Conditional / Ternary Operator
for eg.
Source Code :
class Sample
int a = 100, b ;
b = ( a == 100 ? 2 : 0 ) ;
Output :
Value of a is 100
Value of b is 2