Search Here

Operators In java

 

Operators In java 



Operator एक symbol होते हैं , हम कह सकते हैं की Operators को कोई  operation को perform करने के लिए use करते हैं।
Java Operators Types

Java में different - different action perform करने के लिए different - different Operators है । Java में normally use होने वाले Operators कुछ इस प्रकार हैं -

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison / Relational Operators

  4. Logical Operators

  5. Increment / Decrement Operator

Java Arithmetic Operators

Arithmetic Operators simple calculation में use होने wale Operators होते हैं जैसे Addition , Subtraction etc.

Operator

Name

Example

Explanation

+

Addition

x+y

( + ) plus operator uses to add two or more numbers / values

-

Subtraction

x-y

( -  ) minus  operator uses to substract one  numbers / values from another numbers / values or finds the difference 

/

Division

x / y

quotient of x and y with decimal point.

*

multiplication

x * y

product of x and y

%

Modulus

x % y

remainder of operands

File : Operators.java

class Operators 

{

  public static void main(String[] args) 

  {

    int a = 7;

    int b = 89;

    // here = is a assignment operator.


    System.out.println(a+b);

    // here + is arithmetic operator.

  }

}

Output

javac Operators.java

java Operators

96


Java Assignment Operators

Assignment Operator को equals to = से represent करते हैं , जो कि value को किसी variable में assign करने के लिए use किया जाता है। हालाँकि इसे Arithmetic Operators के साथ भी use करते हैं। नीचे दिए गए Example में आप देख सकते हैं।

Operator

Name

Example

Explanation

=

Assign

x = y

value of y assigned to x

!=

Assign

x = y

value of y assigned to x

+=

Addition then Assign

x += y

First add and assign it to x. (It is same as x = x+y )

- =

Subtract then Assign

x -= y

get the difference and assign it to x. (It is same as x = x-y )





/ =

Divide then assign quotient

x /= y

quotient of x and y then assign it to x . (It is same as x = x/y )

* =

Multiplication then assign the product

x * =y

product of x and y then assign it to x. (It is same as x = x*y )

% =

Divide then assign remainder

x %= y

remainder of operands then assign it to x. (It is same as x = x+%y )


Java Assignment Operator Example

class AssignmentOperator

{

  public static void main(String[] args) 

  {

    // add then assign.

    int a = 90;

    a += 10;

    System.out.println(a);    

    // devide 100 by 2 and assign.

    a /= 2;

    System.out.println("Quotient : "+  a);    

    // get module deviding 50 by 11 then assign.

    a %= 11;

    System.out.println("Modulas : "+  a);

  }

}

Output

javac AssignmentOperator.java

java AssignmentOperator

100

quotient : 50

Modulas : 6


Java Incrementing-Decrementing Operators

Incrementing-Decrementing Operators को किसी variable को 1 से increase या decrease करने के लिए use किया जाता है। हालाँकि इसमें Addition और Subtraction operation ही होते हैं , इसलिए इसमें ( ++ ) या ( -- ) sign ही use होते हैं।

Operator

Name

Explanation

++a

Pre Increment

first increment by 1 then return the value of a

a++

post Increment

first return the value of a then increment by 1.

--a

Pre Decrement

first decrement by 1 then return the value of a

a--

Post Decrement

first return the value of a then decrement by 1.

Java Incrementing-Decrementing Operator Example

class AssignmentOperator

{

  public static void main(String[] args) 

  {

    int a = 90;

    System.out.println("Initial value : "+a);

    // increment by 1

    a++;

    System.out.println("After increment : "+a);

    

    // decrement by 1

    a--;

    System.out.println("After decrement : "+a);

  }

}

Output

javac AssignmentOperator.java

java AssignmentOperator

Initial value : 90

After increment : 91

After decrement : 90

Java Comparison Operators

Comparison Operators दी हुई दो values को compare करके condition के according boolean value true / false value return करते हैं . Java में Comparison Operators को कुछ इस तरह से use कर सकते हैं।

Operator

Name

Example

Explanation

==

Equal

x = = y

checks if x is equal to y.

!=

Not equal

x !=y

checks if x is not equal to y.

<

Less than

x < y

checks if x is less than y.

>

Greater than

x > y

checks if x is greater than y.

<=

Less than or equal

x <= y

checks either x is less than or equal to y.

>=

Greater than or equal

x >= y

checks either x is greater than or equal to y.

public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x == y); // returns false because 5 is not equal to 3

  }

}

false

public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x != y); // returns true because 5 is not equal to 3

  }

}


True

public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x > y); // returns true because 5 is greater than 3

  }

}


public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x < y); // returns false because 5 is not less than 3

  }

}

public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x >= y); // returns true because 5 is greater, or equal, to 3

  }

}

public class Main {

  public static void main(String[] args) {

    int x = 5;

    int y = 3;

    System.out.println(x <= y); // returns false because 5 is neither less than or equal to 3

  }

}


Java Logical Operators

Logical Operators , एक या एक से अधिक expression के according Boolean value return करते हैं। जैसे -

Operator

Name

Example

Explanation

&&

And

x && y

returns True if Both operands (x and y) are true;

!

Not

!x

Returns True if x is not true;

||

Or

x || y

returns True if either x or y is true;


public class Main {

  public static void main(String[] args) {

    int x = 5;

    System.out.println(x > 3 && x < 10); // returns true because 5 is greater than 3 AND 5 is less than 10

  }

}

public class Main {

  public static void main(String[] args) {

    int x = 5;

    System.out.println(x > 3 || x < 4); // returns true because one of the conditions are true (5 is greater than 3, but 5 is not less than 4)

  }

}

public class Main {

  public static void main(String[] args) {

    int x = 5;

    System.out.println(!(x > 3 && x < 10)); // returns false because ! (not) is used to reverse the result

  }

}


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.