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! While you're at it, check out some resources Treehouse students have shared here.

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

Java Java Objects Meet Objects Welcome Back

Does the automatic creation of the $3, $4, etc variable only happen in jshell?

Does the automatic creation of the $3, $4, etc variable only happen in jshell, or does it happen in other environments as well?

1 Answer

Tonnie Fanadez
.a{fill-rule:evenodd;}techdegree seal-36
Tonnie Fanadez
UX Design Techdegree Graduate 22,796 Points

william coleman

I haven't seen this in other environments apart from JShell itself.

In JShell $1,$2, $3, $4,$5, $100 etc. are called Positional Parameters. A positional parameter is a parameter denoted by one or more digits, other than the single digit 0.

Once Jshell has allocated a variable with a preceding dollar symbol followed by a digit, it means that you can use the value of this variable on Jshell by typing out the positional parameter.

For Example

If you type Integer.parseInt("17") JShell prints out $5==> 17

You can then use the created value by just typing "$5"

jshell> Integer.parseInt("17")
$5 ==> 17     
// JShell creates a variable with a positional parameter denoted as $5 with a value of 17

jshell> int number = 3;
number ==> 3

jshell> number + $5   //---->>> You can use the Jshell value by just typing $5
$7 ==> 20


Thanks, Tonnie!