|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Syntax Refrence |
| 4 | +--- |
| 5 | +# Syntax Refrence |
| 6 | +Here you'll find a quick refrence to some basic java syntax. |
| 7 | + |
| 8 | +### Built-In Data Types |
| 9 | +* `String` - Stores text, such as "Hello". String values are surrounded by double quotes. |
| 10 | +* `int` - stores integers (whole numbers), without decimals, such as 123 or -123. |
| 11 | +* `double` - stores floating point numbers, with decimals, like 19.99 or -3.14159265. Double values end with `d`. (for people with experience, we will not be using `float` in our code). |
| 12 | +* `char` - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes |
| 13 | +* `boolean` - stores values with two states: `true` or `false` |
| 14 | + |
| 15 | +### Variables |
| 16 | +```java |
| 17 | +type variableName = value; |
| 18 | +``` |
| 19 | +Assignment: |
| 20 | +```java |
| 21 | +variableName = newValue; |
| 22 | +``` |
| 23 | +Example: |
| 24 | +```java |
| 25 | +String greeting = "Hello World"; |
| 26 | +double treeHeight = 12.2d; // meters |
| 27 | +int funnyNumber = 67; |
| 28 | +funnyNumber = 41; // funnyNumber is now 41 |
| 29 | +``` |
| 30 | + |
| 31 | +### Logic |
| 32 | +Operators |
| 33 | + - `<`, `>`: Less than, Greater than |
| 34 | + - `<=`, `>=`: Less than or equal to, Greater than or equal to |
| 35 | + - `==`: Equivalent to |
| 36 | + - `!=`: Not equivalent to |
| 37 | + - `!`: Not (inverts any boolean after it, so true->false and vice-versa) |
| 38 | + - `&&`: And (True: `(1 == 1) && (2 == 2)`, checks if 1 is equal to 1 **and** 2 is equal to 2) |
| 39 | + - `||`: Or (True: `(1 == 1) || (2 != 2)`, checks if 1 is equal to 1 **or** 2 is equal to 2) |
| 40 | + |
| 41 | +If Statements: |
| 42 | +```java |
| 43 | +if (condition1) { |
| 44 | + // Runs if condition1 is true |
| 45 | +} else if (condition2) { |
| 46 | + // Runs if condition1 is false but the 2nd condition is true |
| 47 | +} else { |
| 48 | + // Run if condition1 is false and condition2 is false |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +### Classes |
| 53 | +Classes need to be named according to the file name and must use a capital letter at the start |
| 54 | +```java |
| 55 | +class Car { |
| 56 | + private double speed = 0; |
| 57 | + |
| 58 | + public boolean isMoving() { |
| 59 | + return speed == 0; |
| 60 | + } |
| 61 | + |
| 62 | + public void accelerate() { |
| 63 | + speed += 1; |
| 64 | + } |
| 65 | + |
| 66 | + public void decelerate() { |
| 67 | + speed -= 1; |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | +- `public`: Accessable from instances of class and inside of its methods |
| 72 | +- `private`: Only accessable from inside the class's methods |
0 commit comments