#9 Arithmetic Operators in Java

#9 Arithmetic Operators in Java

Introduction to Operators

In this section, the speaker introduces the concept of operators and explains how they can be used to perform operations on different values. Examples of various operators such as addition, subtraction, multiplication, division, and modulus are provided.

Basic Arithmetic Operations

  • Addition: Two numbers can be added using the plus operator. For example, 7 + 5 = 12.
  • Subtraction: Two numbers can be subtracted using the minus operator. For example, 7 - 5 = 2.
  • Multiplication: Two numbers can be multiplied using the star (*) operator. For example, 7 * 5 = 35.
  • Division: Two numbers can be divided using the slash (/) operator. For example, 7 / 5 = 1 (integer division).
  • Modulus Operator for Remainder Calculation: The percentage (%) symbol is used as a modulus operator to obtain the remainder of a division operation. For example, 7 % 5 = 2 (remainder).

Increment and Decrement Operations

  • Incrementing a Variable's Value:
  • A variable's value can be incremented by adding a specific value to it using the plus-equal (+=) operator.
  • Alternatively, the shorthand notation "variable++" can also be used for incrementing by one.
  • Example with num1 variable incrementing by two:

num1 += 2; // or num1++

Resulting in num1 becoming equal to nine after the operation.

  • Decrementing a Variable's Value:
  • A variable's value can be decremented by subtracting a specific value from it using the minus-equal (-=) operator.
  • Alternatively, the shorthand notation "variable--" can also be used for decrementing by one.
  • Example with num1 variable decrementing by one:

num1 -= 1; // or num1--

Resulting in num1 becoming equal to eight after the operation.

Conclusion

The speaker concludes the video by summarizing the different arithmetic operations and increment/decrement operations that can be performed using operators in Java.

Simplifying Variable Names

In this section, the speaker discusses how to simplify variable names in code examples by using a different name instead of repeating the same variable name multiple times.

Simplifying Variable Names

  • Instead of using num1 repeatedly, the speaker suggests using num as a simplified variable name.
  • The difference between writing num++ and ++num is explained. The former is called post-increment and the latter is called pre-increment.
  • Post-increment fetches the value first and then increments it, while pre-increment increments the value first and then fetches it.
  • The behavior of post-increment and pre-increment when assigning values to variables is demonstrated with an example.

Difference Between Post-Increment and Pre-Increment

This section explains the difference between post-increment and pre-increment operations in terms of fetching values from variables.

Fetching Values with Post-Increment and Pre-Increment

  • When fetching values from variables, post-increment (num++) and pre-increment (++num) behave differently.
  • An example is provided to demonstrate that assigning a value after post-increment results in fetching the original value, while assigning a value after pre-increment results in fetching the incremented value.

Understanding Assignment Operator with Increment Operations

This section delves deeper into how assignment operators work with increment operations.

Assignment Operator Behavior with Increment Operations

  • When using assignment operators (+=, -=) with increment operations (++, --), there are two statements involved - fetching the value and incrementing it.
  • The behavior of assignment operators depends on whether they are used with post-increment or pre-increment.
  • The process of fetching the value and incrementing it is explained with an example.

Recap and Introduction to Arithmetic Operators

This section provides a recap of the discussed topics and introduces arithmetic operators.

Recap and Introduction to Arithmetic Operators

  • A recap is given on the concepts of post-increment, pre-increment, and assignment operators with increment operations.
  • The speaker mentions that further operations with these concepts will be covered in future lessons.
  • The introduction to arithmetic operators is mentioned as the next topic to be discussed.
Video description

Check out our courses: AI-Powered DevOps with AWS Live Course V2: https://go.telusko.com/ai-devops-v2 Coupon: TELUSKO10 (10% Discount) Java Spring Boot AI Live Course: https://go.telusko.com/JavaSpringBootAI Coupon: TELUSKO20 (20% Discount) Master Java Spring Development : https://go.telusko.com/masterjava Coupon: TELUSKO20 (20% Discount) Udemy Courses: Java Spring:- https://go.telusko.com/Udemyjavaspring Java:- https://go.telusko.com/udemyteluskojava Spring: https://go.telusko.com/udemyteluskospring Java For Programmers:- https://go.telusko.com/javaProgrammers Python : https://go.telusko.com/udemyteluskopython Git : https://go.telusko.com/udemyteluskogit Docker : https://go.telusko.com/udemyteluskodocker For More Queries WhatsApp or Call on : +919008963671 website : https://courses.telusko.com/ Instagram : https://www.instagram.com/navinreddyofficial/ Linkedin : https://in.linkedin.com/in/navinreddy20 TELUSKO Android App : https://go.telusko.com/TELUSKOAPP TELUSKO IOS App : https://apple.co/3SsgmU2 Discord : https://discord.gg/D8hWe9BqfF In this lecture we are discussing: 1)Assignment operator a) = is used to assign a value to a variable b) += is used to assign a value to a variable by adding it to the existing value c) -= is used to assign a value to a variable by subtracting it from the existing value -- there are many but in this lecture we discussing only some e.g of = int num1=11; int num2=12; int result = num1 + num2; int result1=num1-num2; int result2=num1*num2; int result3=num1/num2; e.g of += int num=1; num +=1; = num = num+1; num +=5; = num = num+5; e.g of -= int num2=2; num -=1; num =num-1; num -=5; num =num-5; 2)increment and decrement operator -- there are two type of increment and decrement operator a) pre b) post -- post increment and decrement operator int num=1; num++; System.out.println(num); // 2 -- this operator is known as post increment operator num--; System.out.println(num); //1 -- this operator is know as post decrement operator -- pre increment and decrement operator int num=2; ++num; System.out.println(num);//3 --num; System.out.println(num);//2 Difference between pre and post operator e.g for that we take one example int num=5; System.out.println(num++); // 5 is print on console int num1=5; System.out.println(++num); //6 is printed on screen in post first assignment and then increment e.g int num=5; int copy; copy=num++; in this case first num=5 assign to copy then increment the num copy value is 5; in pre first increment then assignment e.g int num=5; int copy; copy = ++num; in this case first increment num value from 5 to 6 then assign to copy. copy value is 6; Github repo : https://github.com/navinreddy20/Javacode.git More Learning : Java :- https://bit.ly/3x6rr0N Python :- https://bit.ly/3GRc7JX Django :- https://bit.ly/3MmoJK6 JavaScript :- https://bit.ly/3tiAlHo Node JS :- https://bit.ly/3GT4liq Rest Api :-https://bit.ly/3MjhZwt Servlet :- https://bit.ly/3Q7eA7k Spring Framework :- https://bit.ly/3xi7buh Design Patterns in Java :- https://bit.ly/3MocXiq Docker :- https://bit.ly/3xjWzLA Blockchain Tutorial :- https://bit.ly/3NSbOkc Corda Tutorial:- https://bit.ly/3thbUKa Hyperledger Fabric :- https://bit.ly/38RZCRB NoSQL Tutorial :- https://bit.ly/3aJpRuc Mysql Tutorial :- https://bit.ly/3thpr4L Data Structures using Java :- https://bit.ly/3MuJa7S Git Tutorial :- https://bit.ly/3NXyCPu Donation: PayPal Id : navinreddy20 https://www.telusko.com