CodeGym /Java 博客 /随机的 /Java 中的数字运算符
John Squirrels
第 41 级
San Francisco

Java 中的数字运算符

已在 随机的 群组中发布
你好!今天我们来探讨一个很重要的话题,即Java中的数值运算符
Java 中的数字运算符 - 1
在编程中,数字无处不在。如果你深入研究并记得高中,你可能还记得计算机以数字格式表示所有信息:零和一的组合,也称为二进制代码。
Java 中的数字运算符 - 2
编程中有很多数字运算符,因此我们将使用示例来探索其中最重要的运算符 :) 让我们从最简单的开始:算术运算符。这些是众所周知的加法 ( +)、减法 ( -)、乘法 ( *) 和除法 ( /) 运算符。

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;

       System.out.println(x+y);
       System.out.println(x-y);
       System.out.println(x*y);
       System.out.println(x/y);
   }
}
控制台输出: 1032 966 32967 30 这些你都已经用过了。对于这个组,您可以添加余数或模 ( %) 运算符。

public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 33%2;
       System.out.println(y);
   }
}
控制台输出: 1 在此示例中,我们将 33 除以 2。结果为 16,带有一个不能被 2 整除的额外“尾巴”(一)。此“尾巴”是“除法余数”运算的结果。Java 还实现了比较/关系运算符(就像在数学中一样)。他们可能也是你在学校熟悉的:
  • 等于 ( ==)
  • 大于 ( >)
  • 小于 ( <)
  • 大于或等于 ( >=)
  • 小于或等于 ( <=)
  • 不等于 ( !=)
在这里你应该注意一个重要的点,它会导致很多初学者犯错误。“等于”运算符写成==,而不是= 在 Java 中,single=赋值运算符,当一个变量被赋值一个数字、字符串或另一个变量的值 时使用它。
Java 中的数字运算符 - 3

public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 999;
       System.out.println(x=y);// We expect false to be displayed
   }
}
控制台输出: 999 糟糕!这显然不是我们预期的结果。这是一种完全不同的数据类型:我们希望看到一个布尔值,但我们得到了一个数字。都是因为我们在括号中使用了赋值运算符而不是比较运算符x=y (999)的值y赋值给变量x,然后我们显示 的值x。这是正确的方法:

public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 999;
       System.out.println(x==y);
   }
}
控制台输出: false 现在我们已经正确比较了两个数字!:) 这是赋值运算符 ( =) 的另一个特性:它可以“链接”在一起:

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;
       int z = 256;

       x = y = z;
       System.out.println(x);
   }
}
控制台输出: 256 记住分配是从右到左。该表达式 ( x = y = z) 将分步执行:
  • y = z, 那是,y = 256
  • x = y, 那是,x = 256

一元运算符。

它们被称为“ unary ”,来自“ uno ”这个词,意思是“一个”。他们之所以得到这个名字,是因为与以前的运算符不同,它们作用于单个数字,而不是多个。这些包括:
  • 一元减号。它翻转数字的符号。


public class Main {

   public static void main(String[] args) {

       int x = 999;

       // Change the sign for the first time
       x = -x;
       System.out.println(x);

       // Change the sign for the second time
       x= -x;
       System.out.println(x);
   }
}
控制台输出: -999 999 我们使用了两次一元减号运算符。结果我们的数先是负数,然后又变成正数了!
  • 递增 (++) 和递减 (--)
运算++符将数字增加 1,并且--运算符将数字减少相同的数量。

public class Main {

   public static void main(String[] args) {

       int x = 999;
       x++;
       System.out.println(x);

       x--;
       System.out.println(x);
   }
}
控制台输出: 1000 999 如果您听说过 C++ 语言,您可能会熟悉这种表示法。它的创建者用这个有趣的名字来传达“C++ 是 C 语言的扩展”的想法 有两种类型的递增和递减运算符:postfixprefixx++- 后缀 ++x- 前缀 将加号/减号放在数字之前或之后的根本区别是什么?我们将在以下示例中看到:

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = x++;
       System.out.println(y);
   }
}

控制台输出: 999 不对劲!我们想增加x1 并将新值分配给变量 y。换句话说,y 应该是 1000。但我们却得到了其他东西:999。似乎 x 没有增加并且增量运算符不起作用?但它确实奏效了。为了说服自己,请尝试x在最后展示 :)

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = x++;
       System.out.println(y);
       System.out.println(x);
   }
}
控制台输出: 999 1000 事实上,这正是这个操作被称为后缀的原因:它在主表达式之后执行。这意味着,在我们的例子中: int y = x++; y = x首先执行(变量y将被初始化为 的值x),然后才 x++执行 如果这不是我们想要的行为怎么办?然后我们需要使用前缀表示法:

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = ++x;
       System.out.println(y);
   }
}
在这种情况下,++x首先处理,然后才处理y = x;执行。 您应该立即将此差异记入内存,以避免在实际程序中犯错误,在实际程序中使用后缀而不是前缀可能会颠倒一切:)

复合运算符

此外,Java中还有所谓的复合运算符。它们结合了两个运算符:
  • 任务
  • 算术运算符
这些包括:
  • +=
  • -=
  • *=
  • /=
  • %=
让我们考虑一个例子:

public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;

       x += y;
       System.out.println(x);
   }
}
控制台输出: 1032 x += y表示x = x + y. 为简洁起见,连续使用这两个符号。组合-=*=和以/=类似%=的方式工作。

逻辑运算符

除了数字运算符之外,Java 还具有涉及布尔值(truefalse)的运算。这些操作是使用逻辑运算符执行的
  • !- 逻辑。它翻转布尔值

public class Main {

   public static void main(String[] args) {

       boolean x = true;
       System.out.println(!x);
   }
}
控制台输出:
  • &&- 逻辑。只有当两个操作数都为真时,它才返回真。

public class Main {

   public static void main(String[] args) {

       System.out.println(100 > 10 && 100 > 200);
       System.out.println(100 > 50 && 100 >= 100);
   }
}
控制台输出: false true 第一次运算结果为false,因为其中一个操作数为false,即100 > 200. 要返回 true,&&运算符要求两个操作数都为 true(如第二行中的情况)。
  • ||- 逻辑当至少一个操作数为真时,它返回真。
当我们使用此运算符时,我们之前的示例会产生不同的结果:

public class Main {

   public static void main(String[] args) {

       System.out.println(100 > 10 || 100 > 200);
   }
}
控制台输出: true 表达式100 > 200仍然为 false,但对于OR运算符来说,第一部分 ( 100 > 10) 为 true 就完全足够了。
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION