1 | | Let's try a slightly more complex program that will introduce variables. A variable is like a box that can hold something for you. In Java, you can put only one type of thing in a given box. Variables can hold things like: |
2 | | * An integer number (e.g. 42) |
3 | | * A real number (e.g. 3.141592) |
4 | | * A character (e.g. 'a' or '0' or '!') |
5 | | * A string of characters (e.g. "Howdy") |
6 | | * Something you define |
| 1 | Let's try a slightly more complex program that will introduce variables. A variable is like a box that can hold something for you. In Java, you can put only one type of thing in a given box. Different types of variables can hold different things like: |
| 2 | |
| 3 | * `int` can hold an integer number (e.g. 42) |
| 4 | * `double` can hold a real number (e.g. 3.141592) |
| 5 | * `char` can hold a character (e.g. 'a' or '0' or '!') |
| 6 | * `String` can hold a string of characters (e.g. "Howdy") |
| 7 | * `boolean` can hold values with two states: true or false |
10 | | 1. Close your current project if you have one open (File->Close Folder) |
11 | | 2. Make a new folder in your Java Projects folder named !VariablesExample |
12 | | 3. Open the folder (File->Open Folder) |
13 | | 4. Create a new file: (File->New File) |
14 | | 5. Cut and paste the program below into the editor window: |
| 11 | 1. Close your current project if you have one open (File->Close Folder) |
| 12 | 1. Make a new folder in your Java Projects folder named !VariablesExample |
| 13 | 1. Open the folder (File->Open Folder) |
| 14 | 1. Create a new file: (File->New File) |
| 15 | 1. Cut and paste the program below into the editor window: |
35 | | * Creates 3 variables (boxes) named a, b, c. They are all integer variables (they can only hold integer numbers). |
36 | | * The program then places the number 3 in variable a, the number 4 in variable b, and the product of whatever is in a and b into variable c. |
37 | | * Finally, the program prints whatever value is in variable c. |
| 36 | |
| 37 | * Creates 3 variables (boxes) named a, b, c. They are all integer variables (they can only hold integer numbers). |
| 38 | * The program then places the number 3 in variable a, the number 4 in variable b, and the product of whatever is in a and b into variable c. |
| 39 | * Finally, the program prints whatever value is in variable c. |