Operator | Meaning | Example | Result |
---|---|---|---|
+ | addition | 2 + 2 | 4 |
– | subtraction | 53 – 18 | 35 |
* | multiplication | 3 * 8 | 24 |
/ | division | 4 / 2 | 2.4 |
% | remainder or modulo | 19 % 5 | 4 |
17 / 5
char
data type can only hold one characterString
can hold as many as you wantint
float
boolean
String
int number = 5; System.out.println("Literal 6 -> " + 6); System.out.println("Variable number -> " + number); System.out.println("Calculation number * 3 -> " + number * 3 );when run, the output is
Literal 6 -> 6 Variable: number -> 5 Calculation number * 3 -> 15
javac
processes the code statement by statementSystem.out.println(4); System.out.println(x); System.out.println(4 + x);
(4 + x) / (4 - y)
(4 + x)
(4 - y)
3 + 2 * 4
3 + 2 = 5 5 * 4 = 20
2 * 4 = 8 3 + 8 = 11
3 + 2 * 4
3 + 2 * 4 = 3 + 8 = 11
3 * 6 / 2 = 18 / 2 = 9
3 + 2 * 4 = 3 + 8 = 11 (3 + 2) * 4 = 5 * 8 = 40
System.out.println("Line 1 Line 2");results in
error: ';' expected
System.out.println("Line 1
^
error: unclosed string literal
System.out.println("Line 1
^
error: unclosed string literal
Line 2");
^
error: not a statement
Line 2");
^
4 errors
Sequence | Represents |
---|---|
\t | tab character |
\n | new line character |
\" | double quote |
\\ | backslash |
$ cat ThreeLines.java public class ThreeLines { public static void main(String[ ] args) { System.out.println("Line 1 \nLine 2 \nLine 3") ; } } $ java ThreeLines Line 1 Line 2 Line 3
code block
public class PlusPlusMinusMinus { public static void main (String[] args){ int number = 5; System.out.println("number: " + number); number++; System.out.println("number: " + number); ++number; System.out.println("number: " + number); number--; System.out.println("number: " + number); --number; System.out.println("number: " + number); } } $ java PlusPlusMinusMinus number: 5 number: 6 number: 7 number: 6 number: 5
Description Operators unary operators –– – multiplicative operators / % additive operators – assignment operators += *= %=