Java 中的表达式和语句有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39523474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between an expression and a statement in Java?
提问by lxgeek
I'm a beginner for Java, and I want to know the difference between expressions and statements in Java?
我是Java初学者,想知道Java中表达式和语句的区别?
采纳答案by fastcodejava
This is an example :
这是一个例子:
b + 1
is an expression while a = b + 1;
is a statement. A statement consists of expressions.
b + 1
是一个表达式 whilea = b + 1;
是一个语句。语句由表达式组成。
This is not specific to the Java language. Many languages use this kind of grammar e.g. C, C++, Basic
etc (not SQL
).
这不是 Java 语言特有的。许多语言使用这种语法,例如C, C++, Basic
etc (not SQL
)。
回答by musibs
From Javadoc,
从 Javadoc,
Expression
表达
An expressionis a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.
一个表达式是变量,运算符和方法调用,它们根据语言的语法构造由一个构建体,计算结果为单个值。
For example,
例如,
int cadence= 0;
The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int.
int节奏= 0;
表达式返回值的数据类型取决于表达式中使用的元素。表达式cadence = 0 返回一个int,因为赋值运算符返回的值与其左操作数的数据类型相同;在这种情况下,节奏是一个整数。
Statement
陈述
Statementsare roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.
语句大致相当于自然语言中的句子。一条语句构成了一个完整的执行单元。