I have most of these common patterns built-in as SlickEdit macros so I don’t have to remember the patterns. I just click a custom icon for the pattern I want. Unfortunately, I thus don’t remember them. Without my editor, I would be at a loss to remember where the parentheses and semicolons go.
| Control Structures | Precedence |
| Loops | Keywords |
| Try/Catch/Throw | Javadoc |
| Try/Catch/Finally | Regex cheat sheet |
| Literals | HTML cheat sheet |
| Primitives | Links |
| Immutables |
// splitting a string literal over more than one line String s = "abcdefghijklmnopqrst" + "uvwxyz";There is no speed penalty for the + concatenation. It is done at compile time. String literals can be used anywhere you might use a String reference, e. g. "abc".charAt(1) is legal. For problematic/awkward/reserved/quotable characters like embedded ", see escape sequences below.
| Java Primitives | |||||||
|---|---|---|---|---|---|---|---|
| Type | Signed? | Bits | Bytes | Digits | Lowest | Highest | Mnemonic |
| boolean | n/a | 1 | 1 | 1 | false | true | zero/one |
| char | unsigned Unicode | 16 | 2 | 4:5 | '\u0000' [0] aka Character. MIN_VALUE | '\uffff' [216-1] aka Character. MAX_VALUE | Unicode chars are twice as big as C’s. |
| byte | signed | 8 | 1 | 2:3 | -128 [-27] aka Byte. MIN_VALUE | +127 [27-1]aka Byte. MAX_VALUE | Bytes are signed, so half the usual 255 range. |
| short | signed | 16 | 2 | 4:5 | -32,768 [-215] aka Short. MIN_VALUE | +32,767 [215-1] aka Short. MAX_VALUE | 32K |
| int | signed | 32 | 4 | 9:10 | -2,147,483,648 [-231] aka Integer. MIN_VALUE | +2,147,483,647 [231-1] aka Integer. MAX_VALUE | 2 gig |
| long | signed | 64 | : | 9,223,372,036,854,775,807 [-263] aka Long. MIN_VALUE | -9,223,372,036,854,775,808 [263-1] aka Long. MAX_VALUE | exabytes, or billion gig | |
| float | signed exponent and mantissa | : | ±1.40129846432481707e-45 aka Float. MIN_VALUE | ±3.40282346638528860e+38 aka Float.
MAX_VALUE
or roughly ±2127 with to significant digits of accuracy. A float can exactly represent integers in the range -224 to +2 24. |
rough, compact float | ||
| double | signed exponent and mantissa | : | ±4.94065645841246544e-324 aka Double. MIN_VALUE | ±1.79769313486231570e+308 aka Double.
MAX_VALUE
or roughly ±21023 with to significant digits of accuracy. A double can exactly represent integers in the range -253 to +2 53. |
high precision float | ||
| Mutable Primitives | Immutable Objects |
|---|---|
| boolean | Boolean |
| ordinary signed byte | Byte |
| unsigned byte | Byte |
| short | Short |
| char | Character |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| char[] | String |
| Operator Precedence | |||
|---|---|---|---|
| Precedence | Operator | Association | Notes |
| 1 | (prefix) ++
(prefix) -- (unary) + - ~ ! (cast) |
Right (prefix) | ++ prefix means preincrement ~ is bitwise not for ints. Preincrement increments before sampling the value to use in evaluating the rest of the expression. ! is logical not for booleans. Nearly always, you have to put the expression after ! in parentheses. You might as well make a habit of always doing it. |
| 1 | (postfix) ++ --
|
Right (postfix) | ++ postfix means postincrement. Postincrement
increments immediately after sampling the value to use in evaluating the rest of
the expression. Be very careful about using a pre or post incremented variable
elsewhere in an expression. You may be in for a surprise. for example
// Be very careful reusing a pre or post increment variable in an expression. int x = 2; int y = x++ * ++x; // result is y = 8; // it works as if you had coded: int x = 2; int a = x++; // a=2; x=3; int b = ++x; // b=4; x=4 int y = a * b; // y = 8 |
| 2 | * / % | Left (infix) | % is the remainder operator, informally and incorrectly called the modulus operator. / is integer division for ints and floating point division for doubles. |
| 3 | + - | Left (infix) | a - b - c means (a - b) - c not a - ( b - c ), additive operations are performed left to right. + also means concatenation. |
| 4 | << >> >>> | Left (infix) | There is no <<< operator because it would be identical to <<. You have to keep your wits about you when doing unsigned shifts to remember all right shifts must be done with >>>. In JDK 1.5+ the following methods are built-in letting you avoid much low-level bit fiddling: highestOneBit, lowestOneBit, numberOfLeadingZeros, numberOfTrailingZeros, bitCount, rotateLeft, rotateRight, reverse, signum and reverseBytes. |
| 5 | < > <=
>=
instanceof |
Left (infix) | |
| 6 | == != | Left (infix) | == is for comparison. != means not equal.= is for assignment. Pascal’s <> not equal will not work. == and != work on booleans too, often saving a forest of if/elses. |
| 7 | & | Left (infix) | Bitwise AND mostly for for ints. |
| 8 | ^ | Left (infix) | XOR for ints. It is the difference operator. It is true if the boolean
operands are different, e. g.
false ^ false == false false ^ true == true true ^ false == true true ^ true == false It is useful in cryptography because of this magic property of encryption and decryption with a random scrambler number.
long encrypted = message ^ scrambler; long decryped = encrypted ^ scrambler; If you XOR twice with the scrambler, you get right back where you started. For booleans it is clearer to use a != b instead of a ^ b and a == b instead of !( a ^ b) |
| 9 | | | Left (infix) | bitwise OR mostly for ints.
int e = ( a & b << 2 ) | ( c & ( d >>> 1 ) ); // if you trust precedence can be written more tersely: int e = a & b << 2 | c & d >>> 1; |
| 10 | && | Left (infix) | short circuit logical AND for booleans. |
| 11 | || | Left (infix) | short circuit logical OR for booleans. So when you say:
if ( ( ( lowest <= a ) && ( a <= biggest ) ) || notNeeded )all those parentheses are nugatory (computer jargon for not necessary but won’t hurt). You could just as easily have written it, trusting precedence, as: if ( lowest <= a && a <= biggest || notNeeded ) |
| 12 | ? : | Right (ternary) | |
| 13 | = *= /= += -=
<<= >>= >>>= &= ^= |= |
Right (infix) | a += b means a = a + b; a *= b means a = a * b; etc. These make proofreading easier by eliminating typing a variable name twice. |
| Java Keywords | ||||
|---|---|---|---|---|
| abstract | do | import | public | throws |
| boolean | double | instanceof | return | transient |
| break | else | int | short | try |
| byte | extends | interface | static | void |
| case | final | long | strictfp | volatile |
| catch | finally | native | super | while |
| char | float | new | switch | |
| class | for | package | synchronized | |
| continue | if | private | this | |
| default | implements | protected | throw | |
| Reserved keywords (not currently in use) | ||||
| const | goto | |||
| Reserved Literals | ||||
| null | true | false | ||
| You can get the freshest copy of this page from: | or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror) | |
| http://mindprod.com/jgloss/jcheat.html | J:\mindprod\jgloss\jcheat.html | |
![]() | ||
| Canadian Mind Products | ||
| mindprod.com IP:[65.110.21.43] | ||
| view Blog | Your face IP:[38.107.191.109] | |
| Feedback | You are visitor number 229,253. | |