char ch = 'A';
char | String | |
---|---|---|
Type of value | primitive | object |
Memory usage | 2 bytes | depends on length |
Methods | none | length, toUpperCase, ... |
Number of letters | exactly 1 | 0 to many |
Surrounded by | apostrophes: 'c' | quotes: "Cat" |
Comparing | <, >=, ==,... | equals |
char newline = '\n';
char tab = '\t';
char quote = '\"';
char letter = 'a' + 2; // stores 'c'
Note: 'a' is Unicode value 97. Thus 2 more is 99, or 'c'.
int code = 66;
char grade = (char) code; // stores 'B'
public static int count(String text, char c) {
int found = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == c) {
found++;
}
}
return found;
}
System.out.printf(<format string>, <parameters>, ..., <parameters>);
red yellow green
purple pink orange
System.out.printf("%10s %10s %10s\n%10s %10s %10s\n", color1, color2, color3, color4, color5, color6);
do {
<statement>
...
<statement>
} while (<test>);
for (the length of the fence) {
plant a post.
attach some wire.
}
Better - Note the reversal (re-ordering) of the actions
plant a post.
for (the length of the fence) {
attach some wire.
plant a post.
}
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The code - note the printing of first item outside the loop then second action first
inside the loop, and change in starting i value.
System.out.print(1); // plant post
for (int i = 2; i <=10; i++) {
System.out.print(", "); // attach wire
System.out.print(i); // plant post
}
for (the length of the fence) {
plant a post.
if (this isn't the last post) {
attach some wire.
}
}
Consider the previous problem - outputting: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
for (int i = 1; i <=10; i++) {
System.out.print(i); // plant post
if (i != 10) {
System.out.print(", "); // attach wire
}
}
sum = 0.
while (we haven't seen the sentinel) {
prompt and read.
add it to the sum.
}
Better - Classic fencepost (aka "prime the pump" and "loop-and-a-half")
sum = 0.
prompt and read.
while (we haven't seen the sentinel) {
add it to the sum.
prompt and read.
}
Introduce a variable - Same as above but variable n included
sum = 0.
prompt and read a value into n.
while (n is not the sentinel) {
add n to the sum.
prompt and read a value into n.
}
Operator | Meaning | Example | Value |
---|---|---|---|
&& | AND (conjunction) | (2 == 2) && (3 < 4) | true |
|| | OR (disjunction) | (1 < 2) || (2 == 3) | true |
! | NOT (negation) | !(2 == 2) | false |
P | !p |
---|---|
true | false |
false | true |
p | q | p && q |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
p | q | p || q |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
Description | Operator |
---|---|
unary operators | !, ++, --, + (positive), - (negative) |
multiplicative operators | *, /, % |
additive operators | +, - |
relational operators | <, >, <=, >= |
equality operators | ==, != |
logical AND | && |
logical OR | || |
assignment opertors | =, +=, -=, *=, /=, %=, &&=, ||= |
public static boolean isTwoUniqueDigits(int n) {
if (n >= 10 && n <= 99 && (n % 10 != n / 10)) {
return true;
} else {
return false;
}
}
Better:
public static boolean isTwoUniqueDigits(int n) {
return (n >= 10 && n <= 99 && (n % 10 != n / 10));
}
!( str == null || x >= str.length() ) // Not ( null string object or loop counter greater than string length)
!( n >= 1 && n <= 9) // Not a single digit number