Chapter 3 : Java Variables and Operators
Java - Variable Introduction
Variables ये memory locations के नाम होते है | जो values; variables को दी जाती हैं,
वो उस location पर store हो जाती है |
Syntax for Variable Declaration
data_type_name variable_name; //or
data_type_name variable_name1, variable_name2;
For Example,
int a;
int b, c;
Syntax for Variable Definition
data_type variable_name = variable_value;
For Example,
int a = 5;
int b = 10, c = 15;
Java - Types of Variables
Java के लिए Variables के तीन प्रकार होते है -
1. Local Variable
2. Instance Variable
3. Static Variable
1. Local Variable
Local Variables block, methods और constructor के अन्दर होते है |
Local Variable का scope; local होता है | ये ससर्फ methods और constructor के
अन्दर visible होते है |
जब Local Variables; methods और constructor के बाहर जाते है, तब destroyed हो
जाते है |
Source Code :
class Sample
void display(){
int a = 5; //Local Variable
System.out.println("Value of a : " + a);
public static void main(String arg[]){
Sample s = new Sample();
s.display();
Output :
Value of a : 5
2. Instance Variable
Instance Variables; class के अन्दर होते है और methods और constructor के बाहर
होते है |
Instance Variables non-static variables होते है |
Source Code :
class Sample{
int a = 5; //Instance Variable
void display(){
System.out.println("Value of a : " + a);
public static void main(String arg[]){
Sample s = new Sample();
s.display();
Output :
Value of a : 5
3. Static Variable
Static Variables को Class Variables भी कहते है |
ये Instance Variable के तरह class के अन्दर और methods और constructor के बाहर
होते है |
'static' keyword के साथ इनका इस्तेमाि ककया जाता है |
Source Code :
class Sample{
static int a = 5; //Static Variable
void display(){
System.out.println("Value of a : " + a);
public static void main(String arg[]){
Sample s = new Sample();
s.display();
Output :
Value of a : 5
Java Operators
Arithmetic Operators
Operators Explaination
+ (Addition) ये दो Operands को add करता है |
- (Subtraction) ये right operand से left operand को लनकाि देता है |
* (Multiplication) ये दो Operands को multiply करता है |
/ (Division) ये right operand द्वारा left operand को divide करता है |
ये right operand द्वारा left operand को divide करके
% (Modulus)
remainder लनकािता है |
For eg.
Source Code :
//Sample.java
class Sample{
public static void main(String args[]) {
int a = 10, b = 5, c;
c=a + b;
System.out.println("Addition of a and b is " + c);
c=a - b;
System.out.println("Subtraction of a and b is " + c);
c=a * b;
System.out.println("Multiplication of a and b is " + c);
c=a / b;
System.out.println("Division of a and b is " + c);
c=a % b;
System.out.println("Remainder of a and b is " + c);
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
एक Operand की value दूसरे Operand से कम हो तो ये true
<</strong> (less
return करता है | for eg. num1=5; num2=6;
than)
num1 < num2
एक Operand की value दूसरे Operand से ज्यादा हो तो ये true
> (greater than) return करता है | for eg. num1=6; num2=5;
num1 > num2
एक Operand की value दूसरे Operand से कम हो या बराबर
<= (less than or (equal) हो तो ये true return करता है | for eg. num1=5;
equal to) num2=5;
num1 <= num2
एक Operand की value दूसरे Operand से ज्यादा हो या बराबर
>= (greater than or (equal) हो तो ये true return करता है | for eg. num1=5;
equal to) num2=5;
num1 >= num2
दो Operands जब बराबर(equal) होते है, तब ये true return करता
== (equal to)
है |
दो Operands जब एक-दूसरे से अिग होते है, तब ये true return
!= (not equal to)
करता है |
For eg.
Source Code :
//Sample.java
class Sample
public static void main(String args[]){
int a = 6, b = 5;
if(a < b){
System.out.println("a is less than b");
else{
System.out.println("a is greater than b");
if(a <= b){
System.out.println("a is less than b");
else{
System.out.println("a is greater than b");
if(a > b){
System.out.println("a is greater than b");
else{
System.out.println("a is less than b");
if(a >= b){
System.out.println("a is greater than b");
else{
System.out.println("a is less than b");
if(a == b){
System.out.println("a is equal to b");
else{
System.out.println("a is not equal to b");
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
अगर दोनों conditions true हो तो ये true return करेगा |
&& (logical &&)
for eg. (5<6) && (6>5)
अगर दोनों में से एक भी true है , तो ये true return करेगा |
|| (logical OR)
for eg. (5<6) || (6>5)
अगर condition true हो तो ये उसे false कर देता है |
! (logical not) for eg. !((5<6) && (6>5))
!((5<6) || (6>5))
For eg.
Source Code :
//Sample.java
class Sample{
public static void main(String args[]){
if((5 < 6) && (6 > 5)){
System.out.println("Condition is true.");
else{
System.out.println("Condition is false.");
if((5 < 6) || (6 > 5)){
System.out.println("Condition is true.");
}
else{
System.out.println("Condition is false.");
if(!((5 < 6) && (5 > 6))){
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
Truth Table for &, |, ^
a b a&b a|b a^b
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)
Decimal Value Binary Value
अगर a = 20, b = 12 हो तो,
20 00010100
12 00001100
4 00000100
Operation on OR(a|b)
Decimal Value Binary Value
अगर a = 20, b = 12 हो तो,
20 00010100
12 00001100
28 00011100
Operation on XOR(a^b)
Decimal Value Binary Value
अगर a = 20, b = 12 हो तो,
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 हो जायेगा |
Complement Operator (~)
ये Operator सारे bit reverse करता है |
ये Operator 0 को 1 कर देता है और 1 को 0 कर देता है |
Operation on Complement( ~ )
Decimal Value Binary Value
~12 00001100
243 11110011
यहााँ पर Output -13 आने के बजाय 243 आया ऐसा क्यों ?
2's Complement of 243 -(reverse of 243 in binary + 1)
Operation on 2's Complement( ~ )
Decimal Value Binary Value 2's Complement
243 1111 0011 -(0000 1100+1) = -(0000 1101) = -13(output)
For eg.
Source Code :
class Sample{
public static void main(String args[]){
int a=20; /* 0001 0100 */
int b=12; /* 0000 1100 */
int c;
c=a&b;
System.out.println("value of c is " + c); /* 4 = 0000 0100 */
c=a|b;
System.out.println("value of c is " + c); /* 28 = 0001 1100 */
c=a^b;
System.out.println("value of c is " + c); /* 24 = 0001 1000 */
c=a<<2;
System.out.println("value of c is " + c); /* 80 = 0101 0000 */
c=a>>2;
System.out.println("value of c is " + c); /* 5 = 0000 0101 */
}
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
Assignment Operators ग्यारह प्रकार के होते है –
Operators Examples
= (assignment) c=a+b
+= (add assignment) c += a same as c = c + a
-= (subtract assignment) c -= a same as c = c - a
*= (multiply assignment) c *= a same as c = c * a
/= (divide assignment) c /= a same as c = c / a
%= (modulus assignment) c %= a same as c = c % a
&= (AND assignment) c &= a same as c = c & a
|= (OR assignment) c |= a same as c = c | a
^= (XOR assignment) c ^= a same as c = c ^ a
<<= (Left Shift assignment) c <<= a same as c = c << a
>>= (Right Shift assignment) c >>= a same as c = c >> a
For eg.
Source Code :
class Sample{
public static void main(String args[]){
int a=20, b=12;
b = a + b;
System.out.println("value of b is " + b);
b += a;
System.out.println("value of b is " + b);
b -= a;
System.out.println("value of b is " + b);
b *= a;
System.out.println("value of b is " + b);
b /= a;
System.out.println("value of b is " + b);
b %= a;
System.out.println("value of b is " + b);
b &= 2;
System.out.println("value of b is " + b);
b |= 2;
System.out.println("value of b is " + b);
b ^= 2;
System.out.println("value of b is " + b);
b <<= 2;
System.out.println("value of b is " + b);
b >>= 2;
System.out.println("value of b is " + b);
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
Increment Operator (++) ये variable की value 1 से बढ़ा देता है |
Decrement Operator (--) ये variable की value 1 से घटा देता है |
Operators Same as
++a (Increment Prefix) a=a+1
--a (Decrement Prefix) a=a-1
a++ (Increment Postfix)
a-- (Decrement Postfix)
for eg.
Source Code :
class Sample{
public static void main(String args[]){
int a=20;
System.out.println("Print Value with prefix : " + ++a);
// increase value with increment prefix
System.out.println("Value of a : " + a);
System.out.println("Print Value with prefix : " + --a);
// decrease value with decrement prefix
System.out.println("Value of a : " + a);
System.out.println("Print Value with postfix : " + a++); // increase value with
increment postfix
System.out.println("Value of a : " + a);
System.out.println("Print Value with postfix : " + a--); // decrease value with
decrement postfix
System.out.println("Value of a : " + a);
Output
Print Value with prefix : 21
Value of a : 21
Print Value with prefix : 20
Value of a : 20
Print Value with postfix : 20
Value of a : 21
Print Value with postfix : 21
Value of a : 20
Conditional Operators
Conditional Operator में तीन Expressions होते है |
Conditional Operator को Ternary Operator भी कहते है |
Conditional Operator में अगर पहिा expression true होता है, तो वो दूसरा
expression output में print करता है |
अगर Conditional Operator में पहिा expression false होता है, तो वो तीसरा
expression output में print करता है |
Syntax for Conditional / Ternary Operator
expression1 ? expression 2 : expression 3
for eg.
Source Code :
class Sample
public static void main(String args[]){
int a = 100, b ;
b = ( a == 100 ? 2 : 0 ) ;
System.out.println("Value of a is " + a);
System.out.println("Value of b is " + b);
Output :
Value of a is 100
Value of b is 2