Shorthand Assignments Program for Java

         Shorthand Assignment Operators



                                                 Java offers special shorthands that simplify the coding of a certain                                      type of Assignment statement.

For example,            a = a+10;

can be written as,      a+=10;


  The operator pair += tells the compiler to assign to a the value of   a+10.  This shorthand works  for all the binary operators in Java( those that requires two operands ).  The general form of Java Shorthand is :

                                                    var  =   var operator expression

is same as,

                                                      var  operator  =   expression

Following are some example of Java Shorthands :

                                          x-=10;            is equivalent to                 x=x-10;

                                          x*=3;              is equivalent to                  x=x*3;

                                          x/=2;               is equivalent to                  x=x/2;

                                          x%=z;               is equivalent to                  x%=z;


  One important and useful thing about such arithmetic assignment operators of Java is that they  combine an arithmetic operator and an assignment operator, and eliminate the repeated operand thereby facilitate a condensed approach.  


The following code will help you understand these operators more clearly :

       


  class Assignment{    //class name


    public static void main(String Args[]){     //method main

        int x=10 ,y=20;      //variable initialization

        

        y+=x;    //ADD

        

        System.out.println("Addition results to :" +y);

        

        y-=x;    //SUBSTACT

        

        System.out.println("Substraction results to :"+y);

        

        y*=x;    //MULTIPLY 

        

        System.out.println("Multiplication results to :"+y);

        

        y/=x;   //DIVIDE

        

        System.out.println("Division results to :"+y);

    

    

    }



}


so now you have understood Java Shorthand Assignments. Let's test it with a question :

           Write a program to add 10 to first three multiples of integer 5 using Shorthand                               Assignments  and subtract 5 from first three multiples of integer 6 using

            Shorthand Assignments. 


 Hope you have understood the Array concept now.

For more Program codes and videos visit my Youtube channel :

 https://youtube.com/channel/UC27TQ7zJKapHF78bwvi5iVw

For more Posts based on Programming visit my Blog:http://studytable10.blogspot.com/

if this blog has helped you, please follow my blog and share it so that more people can get help from it.


Thanks!! ☺

Comments

Post a Comment

What's in your mind?

Popular posts from this blog

Java Program for Binary to Decimal Conversion | Study Table

Binary Search Program for Java | Study Table

How to Start a Business?