KEMBAR78
Operators Examples | PDF | Computer Engineering | Software Development
0% found this document useful (0 votes)
3 views4 pages

Operators Examples

The document provides an overview of basic arithmetic, assignment, comparison, and logical operators in Java, including examples for each operator. It covers operations like addition, subtraction, multiplication, division, and modulus, as well as increment and decrement. Additionally, it explains comparison operators for value comparison and logical operators for evaluating conditions.

Uploaded by

Sajmal Sajmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Operators Examples

The document provides an overview of basic arithmetic, assignment, comparison, and logical operators in Java, including examples for each operator. It covers operations like addition, subtraction, multiplication, division, and modulus, as well as increment and decrement. Additionally, it explains comparison operators for value comparison and logical operators for evaluating conditions.

Uploaded by

Sajmal Sajmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

+ Addition Adds together two values x+y

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x + y);

- Subtraction Subtracts one value from another x-y

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x - y);

* Multiplication Multiplies two values x*y

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 3;

System.out.println(x * y);

/ Division Divides one value by another x/y

public class Main {

public static void main(String[] args) {

int x = 12;

int y = 3;

System.out.println(x / y);

}
}

% Modulus Returns the division remainder x % y

public class Main {

public static void main(String[] args) {

int x = 5;

int y = 2;

System.out.println(x % y);

++ Increment Increases the value of a variable by 1 ++x

public class Main {

public static void main(String[] args) {

int x = 5;

++x;

System.out.println(x);

-- Decrement Decreases the value of a variable by 1 --x

public class Main {

public static void main(String[] args) {

int x = 5;

--x;

System.out.println(x);

}
A list of all assignment operators:

Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

Java Comparison Operators

Comparison operators are used to compare two values:

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Java Logical Operators

Logical operators are used to determine the logic between variables or values:

Operator Name Description Example

&& Logical and Returns true if both statements are true x < 5 && x < 10

|| Logical or Returns true if one of the statements is true x < 5 || x < 4

! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

Logical ‘AND’ Operator (&&)

This operator returns true when both the conditions under consideration are satisfied or are true. If
even one of the two yields false, the operator results false. In Simple terms, cond1 && cond2 returns
true when both cond1 and cond2 are true (i.e. non-zero).

You might also like