Chapter 2: Everything is an Object
Where storage lives
1. Registers - the fastest and most limited; you don’t have direct control of this
2. The stack - lives in RAM; very fast, second only to registers; moves stack pointer up or down to release or create new memory respectively; some Java storage exists on the stack, like object references but Java objects themselves are not placed on the stack
3. The heap - general purpose pool of memory; also in RAM; much more flexible than the stack because the compiler does not need to know how much storage or how long the storage must stay ont he heap; it takes more time to allocate heap storage than the stack
4. Static storage - contains data that is available the entire time a program is running; use the static keyword, however Java objects are never placed into static storage
5. Constant storage - constant values are often placed directly into the program code although it can optionally be placed into ROM in embedded systems
6. Non-RAM storage - lives completely outside of the program; two examples: streamed objects (e.g. bytes sent from another machine) and persistent objects (e.g. placed on disk)
Fields and methods
When you define a class you can put two elements into your class: fields (or data members) and methods (or member functions). The fundamental parts of a method are the name, the argument, the return type, and the body
The static keyword
Two reasons:
1. Have only one piece of storage for particular piece of data regardless of how many objects are created, or even if no object is created (static data)
2. You need a method that you can call even if no object is created (static method)
Chapter 3: Controlling Program Flow
Ternary if-else operator
boolean-exp ? value0 : value1
Literals
Use literals when the type is ambiguous by giving the compiler additional information. For example:
public class Literals {
char c = 0xffff; // max char hex value
byte b = 0×7f; // max byte hex value
short s = 0×7fff; // max short hex value
int i1 = 0×2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long.
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix
long n3 = 200;
//! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power i.e. 27 x 10^-47
}
A situation where this would be required is float f = 1e10f, because exponent numbers are expected to be doubles and without this suffix, a casting error will occur
Promotion
If you perform mathematical or bitwise operations on char, byte, or short, the result will be int. You will need to explicitly cast back to the required type (although you may lose information). Promotion generally means the largest data type in an expression will determine the size of the result of that expression.
Java has no “sizeof”
C and C++ have sizeof for portability since different data types might be different sizes on different machines. Java does not need sizeof beacuse all the data types are the same size on all machines.
Example of a multi-variable for statement
for(int i = 0, j = 1;
i < 10 && j != 11;
i++, j++)
/* body of for loop */;
Labels
Labels are used with loops to provide additional control flow and can be called by break or continue. For example:
outer:
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
System.out.println(”j:” + j);
if (j == 2)
{
continue outer;
}
}
}
Math.Random
Math.Random() generates values from [0,1), that is 0 is inclusive.
Chapter 4: Initialization & Cleanup
Overloading
Often the same word expresses a number of different meanings is overloading
Calling constructors from constructors
Use the this keyword with the parameters for the required constructor
e.g.
Cellphone(int cost){
// do stuff
}
Cellphone(String name){
this(500);
// do more stuff
}
Array declaration
You can use both int[] arr; or int arr[]; there are two styles to conform to what C and C++ programmers expect
Chapter 5: Hiding the Implementation
Nothing
Chapter 6: Reusing Classes
Lazy initialization - creating the object only when necessary to reduce overhead. That is, don’t automatically create an expensive object before the constructor or in the constructor if it may not be used.
The final keyword
1. Final data
Tell the compiler the data is constant, can be compile-time constant or a value that doesn’t change after it has been initialized during runtime. With primitives the value is constant, however for final objects, the reference is constant but the object itself can change. This is important when thinking about arrays, which are objects (you cannot stop the array values from changing!)
Blank final
Fields declared as final but with no initialization value
Final arguments
You cannot change what the argument reference points to
2. Final methods
Two reasons: prevent overriding by inheritance and and for efficiency. For efficiency, making a method final turns any calls to that method as an inline call. That is, the method call will be replaced with a copy of the code from the body. However, the JVM is usually smart enough to detect this, so you should not worry about it.
Final and private
Any private method is implicitly final
3. Final classes
You don’t want to allow subclassing