java中i+=2什么意思_三分钟看懂Java中i++与++i的性能差别以及循环中如何使用

在Java中,自增是一种非常常见的操作,在自增中,有两种写法,一种是前缀自增(++i),一种是后缀自增(i++)。这里主要简单介绍两种自增的差别。

510f6f1e68a6a0e597e2070fcdaa8904.png

一、含义差别

前缀自增和后缀自增是不同的。前缀自增(++i)是从内存中加载i,然后把它加1,使用,再返回存到内存中。而后缀自增(i++)是从内存中加载i,使用,把它加1,再存到内存中。在如下的for循环中,它们的使用没有区别:

// 循环使用后缀自增for (int i = 0; i < maxIteration; i++) {}// 循环使用前缀自增for (int i = 0; i < maxIteration; ++i) {}

但是,在如下代码中,它们会返回不同的结果:

// 后缀自增int i = 0;int j = i++; // 加载i,然后把i赋给j,然后i加1,再存到内存中System.out.println("i:" + i + "j:" + j);// 前缀自增i = 0;j = ++i; // 加载i,然后i加1,然后把i赋给j,再存到内存中System.out.println("i:" + i + "j:" + j);

可以看到,第一种i=1,j=0。但是第二种,i=1,j=1。原因也解释了,参考注释。

20b6059dffec9efef3889f280c668ade.png
e43b98750f4f32adc241d3bc6d80b872.png

二、性能(过程)差别

但是,如前所述,这两种自增在某些情况下的性能是有差别的。原因在于前缀自增(++i)是变量本身加1返回,而后缀自增(i++)则是创建一个新变量,得到原有变量之后加1返回。因此,后缀自增的过程会创建一个新变量,在编译器没有优化的情况下,后缀自增的速度比前缀自增慢一点点。如下代码:

public static void main(String[] args) { Test test = new Test(); long maxIteration = 1000000000L; // 后缀自增循环 long start = System.currentTimeMillis(); for (long i = 0; i < maxIteration; i++) { test.post(); } System.out.println("后缀自增:" + ((System.currentTimeMillis()) - start)); // 前缀自增循环 long start2 = System.currentTimeMillis(); for (long i = 0; i < maxIteration; ++i) { test.pre(); } System.out.println("前缀自增:" + ((System.currentTimeMillis()) - start2)); // 直接加1赋值 long start3 = System.currentTimeMillis(); for (long i = 0; i < maxIteration; ++i) { test.plusEquals(); } System.out.println("加1赋值:" + ((System.currentTimeMillis()) - start3));}private int post() { int a = 0; a++; return a;}private int pre() { int a = 0; ++a; return a;}private int plusEquals() { int a = 0; a += 1; return a;}

我们循环1000000000次,分别在循环中使用后缀自增,前缀自增和直接加1赋值的方法,可以看到如下的结果:

ea0ef4e6d8dd4f69324f2033309f567a.png

尽管每次运行的速度有差别,但是总体上前缀自增比后缀自增快一点点,而加1赋值则相对不稳定。

三、总结

但是要注意的是:一般来说,经过优化的编译器,前缀自增和后缀自增在循环中应该会被编译成相同的字节码,不会有速度上的差异。为了保险起见,写++i这种方式是推荐的。

b3d8a774c68b5a210423db9d5335e688.png
好的,@Mapper注解是Mybatis框架用于标识数据访问层接口的注解,用于告诉Spring容器将该接口类实例化并注入到其他Bean。其使用步骤如下: 1. 在Spring Boot项目引入Mybatis和Mybatis-Spring的依赖 2. 在配置文件配置数据源和Mybatis的相关属性 3. 创建一个数据访问层接口,使用@Mapper注解标识该接口 4. 在该数据访问层接口定义需要操作的数据库方法 5. 在Service或Controller注入该数据访问层接口的实例,并调用其的方法 下面是一个示例: 1. 在pom.xml添加Mybatis和Mybatis-Spring的依赖: ```xml <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> ``` 2. 在application.properties配置数据源和Mybatis的相关属性: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml ``` 3. 创建一个数据访问层接口UserMapper,使用@Mapper注解标识该接口: ```java @Mapper public interface UserMapper { User selectByPrimaryKey(Integer id); int insert(User record); int updateByPrimaryKey(User record); int deleteByPrimaryKey(Integer id); } ``` 4. 在mapper目录下创建UserMapper.xml,定义需要操作的数据库方法: ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="username" property="username" jdbcType="VARCHAR"/> <result column="password" property="password" jdbcType="VARCHAR"/> </resultMap> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer"> select * from user where id = #{id,jdbcType=INTEGER} </select> <insert id="insert" parameterType="com.example.demo.entity.User" useGeneratedKeys="true" keyProperty="id"> insert into user (username, password) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}) </insert> <update id="updateByPrimaryKey" parameterType="com.example.demo.entity.User"> update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where id = #{id,jdbcType=INTEGER} </delete> </mapper> ``` 5. 在Service或Controller注入UserMapper的实例,并调用其的方法: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } @Override public int insert(User user) { return userMapper.insert(user); } @Override public int updateByPrimaryKey(User user) { return userMapper.updateByPrimaryKey(user); } @Override public int deleteByPrimaryKey(Integer id) { return userMapper.deleteByPrimaryKey(id); } } ``` 这就是使用@Mapper注解的基本步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值