Sei sulla pagina 1di 10

Java after intro

Getting your hands dirty

The classloader
An object responsible for loading classes
Converts a named class into the bits responsible for implementing that class
Classes are introduced into the JVM when they are referenced by name in a class
that is already running. (just a bit of magic is required to get the main class loaded)
For the defauld ClassLoader classes must be on the Classpath .
The Classpath defines what you can use in your program. Java SE classes are
automatically added.

Name collisions

Im Bob!

Who is right? Why cant there be 2 classes named Bob?


Because Bob is not his full name!
package to the rescue!

Im Bob

The fully qualified name of a class is your.package.name.Bob

Package naming convetions:

lowercase
words separated by dots
uniqueness comes from reversing the organization domain name (which is unique) -> ro.academyplus.Bob

ro, ro.academyplus, ro.academyplus.people etc. are independent packages, although ro. academyplus is called a subpackage of
ro there is no relationship between them

Hidding your data


Remember access modifiers?
1.
2.
3.
4.

private - only class members can access


protected - only class members and subclasses can access
package - only class members and classes from the same package
public - anyone can access

Access modifiers can be applied also to classes not only members of a class
1 public class / file

Bringing objects to life


Creating an object always involves a constructor.
A constructor is not manadatory. A default one is provided.
Adding an explicit contructor removes the default constructor.
Constructors are a means to setting up initial object state.

Multiple constructors are allowed:


public Client(){
account = new Account( 0.0);
}
public Client(float initialBalance){
account = new Account(initialBalance);
}

Arrays

Efficient
Fixed size that cannot be changed after creation
Can store primitives
Arrays are Objects

int[] numbers; // uninitialized


int[] numbers = new numbers[10];// an array which can hold 10 int values
System.out.println(Arrays.toString( numbers));// print the contents of an array
int[] numbers={1,2,3,4}; //initialize with default values

Primitive autoboxing
int a = 2;
Integer b = 2;
Integer c = new Integer( 2);
String two = "2";
boolean doStuff() {
return a == b; //true
}
boolean doMoreStuff() {
return b == c; // false;
}
boolean doMagicStuff() {
return b.equals(Integer. parseInt(two)); // true
}

The most overwritten methods


public String toString();

Called by print, println etc.


Provides a text representation of your object.
Always overwrite it. Makes debugging easier.
Default implementation from Object prints the refference value.
Same goes for equals/hashCode.

Overloading stuff
int compute( int a, int b){
return a*2+b*3;
}
int compute( int a){
return a*(a+1)/2;
}
float compute( int a){ // nope this is not overloading
return a*(a+1)/2.2;
}
float compute( float r) {
return 3.14f * r * r;
}
String compute( float r, boolean area) {
return (area ? 3.14f * r * r : 2 * 3.14 * r) + "";
}

Exercises
Heroes: Orc, Elf, Knight, Mage
Villains: Devil, Goblin, Necromancer, Dark mage
Artifacts: Amor, Helm, Sword, Staff, Bow, Axe
Implement: toString, constructors
What you should have in your classes: a way to compute the damage, a way to take damage, artifacts .

Potrebbero piacerti anche