自增、自减、幂运算

课程代码:

 1 package operator;
 2 
 3 public class Demo04 {
 4     public static void main(String[] args) {
 5         // ++、-- 自增、自减
 6         int a = 3;
 7         int b = a++;
 8         int c = ++a;
 9         System.out.println(a); // 5
10         System.out.println(b); // 3 先赋值b,再自增
11         System.out.println(c); // 5 先自增,再赋值 (注意前面a已经变成4,b为3)
12         System.out.println("==============");
13 
14         //幂运算 2^3 2*2*2=8
15         double pow = Math.pow(2,3); //如果不强转,不能使用int、float等类型(pow的方法要求是使用double)
16         System.out.println(pow);
17     }
18 }
View Code

 

注意点:

1:即使是在声明变量时,自增和自减也会做运算。

2:Math类中的pow方法,默认是double类型。

posted @ 2021-02-21 23:36  现在开始JAVA  阅读(84)  评论(0)    收藏  举报