[关闭]
@smilence 2017-01-14T22:04:41.000000Z 字数 4314 阅读 1654

JAVA for N00bs

NewChapter


HEADS-UP

reference reference reference reference reference...WTF is "reference"?

"Reference is a relation between objects in which one object acts as a means by which to connect to or link to, another object. The first object in this relation is said to refer to the second object. "

The first object is the reference, the second is the actual instance/object.

final keyword for variable

class Instance {
    public static void main (String[] args) {
        final int x = 3;
        x = 4; // error: cannot assign a value to final variable x

        final Apple a = new Apple(1);
        a.setSize(2);
        a = new Apple(3); // error: cannot assign a value to final variable a
    }
}

class Apple {
    private int size;
    static final int maxSize = 100; //static keyword for variable

    Apple(int s) {
        setSize(s);
    }

    public void setSize(int s){
        if( s > Apple.maxSize )
            System.out.println("the size is invalid - too large");

        size = s;   
    }
}

Immutable vs final: String, Integer.

How about:

String str = "abc";
str = "xyz";

Read this whenever possible! http://docs.oracle.com/javase/tutorial/java/index.html

JAVA always PASS-BY-VALUE

e.g.

void swap(int a, int b) {
  temp = a;
  a = b;
  b = a;
}

swap(x,y);

'pass by reference` means a reference to a variable is passed to a method.Java does not do this.Java manipulates objects by reference, but it passes object references to methods by value.

Passing Primitive Data Type Arguments

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost. Here is an example:

public class PassPrimitiveByValue {

 public static void main(String[] args) {

   int x = 3;

   // invoke passMethod() with 
   // x as argument
   passMethod(x);

   // print x to see if its 
   // value has changed
   System.out.println("After invoking passMethod, x = " + x);

}

 // change parameter in passMethod()
 public static void passMethod(int p) {
   p = 10;
 }
}

When you run this program, the output is:

After invoking passMethod, x = 3

Passing Reference Data Type Arguments

Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before. However, the values of the object's fields can be changed in the method, if they have the proper access level.

For example, consider a method in an arbitrary class that moves Circle objects:

public void moveCircle(Circle circle, int deltaX, int deltaY) {
    // code to move origin of circle to x+deltaX, y+deltaY
circle.setX(circle.getX() + deltaX);
circle.setY(circle.getY() + deltaY);

// code to assign a new reference to circle
    circle = new Circle(0, 0);
}

Let the method be invoked with these arguments:

moveCircle(myCircle, 23, 56)

Inside the method, circle initially refers to myCircle. The method changes the x and y coordinates of the object that circle references (i.e., myCircle) by 23 and 56, respectively. These changes will persist when the method returns. Then circle is assigned a reference to a new Circle object with x = y = 0. This reassignment has no permanence, however, because the reference was passed in by value and cannot change. Within the method, the object pointed to by circle has changed, but, when the method returns, myCircle still references the same Circle object as before the method was called.

OVERLOADING

The same blackbox, given different input, perform different operations.

The concept of "reference"

public User(String username, String password);

public User(User usr);//Deep copy, references are not equal

User user2 = new User();
User user3 = new User(user2);

.equals or == ?

INHERITANCE & SUPER KEYWORD

Is a and has: Apple(child class) is a kind of Fruit(parent class), so it has all the attributes(field) and behaviors(method) of Fruit.

this keyword: "The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter."

Naming convention

public class Employee extends Person { //"Employee is a kind of Person"
  private String ssn;
  private int salary;

  public Employee(String name, int age, String SSN, int Salary) {
   super(name, age); // equivalent to this.name = name; this.age = age;
   ssn = SSN;  // this.ssn = SSN
   salary = Salary; //this.salary = Salary
  }
}


Person p = new Employee(name, age, SSN, Salary); //p cannot reference the Employee-only fields
Employee e = new Person(); //e cannot reference a super class instance

instanceof function

One class can have at most one parent class.All classes extends Object class.

OVERRIDING & FINAL KEYWORD

@Override annotation

Person person = new Employee();
person.details(); // Fields are determined at compile time, but non-static member methods are at runtime!!!!

final

public final class CreditCardProcessor  {



}

public class CreditCard {
  public final bool checkValid() {}
}

static method can only access static fields/methods.Make a lot sense here.

static method can be accessed by class name or an instance of that class(not recommended):
Person.details() or person.details()

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注