মঙ্গলবার, ৭ জানুয়ারী, ২০১৪

this Constructor


Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
Using this with a Field
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
For example, the Point class was written like this
public class Point {
    public int x = 0;
    public int y = 0;
       
    //constructor
    public Point(int a, int b) {
        x = a;
        y = b;
    }
}
but it could have been written like this:
public class Point {
    public int x = 0;
    public int y = 0;
       
    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}
Each argument to the constructor shadows one of the object's fields — inside the constructor x is a local copy of the constructor's first argument. To refer to the Point field x, the constructor must use this.x.
Using this with a Constructor
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation. Here's another Rectangle class, with a different implementation from the one in the Objects section.
public class Rectangle {
    private int x, y;
    private int width, height;
       
    public Rectangle() {
        this(0, 0, 0, 0);
    }
    public Rectangle(int width, int height) {
        this(0, 0, width, height);
    }
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    ...
}
This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables. The constructors provide a default value for any member variable whose initial value is not provided by an argument. For example, the no-argument constructor calls the four-argument constructor with four 0 values and the two-argument constructor calls the four-argument constructor with two 0 values. As before, the compiler determines which constructor to call, based on the number and the type of arguments.
If present, the invocation of another constructor must be the first line in the constructor.
Summery:
“this” constructor always calls the current method. It is used in constructor for the purpose of the calling of the current method. It is also used for the shortest calling of the method path. But it is the too shortest path of calling a method.



Prime Number Theorem

One of the supreme achievements of 19th-century mathematics was the prime number theorem, and it is worth a brief digression. To begin, designate the number of primes less than or equal to n by π(n). Thus π(10) = 4 because 2, 3, 5, and 7 are the four primes not exceeding 10. Similarly π(25) = 9 and π(100) = 25. Next, consider the proportion of numbers less than or equal to n that are prime—i.e., π (n)/n. Clearly π (10)/10 = 0.40, meaning that 40 percent of the numbers not exceeding 10 are prime. Other proportions are shown in the table.

A pattern is anything but clear, but the prime number theorem identifies one, at least approximately, and thereby provides a rule for the distribution of primes among the whole numbers. The theorem says that, for large n, the proportion π(n)/n is roughly 1/log n, where log n is the natural logarithm of n. This link between primes and logs is nothing short of extraordinary.


One of the first to perceive this was the young Gauss, whose examination of log tables and prime numbers suggested it to his fertile mind. Following Dirichlet’s exploitation of analytic techniques in number theory, Bernhard Riemann (1826–66) and Pafnuty Chebyshev (1821–94) made substantial progress before the prime number theorem was proved in 1896 by Jacques Hadamard (1865–1963) and Charles Jean de la Vallée-Poussin (1866–1962). This brought the 19th century to a triumphant close.