Code Symphony: Mastering the Art of Java Operators

Code Symphony: Mastering the Art of Java Operators

In Java, operators are symbols that allow you to perform various operations on variables and values. They are essential for performing calculations, comparisons, and other manipulations in your programs.
Let's go through some commonly used operators in Java along with real-life examples and code snippets:
No alt text provided for this image
Types Of Opreators
Addition (+): Calculating the total number of apples.
int applesInBasket = 5
int applesBought = 3;

int totalApples = applesInBasket + applesBought;
System.out.println("Total apples: " + totalApples);
// System.out.println is a Java statement used to display or print output to the console.        

Division (/): Splitting a pizza equally among friends.
int numSlices = 8
int numFriends = 4;

int slicesPerFriend = numSlices / numFriends;
System.out.println("Slices per friend: " + slicesPerFriend);        

Greater than (>) and Less than (<): Comparing the number of likes on two social media posts.
int likesPost1 = 50
int likesPost2 = 75;

boolean isPost2MoreLiked = likesPost2 > likesPost1;
System.out.println("Is Post 2 more liked? " + isPost2MoreLiked);        

Logical AND (&&) and OR (||): Checking if a product is in stock and on sale.
boolean isInStock = true;
boolean isOnSale = false;

boolean canPurchase = isInStock && isOnSale;
System.out.println("Can purchase? " + canPurchase);        

Modulus (%): Finding out if a number is even or odd.
int number = 15;
boolean isEven = number % 2 == 0;

System.out.println("Is number even? " + isEven);        

Increment (++) and Decrement (--): Counting the number of unread messages.
int unreadMessages = 10;
unreadMessages++;

System.out.println("Unread messages: " + unreadMessages);
// Unread messages: 11
// The output(value) is 11 of  unreadMessages after increment++.        

Ternary Operator (condition ? expr1 : expr2): Checking if a customer is eligible for free shipping.
double cartTotal = 75.99;

boolean isEligibleForFreeShipping = cartTotal >= 50 ? true : false;

System.out.println("Eligible for free shipping? " + isEligibleForFreeShipping);        

Here's a Boolean Equivalences chart that demonstrates different comparisons involving boolean values .
true and false using the == operator:
System.out.println("true = true : " + (true == true));
System.out.println("false = false : " + (false == false));
System.out.println("true = false : " + (true == false));
System.out.println("false = true : " + (false == true));

true = true : true
false = false : true
true = false : false
false = true : false        

BOMAS in Java stands for Brackets, Orders (exponents), Multiplication, and Division (left-to-right), Addition, and Subtraction (left-to-right).
BOMAS is a way to remember the order of operations when evaluating expressions in Java.

  1. Brackets: Perform calculations inside brackets first.
  2. Orders (exponents): Calculate any exponents (like a^b) next.
  3. Multiplication and Division: Evaluate multiplication and division operations from left to right.
  4. Addition and Subtraction: Lastly, perform addition and subtraction operations from left to right.

Example:

Imagine you have a shopping cart with two items, and you want to calculate the total cost after applying discounts.

double item1Price = 25.99;
double item1Discount = 0.15;
double item2Price = 19.95;
double item2Discount = 0.10;
? ? ? ??
// Calculate the total cost of items with discounts
double totalCost = (item1Price * (1 - item1Discount)) + (item2Price * (1 - item2Discount));
? ? ? ??
System.out.println("Total Cost: $" + totalCost);
// Total Cost: $40.046499999999995
        

These examples demonstrate how operators are used in various real-life scenarios to perform calculations, comparisons, and decision-making in Java programs.

Remember that operators can be combined in more complex expressions to solve different problems efficiently.

That's It For Now!

If it's helpful like or comment for any suggestions.

If you have any query, please feel free to contact me.

Cheers ??.

要查看或添加评论,请登录

Satish Jassal的更多文章

社区洞察

其他会员也浏览了