lelelime

Unary Operators Java

May 9th, 2021
443
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class UnaryOperotor {
  2.     public static void main(String[] args) {
  3.         int var1,var2;
  4.         var1=50;
  5.        
  6.        
  7.         var2 = -var1;
  8.         System.out.println("var1 is: " + var1 + ", var2 is: " + var2);
  9.         //inverts the sign of var1 value, can used to change to nagtive to positive, vice versa
  10.        
  11.         var1=50;
  12.         var2= var1++;
  13.         System.out.println("var1 is: " + var1 + ", var2 is: " + var2);
  14.         /* in this the var2 value is replaced/added by var1 value without the increment and saved as var2.
  15.          * and then ++ increcrement is added to var1.
  16.          * when the ++ is at the end the value is incremented after saving to var2.
  17.         */
  18.        
  19.         var1 = 50;
  20.         var2 = ++var1;
  21.         System.out.println("var1 is: " + var1 + ", var2 is: " + var2);
  22.         /* in this the var2 value is replaced by var1 value with the increment and saved as var2.
  23.          * therefore the value of var1 and var2 is the same
  24.          * when the ++ is at the beginning, the value is incremented before saving var2.
  25.         */
  26.        
  27.         var1=50;
  28.         var2= var1--;
  29.         System.out.println("var1 is: " + var1 + ", var2 is: " + var2);
  30.         /* in this the var2 value is replaced/added by var1 value without the decrement and saved as var2.
  31.          * and then -- increcrement is added to var1.
  32.          * when the -- is at the end, the value is decreased after saving to var2.
  33.         */
  34.        
  35.         var1 = 50;
  36.         var2 = --var1;
  37.         System.out.println("var1 is: " + var1 + ", var2 is: " + var2);
  38.         /* in this the var2 value is replaced by var1 value with the decrement and saved as var2.
  39.          * therefore the value of var1 and var2 is the same
  40.          * when the -- is at the beginning, the value is decreased before saving to var2.
  41.         */
  42.        
  43.         boolean bool1, result;
  44.         bool1 = true;
  45.         result = !bool1;
  46.        
  47.         System.out.println("bool1 is: " + bool1);
  48.         System.out.println("Result is: " + result);
  49.         /* "!" will invert the value of the boolean. For example if the value is true
  50.          * and you put "!" the value will invert to false, vice versa with true.
  51.          */
  52.     }
  53.  
  54. }
RAW Paste Data