
Chapter 1:
camelCase: first letter small.
static final=constant. constants are UPPERCASE.
only one public class per source code file. It's name must match the file.
access means visibility.
public allows access between packages.
strictfp: on class/method. makes floating points platform independent.
Can methods change from under a class?
abstract methods change with i.
Interface methods: public abstract by default.
Interfacde vars: public, static, final.
Interface has no static methods.
An interface can only extend other interfaces.
Interface classes and methods are public abstract.
Interface constants are: public static final. Weather declared that way or not.
this is implicit.
Trying to use a private function from a subclass gives a compiler error.
A subclass cannot even know anything private to the superclass. Also within the same package.
I am protected through packages.
Protected vars: can access through direct inheritance.
Subclass outside the package can access only through inheritance.
Protected in child.. can access directly, but no through new reference.
Once inherited, that protected thing is only accessible to subclasses.
no modifier means default access.
There are newer access modifiers on local vars, except for final.
Abstract functions dont even have curly braces.
Overload is not an implementation.
only functions can be native.
only functions and classes can be strictfp.
constructors cant be: static/final/abstract.
There are no final objects, only final references.
Cannot declare enums in methods.
Chapter 1:
identifier naming rules:
-start with \w, $ or _.
-any number of characters.
-after first character, numbers are allowed.
-no keywords, see table 1-1.
-case sensitive
interface classes define WHICH methods must be implemented, but not HOW.
interfaces are by default public, abstract.
static and final = constant.
setters must be public, void.
cohesive=clearly defined roles of classes.
a package is to prevent two different classes having an identical name.
has access to=can create instances of, can inheritc, might access variables and functions.
a class with default access can only be seen by classes in the same package.
default=seen within package.
public=seen between packages. public classes still need to be imported.
strictfp modifies a class. prevents platform dependent behaviour.
final means noone can override your methods.
if one method is abstract, the whole class must be abstract.
abstract final is pointless and does not compile.
an interface is a contract for what a class can do.
a class that implements an interface must implement all the interfaces methods.
all functions of a interface MUST be public abstract.
an interface can only extend more of itself.
interface variables are by default public static final.
classes= default public
members=private,default,protected,public
accessing and inheriting is not the same thing.
if a member is public, it gets inherited regardless of package state.
when inheriting you cannot access private members.
a private method cannot be overridden.
protected allows inheritance between packages.
default: inheritance wont work between packages.
protected means parent->rules!
subclasses can only access through inheritance.
They cannot create new instances of the superclass and access that.
at inheritance it becomes private except for, for greater subclasses.
private cannot be sent to a function.
final prevents a method from being overridden in a subclass.
abstract methods are empty, and subclasses must implement them.
if class has even one abstract method, it must be abstract itself.
The first concrete class of an abstract class must implement all abstract methods of the superclass.
an overload is not enough, it must actually implement it.
native means a method is implemented in platform dependent code.
a varaible can never be strictfp.
constructors cant have return types and they share name with the class.
they cant be static, final, abstract or void.
BASIL without the A. And doubly floating.
primitives: byte short int long, double flood/float.
1,2,4,8,4,8.
signed: 1 means negative.
char=2, without the signed.
instances: can be final transient.
cannot be marked: abstract, synchronized, strictfp, native or static.
local variables dont get default values.
they must be initialized.
shadowing: declare variable with same name as instance
the brackets can be anywhere.
final:
class cannot be subclassed,
method cannot be overridden,
variable cannot be changed.
transient: doesnt save when serializing.
static=independent of instances.
javabeans=set/get, add/remove or is.
cannot declare enums in methods.
extends=is a.
Chapter 2:
doShapes(Gameshape shape)//can also take any subclass of it.
A class that implements an interface passes the is-a test for it.
Animal b = new Horse;
animal ref, horse object.
b.eat();//runs horse version.
cant change access when overriding functions.
overriding rules:
-same arg list,
-same or subtype return.
-same or less restrictive access.
-in different package can only override public/protected.
CAN, make new exceptions.
CANNOT make broader exceptions.
No final or static.
Can't override what you don't inherit.
super=execute super method, then do this.
Overload: CAN declare new or broader exceptions.
CAN change return.
Arg ref determines which oveloaded version is called.
Animal joe = new Horse();
joe.eat();//Horse->eat.
Animal joe = new Horse();
joe.eat("carrots"); //compiler error, finds no eat in Animal.
Dog d = (Dog) animal;//downcast.
the compiler is forced to trust us on this.
it compiles but may fail later.
if any superclass has implemented an interface, then that is done.
interface can do nothing to a class.
extend must be before implements.
with objects, can return null.
can return primitives that can convert to.
can return an instance of a subclass.
All classes have constructors.
All objects are created with constructors all the way up.
constructors have no return type, and same name as class.
If you type in a constructor then there is no default constructor.
Only constructors can call constructors.
ANY return type means it is not a constructor.
When calling super constructor u must provide the appropriate arguments.
constructors are never inherited or overridden.
stackoverflow when back and forth forever.
static method cannot access a non static method.
Animal []a={new Animal(),new Dog(),new Animal()}
for (int x...
a[x].dostuff //calls the animal thing. the compiler turnns a[x] into animal.
coupling: what one class knows about another. The less the better.
cohesion: single, well focused purpose.
private=only an instance can access it.
classes implement interfaces.
classes extend one another. Extends means IS-A.
constructors dont return anything, methods do.
HAS-A means u have a reference to it. U have a reference to that code.
all java objects are in a way polymorphic cause they pass at least two IS-A tests. except for the class object, off course.
a reference variable can refer to any subtype of the type.
Dog d = (Dog) Animal; //downcast.
abstract classes dont need to implement anything.
interfaces extend one another. that means they simply add up.
classes/interfaces extend one another. (extend=within)
classes implement interfaces.
when return type is a primitive, it is okey to return any primitive return type. For instance, int or string.
when only some of the subclasses need to be able to do something, such as animate themselves.
then use interface and have just those classes implement it.
Animal b = new Horse(); //Animal ref, but a Horse object
first ref.
ref decides what objects method is called.
Dog d = (Dog) animal;//downcast.
abstract classes can implement whatever the hell they want.
when overrriding you can return a different thing, as long as its a subclass of the original thing.
returning null is always ok.
can return things that can be converted or casted to the stated type.
if there is a constructor with some args, the compiler wont make a default one with no args.
static methods cant be overridden.
loosely coupled classes only know of each other through inheritance.
cohesion=the degree to which a class has a single well focused purpose.
abstract methods have no body.
a final method cannot be overridden.
a private method cannot be seen.
plymorphism:
coupling,dependency: degree to which each module relies on the others.
cohesion: how strongly related the functions of a module are.
Chapter 3:
184,185 wtf?
Octal: starts with 0 (21 digits max)
Hex: starts with 0x (16 digits max)
is int by default, but if followed with l, is long.
floating point literal = double(64 bit)
float g = 49837.0424F;
commas won't compile. D is default behavior.
double d = 3.63D;
int x=1; if(x){}//compile error.
characters are just 16 bit unsigned ints.
char a = 0x892;
char b=982;
char c = (char)70.000;//without cast, get error.
byte c = b + c;
turns b + c into int automatically.
possible loss of precisino, narrowing.
int x = 36.55;//illegal
itn x = (int) 36.55;
floating points are implicitly a double
float g = 32.2f;//works.
value of a negative number=flip all bits, add one.
int a = 6;
int b = a; they have sep.values.
parent_class ref_name = new Child_class;
legal because its child.
must be parent->child.
if the object is a parent it might be asked to do things it doesn't know how to.
precendence:
static, instance, local, block.
array elements r always given default values.
automatic var: local, must be assigned. inside public static void main.
if its not certain a var is initialized, compile error.
local object refs: are default to nothing, not even null.
to check for null, var must be initialized.
instance var refs; default=null
local var refs; default is nothing
218, dont get the last.
int[5] scores;//does not compile.
int []testscores;//declares the array
testscores=newe int[4];//constructs the array.
arrays must be given a size when they are constructed:
int [] carlist = new int[];//does not compile.
new causes constructor all the way up to run.
int[][]myArray=new int[][];
myArray ---->
array out of bounds is a runtime exception.
negative is also out of bounds.
Animal[]pets=new Animal[3];
one array object,
with three new refs,
but no animal objects.
lengt 4 = 0 to 3.
int[]dots={6,x,8}
int[] testscores;
new object[3] {null,..,..}
not legal, size must be specified.
an int array can contain anything that fits within an int: byte, char,short..
can also cotnain subclass objects of its declared type.
interface array can refer to any instance of a class that implements that interface.
int array cannot get=to strings array.
explicit cast will not help.
when assigning the arrays must have teh same dimensino quantity.
in twod array must be put array at x[2];
static initialization blocks run once, when the class is started.
instance initialization block runs everytime an instance is created.
instance inits run after super.
1.static inits
2.constructors
3.instance inits
Integer p = new Integer(567);
y++;
makes a new object and refers to it.
==refers to the reference.
primitives equal when they have the same value, even if they r refs.
when multiple constructors and none fits, take the smallest wider.
the compiler chooses widening over boxing.
boxing=begins with capital letter.
it tends to widen more.
widening beats var args.
but specified var number beats var args.
by can widen to an object though.
#################################
heap: instances. heap instances.
stack: local.. local stack.
integers can be base 8,10 and 16.
octals have a zero infront. 21 digits max.
hexes have a 0X infront, and can be capital or not. up to 16 digits.
can have l after it. then its a long.
literals are the actual numbers. they are double by default.
commas are not allowed.
booleans can only be true or false.
numbers cannot represent booleans.
int x = 1;if (x) {} // Compiler error!
Octal: 010=8, 011=9
Hexadecimal: 0xDeadCafe.
long jo = 110599L;
never use comma.
numbers are ints by default. they are implicitly, an int. ints are 32 bits.
byte b = 27;
is identical to:
byte b = (byte) 27;
it automatically casts the thing.
expression with ints or smaller is always an int.
byte c = (byte) (a + b); //without the cast there would be compile error. it always goes to int automatically.
putting a bigger thing into a smaller container=narrowing. requires an explicit cast.
by explicitly casting you ar saying are aware of the danger and accept responsibility.
float a = 100.001f;
int b = (int)a; // Explicit cast, the float could lose info.
32.3 is automatically a double. (64 bit)
float is 32 bit.
so to make it legal it must be:
float f = (float) 32.3;
float g = 32.3f;
+= spares you from that turning the thing to int automatically thing.
int a = 6;
int b = a;
just copies the content itself.
static is forever.. so to speak.
instance variables live with the instance.
when out of scope they usually say: cannot find symbol.
0,0.0, null and '\u0000' are the default values.
String t = s.toLowerCase();//runtime exception, because s is a zero.
automatic variables:
-must be assigned a value.
-are defined within curly braces of a method.
local variables must be initialized.
cant work with a variable that may not have been initialized. compiler will complain.
local instances dont get a default value.
not null, not anything. they have no value.
locally, nothing gets a value.
when using string references, new objects are created when they are being changed.
object references are very able to change objects in the calling place.
functions cant chagne the original reference, but they can change the object iself.
int[5] scores;//wont compile because u cant decide array size there.
int[] carList = new int[]; // Will not compile; needs a size
out of bounds is a runtime exception.
int[] dots = {6,x,8};
an int array can hold any value that can fit into a 32-bit int variable.
an array can have subclasses of the declared type of the array.
characters are just 16 bit unsigned integers under the hood.
characters can also be assigned hex and octs.
implicit casts happen automatically.
explicit cast is needed when putting a big thing into a small container.
initializae blocks: don't have names, they can't take arguments, and they don't return anything.
initialize runs before the constructor is run.
all wrapper constructors except string have two constructors. one for the primitive and one for string.
character only takes character.
with boolean, only the string "true" equates to true. everything else goes to false.
Integer i2 = new Integer(42);
byte b = i2.byteValue();
double d4 = Double.parseDouble("3.14");
when having references to strings, if one reference changes the string, it only changes the string for itself.
arrays must always be given a size.
int[] carList = new int[]; // Will not compile needs a size
a reference that has not had an object assigned to it is a null reference.
trying to access an array out of range is runtime exception.
will be in a complex loop.
new Object[3] {null, new Object(), new Object()} // not legal size must not be specified
IS-A is checked with instanceof.
order: static init, instance init, constructor
wrappers:
default: char-int.
char=only char.
bool=bool-string.
xxxvalue, parsexxx
Chapter 4:
x*=2+5
x=x*(2+5)
expression on right always goes in parenthesis.
enum color {red,blue}
color c1=color.red; color c2=color.red;
if(c1==c2){..}
if(c1.equals(c2)){...}
((B)a).dobstuff();
downcasts a to B.
null instance of always returns false.
compilation fails when testing instance of between hierarchies.
arrays are always instances of objects.
a="string";b>c=7;
a+b+c="string37";
a+(b+c)="string10";
|| is short circuit. the longer is the short circuit.
|| and && only work with booleans.
^=Xor=exactly one.
42L=42F works!.
x*=2+5//x=x*(2+5); // the expression on the right is always placed in parenthesis.
==looks at the bit pattern of the value. can only compare compatible types.
floating point can be compared to an integer and usually acts as excpected.
refernce == tests if they reference the same object.
instanceof fails across hierarchies. a dog can never be a cat for instance.
all arrays are objects.
incrementing a final variable=>compiler error.
|| and && only works with boolean operands. if (5 && 6) will not compile.
If either operand is a String, the + operator becomes a String concatenation
operator. If both operands are numbers, the + operator is the addition operator.
if try to increment a final variable => compiler error.
| evaluates both sides brutally.
|| is short circuit.
^=XOR.
you CAN compare long to floats.
evaluating out of bounds means out of bounds exception.. on runtime.
| allows var to be incremented. doubles prevent incrementor from actually incrementing.
Chapter 5:
int x=3;
switch(x){
case x:
default:
}
final inst b;
b=2;//wont work within switch.
case must be theoretically possible, it may not pass boundaries.
case 80:
case 80:
wont compile.
switch(new Integer(4))//ok. MUST BE: case x:
once a case is matched, it will execute all the way down.
default works just like any other case with fall through.
while(x=5)//wont compile.
for: only one middle expression is allowed.
for(;;)
vars in () r local to the loop. must be of same type.
for(x y)
x cant be already declared.
cant stuff long into an int.
Exceptions:
first line that fails stops entire try.
finally always happends at end, just before return is implemented.
finally always runs. try must have at least either catch or final.
catch must come straight afetrr. finally must come in after last catch.
no sneaky code allowed within try/catch/finally.
narrower first, then widen.
###############################
There must be either catch or finally.
and they must come right after the try.
cases with same value twice wont compile.
in loops you can have only one test expression. if you have many you must put them in brackets.
a variable outside the loop can be used after the loop.
but if its only declared inside the loop it cannot be used after it.
a try must be followed by either a catch or a finally.
catch must follow immediately. no code can be sneaded in between.
or exceptions have java.lang.exception somewhere up there.
Chapter 6:
stringbuffer-builder.
builder is non-synchronous.
buff to build.
string x = "java".
x.concat("rules!");
print x //prints "java";
x.concat just creates an extra object.
x=x.concat creates an object and moves the ref to it.
String s = new String("abc");
creates two objects, oen in constant pool and one not.
charAt begins at 0.
x+=changes the ref as well.
length returns reg.number of characters.
x.substring(5,8);//5,6,7, from 0123..
trim removes leading and trailing whitespaces.
sb.append changes the ref as well.
doesnt create objects as before.
sb.delete(4,6); removes (6-4),2, starts after 4.
buffbuild objects r changeable.
451 comes on the exam, is fuzzy.
File file = new File("foo");
never creates a file, refs it if it exists.
read password returns an array,
which can be completely deleted from memory.
console c = system.console();
transient vars can be saved manually.
if a class is serializeable then that goes all the way down the inheritance tree.
deserializing does not call constructors.
transient vars always go back to null.
non serialized constructors will run when reading back.
when serializing collections and arrays, all objects must be serializable.
Calendar c = Calendar.getinstance();
is abstract so new is illegal.
Date d1=new Date.
Calendar c = Calendar.getinstance();
c.SetTime(d1);
roll never changes bigger variables.
a matched string generally cannot be reused.
+/*/?=greedy. +?/*?/?? = reluctant.
String pattern="\\d"; //one \ breaks.