2

I was going through this article on java9 and came across this line where it states that Java is statement-oriented whereas REPLs are expression-oriented.

Can somebody explain the difference between these two?

Naman
  • 27,789
  • 26
  • 218
  • 353
Sadiq Ali
  • 1,272
  • 2
  • 15
  • 22

1 Answers1

1

A very quick and coincidently visible difference is that following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

  • Assignment expressions
  • Any use of ++ or --
  • Method invocations
  • Object creation expressions
  • Such statements are called expression statements.

So when in your HelloWorld.java class the following would not compile -

int z = 1 //(; missing)

Jshell on its prompt successfully stores the value as:

jshell> int z = 1
z ==> 1

Morevoer

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution.

whereas an Expression is 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.

Naman
  • 27,789
  • 26
  • 218
  • 353