increment java_post-increment, pre-increment. JAVA
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
I understand the difference between this operations (post-increment, pre-increment). But question: I have an expression:
int x = 4;
long y = x * 4 - x++;
The highest priority has post-unary operator, than "*" and last "-". In my opinion it will be:
long y = x * 4 - x++;
1). x++ => return 4 (save x = 5)
2). final expression: 5 * 4 - 4 = 16
But when I compile this in IDE the answer is 12 ! What's a problem and where did I do smth. wrong?
回答1:
EDITED
Here are the steps to evaluate the expression:
Evaluate all the expression that has increment/decrement from left to right
Replace all variable with their true value
Evaluate the expression using PEMDAS rule
Example:
int x = 4;
int y = ++x * 5 / x-- + --x;
first we need to substitute all values of x before evaluating the expression (substitute from left to right)
++x -> since it is a post increment we will first increment x before substituting, thus x will be 5
5 * 5 / x-- + --x -> this will be the new equation
now we will substitute x in x--
x-- -> since it is a post decrement x here will be substituted with 5 and after the substitution decrement x, thus x will be 4
5 * 5 / 5 + --x -> this will be the new equation
now we will substitute x in --x
--x -> since it is a pre decrement we will first decrement x substituting, thus x will be 3
5 * 5 / 5 + 3 // this will be the new equation
Since there are no variables in the equation we will now evaluate the expression using PEMDAS
25 / 5 + 3 5 + 3
8
thus the result will be 8
回答2:
x evaluates to 4, and x++ evaluates to 4 as well, and then gets incremented to 5. So it's essentially 4 * 4 - 4 , which gives 12 as expected.
回答3:
Multiplication is resolved separately, so x == 4. Incrementation occurs afterwards, so it's actually 4 * 4 - 4 == 12 and after this operation x == 5.
回答4:
The left side of a subtraction is always computed before the right side because subtraction is left associative i.e. 5 - 2 - 1 is (5 - 2) - 1, not 5 - (2 - 1).
This is why the multiplication happens before the increment. Associativity determines what happens first here, not precedence.
回答5:
long y = 4 * 4 - 4;
x will be incremented after this assignment
Everytime x is "called" with x++ it gets incremented after E.g.
int x = 1;
System.out.println(x++ + x++ * x++ + x++);
// 1 + 2 * 3 + 4
increment java_post-increment, pre-increment. JAVA相关推荐
- increment java_Java中的increment()有什么作用?
publicclassThisTest{privateinti=0;//第一个构造器:有一个int型形参ThisTest(inti){this.i=i+1;//此时this表示引用成员变量i,而非函数 ...
- 第十九章 Caché 函数大全 $INCREMENT 函数
文章目录 第十九章 Caché 函数大全 $INCREMENT 函数 大纲 参数 描述 参数 variable num INCREMENT或INCREMENT或INCREMENT或SEQUENCE $ ...
- 转: java并发编程-Executor框架
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- Java线程通俗讲解
Java线程介绍 通常情况下,在一个服务器上面运行的程序是很多的,可能同一时间会有多个客户 端的程序访问服务器,服务器都要对这些客户端做出响应.如果我们处理器有限, 只有一个处理器的时候,如何能够让这 ...
- Java遗传算法并行多机调度程序
程序有两部分组成,分别是gen和DNA程序.其中gen用来生成50条原始链,作为DNA初始化数据.DNA程序在gen生成的50条链的基础上完成交叉,变异直到找到最优答案. d:/工业/hk/bestL ...
- java 控制 android_Java For Android - 流程控制
为了控制程序的流程,Java编程语言提供了三种循环结构.灵活的if-else语言.switch语句.异常处理语句和分支语句. 1. 循环 while语句用于在一个布尔表达式保持为true时循环执行一个 ...
- java bean状态_无状态和有状态企业Java Bean
我将通过Java EE 6教程,我试图了解无状态和有状态会话bean之间的区别.如果无状态会话bean不在方法调用之间保留它们的状态,为什么我的程序执行它的方式? package mybeans; i ...
- HBase基本操作-java api
上次将了HBase搭建完成后,一些Shell的使用,现在我们将了解一下HBase的代码控制,我使用的是java8以及hbase2.1.1版本 首先你的集群已经是搭建完成了,然后这次我使用的是IDEA, ...
- java bean状态_无状态和有状态的企业Java Bean
我正在阅读Java EE 6教程,试图理解无状态会话bean和有状态会话bean之间的区别.如果无状态会话bean在方法调用之间没有保持其状态,为什么我的程序按原样运行? package mybean ...
最新文章
- Windows 7官方主题之“海上航行”
- 从神经质症的发生发展过程找到走出焦虑的途径
- 1.java的基础和数据类型
- Linux性能优化2.1 CPU性能统计信息
- svn 切换账号信息
- 用SQL Server 2017图形数据库替换数据仓库中的桥表
- [转]文件的操作方式
- php 使用csv海量数据导出类
- WebSphere报错指南
- JavaScript —— Symbol数据类型的拓展
- k8s mysql 弹性_k8s 弹性伸缩,基于prometheus自定义指标
- eclipse svn插件下载地址
- matlab在机械中的应用,MATLAB在机械工程控制基础中的应用
- 小宝智能机器人-智能-教育-情商高-家用教育商用一体机器人
- java jdom_Java JDOM解析器
- Gjs编程及调用底层库原理
- Java递归查询某个节点下所有子节点多级信息(递归部门查询,递归树形结构数据查询)
- Geospatial-地理空间
- 用正则表达式来判断手机号、地址、身份证号、邮箱等格式是否正确
- vue结合饿了么_饿了么基于Vue2.0的通用组件开发之路(分享会记录)
热门文章
- 机器学习基础(四十)—— 将距离转换为权重
- 面向对象——类设计(五)
- 创建对称矩阵(numpy)
- java 控制台输出到gui_java – 如何将类似窗口的“控制台”添加到GUI?
- python免费教程视频-28岁想从零开始自学python,有哪些靠谱免费的视频?
- python能自学成功吗-自学入门Python能学会吗?
- python自动化办公演示视频-2020年最新Python自动化办公视频教程(2020/9/3)
- python 代码命令大全-Python 命令行参数
- python开发软件的实例-如何编写Python软件开发文档(7个技巧)
- Android轻松实现语音识别