This guide will help you understand different variable types in Java using the jshell environment. We'll explore int, boolean, double, and String variables with practical examples.
To begin:
- Open your terminal/command prompt
- Type
jshelland press Enter - You should see the jshell prompt:
jshell>
Definition: An integer variable stores whole numbers without decimal points.
How to declare:
int variableName;
int variableName = value;TYPE into jshell:
jshell> int age = 25
age ==> 25
jshell> int score = 100
score ==> 100
jshell> int negative = -10
negative ==> -10
jshell> int sum = age + score
sum ==> 125Type /vars to see all declared variables.
Save the work in a file using /save vars.jsh.
and then exit jshell using /exit or Ctrl-D.
Now, save the repo to your GitHub account.
git add .
git commit -m "Add Java Variables Guide"
git pushGo to a browser and navigate to your GitHub account to see the changes in this repository.
Okay go back to the terminal and let's continue.
Definition: A boolean variable stores true/false values.
How to declare:
boolean variableName;
boolean variableName = true/false;TYPE into jshell:
jshell> boolean isStudent = true
isStudent ==> true
jshell> boolean hasPassedExam = false
hasPassedExam ==> false
jshell> boolean result = 10 > 5
result ==> true
jshell> boolean isEqual = (15 == 15)
isEqual ==> trueDefinition: A double variable stores decimal numbers with high precision.
How to declare:
double variableName;
double variableName = value;TYPE into jshell:
jshell> double price = 19.99
price ==> 19.99
jshell> double temperature = -2.5
temperature ==> -2.5
jshell> double result = 10.5 + 3.7
result ==> 14.2
jshell> double division = 10.0 / 3.0
division ==> 3.3333333333333335Definition: A String variable stores text (sequence of characters).
How to declare:
String variableName;
String variableName = "text";Hey, here, replace "John" with your name.
TYPE into jshell:
jshell> String name = "John"
name ==> "John"
jshell> String greeting = "Hello, World!"
greeting ==> "Hello, World!"
jshell> String combined = name + " says " + greeting
combined ==> "John says Hello, World!"
jshell> String empty = ""
empty ==> ""- Try typing these examples in jshell
- Experiment with different values
- Try combining variables in operations
- Use
/varscommand to see all declared variables - Save the work in a file using
/save vars.jsh - Use
/exitto leave jshell
Be sure to do commit of all this work to your GitHub repository.
git add .
git commit -m "Add Java Variables Guide"
git push- Don't forget to declare the variable type
- Use correct capitalization (String vs string)
- Remember to use quotes for String values
- Don't use decimal points in int variables
- Remember that boolean only accepts true/false values
This guide should give you a solid foundation for understanding and working with basic Java variables in jshell. Practice with these examples and try creating your own variables to become more comfortable with these concepts.
Now go to the ClassPerson.md guide to learn about creating custom classes in Java.