object references stored in stack, objects stored in heap
String s; // no objects connected
String s = "asdf"; // an object connected
newString s = new String("asdf");
sizeof function, whose main goal is for portability (C, C++)
a variable defined inside a scope is no longer accessiable outside of the scope
hide a variable in a larger scope is not allowed in Java
{
int x = 12;
{
int x = 96; // illegal
}
}
primitive types are released outside of the scope beacuse they are stored on the stack
objects persist past the end of the scope, references are released
Java use reversed URLs. Every file automatically live in their own namespaces. Each class within a file has a unique identifier.
Problem: deep directory path, many of them are empty (e.g. com)
static keywordfinal keyword| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-28T20:29:02Z |
final int valueOne = 9static final int INT_5 = rand.nextInt(20)final datafinal makes the value a constantfinal makes the reference pointed to constantfinal has nothing to do with Inheritance, it only indicates that outside
of the constructor, the field cannot be changedclass A {
final int i;
protected A(int ii) {
i = ii;
}
}
class B extends A {
public B() { super(1000); }
public void print() {System.out.println(i);}
public static void main(String[] args) {
new B().print();
}
}
/* Output
1000
*/
finalBlank finals are finals fields without initialization values. Variables with blank finals can be initialized later, so it’s possible to have different final values for same field in different instances.
class BlankFinal {
private final int i;
public BlankFinal() { i = 1; }
}
final argumentsfinal arguments cannot be changed inside the methodfinal methodsPrivate methods in a class are implicitly final
Define methods using the same name with private methods in base class is not Overriding
public class PrivateOverride {
private void f() {
System.out.println("private f()");
}
public static void main(String[] args) {
PrivateOverride po = new Derived(); po.f();
}
}
class Derived extends PrivateOverride {
public void f() { System.out.println("public f()"); }
}
/* Output:
private f()
*/
final classPrevent all inheritance from this class
| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-23T18:22:21Z |
Any mathematical or bitwise operations on primitive data types smaller than int (char, byte, short), those values are promoted to int before performing the operations, and the resulting values are int, so when assigning back, there might be some losing information.
If arithmetic operators not combined with equal operator, the result (right side) must be explicitly cast back to the original type:
short x = 1, y = 2;
s = (short) x * y;
s = (short) ~y;
<<, inserting zero at the lower-order bits>>, value positive: inserting zero at the higher-order bits, value negative: inserting one>>>, unsigned right shift, inserting zeros| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-23T18:17:48Z |
if left operand is int, only FIVE lower-order bits of the right-hand side are used to prevent shifting more than the number of bits in an int
e.g., 5>>32 -> 0
if left operand is long, only SIX lower-order bits of the right-hand side are used
| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-23T18:21:03Z |
If left operand is byte or short, they promoted to int,
then truncated when assigned back, so sometimes get -1.
short s = -1; // 1111-1111-1111-1111
System.out.println(Integer.toBinaryString(s));
s >>>= 10;
// s >>> 10:
// promoted to int first: 1111-1111-1111-1111-1111-1111-1111-1111
// right shift 10: 0000-0000-0011-1111-1111-1111-1111-1111
// assign back, truncated: 1111-1111-1111-1111, s remains same
System.out.println(Integer.toBinaryString(s));
s = -1;
s >>>= 17;
System.out.println(Integer.toBinaryString(s));
/*out
11111111111111111111111111111111
11111111111111111111111111111111
111111111111111
*/
byte b = -1;
System.out.println(Integer.toBinaryString(b));
b >>>= 10;
System.out.println(Integer.toBinaryString(b));
System.out.println(Integer.toBinaryString(b>>>10));
/* out
11111111111111111111111111111111
11111111111111111111111111111111
1111111111111111111111
*/
null) are actually created| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-26T23:02:44Z |
public class VarArgs {
static void printArray(Object[] args) {
for (Object obj: args)
System.out.print(obj + " ");
}
public static void main(String[] args) {
printArray(new Object[]{
47, (float) 3.14, 11.11, "string"});
}
}
void printArray(Object... args) {
for (Object obj: args)
System.out.print(obj + " ");
}
void printArrayOptional(String arg1, Object... args) {
}
void main() {
printArray(47, (float) 3.15, 11.11, "one");
printArray((Object[])new Integer[]{1, 2, 3, 4});
printArray();
printArrayOptional("111", 1, "3", 4.32);
printArrayOptional("111");
}
void f(float i, char... args);
void f(char... args);
// call with
f('a', 'b'); // won't compile
| position | ease | box | interval | due |
|---|---|---|---|---|
| front | 2.50 | 3 | 6.00 | 2021-01-26T23:30:20Z |
public enum Spiciness {
NOT, MILE, MEDIUM, HOT, FLAMING
}
public class SimpleEnumUse {
public static void main(String[] args) {
Spiciness howHot = Spiciness.MEDIUM;
System.out.println(howHot);
for(Spiciness s : Spiciness.values())
System.out.println(
s + ", ordinal " + s.ordinal());
}
}
/* output:
MEDIUM
NOT, ordinal 0
MILD, ordinal 1
MEDIUM, ordinal 2
HOT, ordinal 3
FLAMING, ordinal 4
*/
switchswitch (degree) {
case NOT:
case MILE:
default: break
}
%n handles different line endings on different platforms when using System.out.printf or System.out.formate or E means “ten to the power”, e.g., 1.38e-43f means 1.38 * 10-43goto is a reversed word in Java, but it’s not usedbreak label and continue label