Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Matthew Francis
Matthew Francis
6,967 Points

Super Newbie - Need a clarification on variables, expressions and statements

Super new to Java here, anywho:

I came accros this question in stackoverflow and can't seem to comprehend/visualize the differences between the three fully, could soemone dumb it down for me?(an example of each would be much appericiated!).



My understanding:

Variable : must return a value (eg; String varName = "Hey");

Expressions: I've read that it can either return a value or it can be void. How does this work?

Statements: No idea how this works.

2 Answers

Simon Coates
Simon Coates
28,693 Points

variables are named containers for values. expressions evaluate to values. A statement is a small unit of work. It's a small section of code typically ending with a ; that tells the code to do something. See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html for a discussion of expressions and statements.

Eric Stermer
Eric Stermer
9,499 Points

An expression is kind of a mathy term. If you remember your typical grade school math class had expressions such as 2+2=4 or 1+1=2

Expressions in code are much like the expressions in math which contain operators such as + - / * % etc.

As in math where you get a resultant from the expression, in code you can store the resultant in a variable or even return it in a function.

an example of this would be: int value = 1 + 1; where value = 2. This resultant of 2 is stored in the variable called value and can be used elsewhere in your code.

You can even use expressions with other datatypes: String sentence = "W" + "a" * 8 + "z" + "u" * 8 + "p"; resulting in sentence storing "Waaaaaaaazuuuuuuuup".

Simply put, this is what an expression is in code.