DEV Community

Cover image for Operators in Java
M. Oly Mahmud
M. Oly Mahmud

Posted on

1

Operators in Java

Operators in Java are special symbols or keywords used to perform operations on variables and values. They play a crucial role in manipulating data and controlling program flow. Java provides a wide range of operators grouped into several categories based on functionality.

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical tasks such as addition, subtraction, multiplication, division, etc.

  • + is used to perform addition
int x = 5, y = 10;
int sum = x + y; // sum = 15
Enter fullscreen mode Exit fullscreen mode
  • - is used for subtraction
int x = 10, y = 5;
int difference = x - y; // difference = 5
Enter fullscreen mode Exit fullscreen mode
  • * for multiplication
int x = 5, y = 4;
int product = x * y; // product = 20
Enter fullscreen mode Exit fullscreen mode
  • / for division
int x = 20, y = 4;
int quotient = x / y; // quotient = 5
Enter fullscreen mode Exit fullscreen mode
  • % is used to find the division remainder
int x = 10, y = 3;
int remainder = x % y; // remainder = 1
Enter fullscreen mode Exit fullscreen mode
  • ++ or increment operator increases the value of a variable by 1.
int x = 5;
x++; // x = 6
Enter fullscreen mode Exit fullscreen mode
  • -- or decrement operator Decreases the value of a variable by 1.
int x = 5;
x--; // x = 4
Enter fullscreen mode Exit fullscreen mode

Comparison operators

Comparison operators are used to compare two values (or variables). The return value of a comparison is either true or false.

  • == or Equal to: Checks if two values are equal.
int x = 5, y = 5;
boolean isEqual = (x == y); // isEqual = true
Enter fullscreen mode Exit fullscreen mode
  • != or Not equal: Checks if two values are not equal.
int x = 5, y = 10;
boolean isNotEqual = (x != y); // isNotEqual = true
Enter fullscreen mode Exit fullscreen mode
  • > or Greater than: Checks if one value is greater than another.
int x = 10, y = 5;
boolean isGreaterThan = (x > y); // isGreaterThan = true
Enter fullscreen mode Exit fullscreen mode
  • < or less than: Checks if one value is less than another.
int x = 5, y = 10;
boolean isLessThan = (x < y); // isLessThan = true
Enter fullscreen mode Exit fullscreen mode
  • >= ( Greater than or equal to ): Checks if one value is greater than or equal to another.
int x = 10, y = 10;
boolean isGreaterThanOrEqualTo = (x >= y); // isGreaterThanOrEqualTo = true
Enter fullscreen mode Exit fullscreen mode
  • <= ( Less than or equal to ): Checks if one value is less than or equal to another.
int x = 5, y = 10;
boolean isLessThanOrEqualTo = (x <= y); // isLessThanOrEqualTo = true
Enter fullscreen mode Exit fullscreen mode

Assignment Operators

Assignment operators are used to assign values to variables.

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

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 Reverses the result, returns false if the result is true. !(x < 5 && x < 10)

Conclusion

Operators are the building blocks for manipulating data and implementing logic. Practice using different operators in real-world scenarios to strengthen your command over them.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay