JAVA MATERIAL USEFUL FOR FRESHERS
Basic Java Refresher.
A quick run-through of basic Java features and syntax in a single handout.
Student Java Example
• As a first example of a java class, we'll look at a simple "Student" class. Each Student
object stores an integer number of units and responds to messages like getUnits() and
getStress(). The stress of a student is defined to be units * 10.
Implementation vs. Interface.
• In OOP, every class has two sides...
• 1. The implementation of the class -- the data structures and code that implement its features.
• etc........
Student Client Side.
• First we'll look at some client code of the Student class.
• Client code will typically allocate objects and send them messages.
• etc.....
Object Pointers.
• The declaration "Student x;" declares a pointer "x" to a Student object, but does not
allocate the object yet.
• Java has a very simple and uniform memory system. Objects and arrays are allocated in
the heap and accessed through pointers.
• etc....
new Student() / Constructor
• The "new" operator allocates a new object in the heap, runs a constructor to initialize it,
and returns a pointer to it.
- x = new Student(12)
• Classes define "constructors" that initialize objects at the time new is called.
Constructors are similar to methods, but they cannot be called at will. Instead, they
are run automatically by the JVM when a new object is created.
• etc.....
Message send
• Send a message to an object.
- a.getUnits();
- b.getStress();
• Finds the matching method in the class of the receiver, executes that method against the
receiver and returns.
• etc......
Object Lifecycle
• The client allocates objects and they are initialized by the class ctor code
• The client then sends messages which run class method code on the objects.
• etc.....
Student Client Side Code
// Make two students
Student a = new Student(12); // new 12 unit student
Student b = new Student();// new 15 unit student (default ctor)
etc........
Student Implementation Side
• Now we'll look at the implementation of the Student class. The complete code listing
for Student is given at the end of this section..
Class Definition.
• A class defines the instance variables and methods used by its objects.
• Each variable and method may be declared as "public" if it may be used by clients, or
"private" or "protected" if it is part of the implementation and not for use by clients.
• etc....
Public Interface.
• The most common public/private scheme is...
• All ivars are declared private
• etc.....
Java Class.
• The convention is that java classes have upper case names like "Student" and the code
is in a file named "Student.java".
• By default, java classes have the special class "Object" as a superclass. We'll look at
what that means later when we study superclasses.
• etc.....
Instance Variables.
• Instance variables (ivars) are declared like ordinary variables -- a type followed by a
name.
protected int units;
• An ivar defines a variable that each object of this class will have -- allocates a slot
inside each object. In this case, every Student object has an int ivar called "units"
within it.
• etc.....
public/private/protected.
• An ivar or other element declared private is not accessible to client code. The element is
only accessible to the implementation inside the class.
• Suppose on the client side we have a pointer "s" to a Student object. The statement
"s.units = 13;" in client code will not compile if "units" is private or protected.
•etc......
Constructor (ctor).
• A constructor has the same name as the class.
• It runs when new objects of the class are created to set up their ivars.
public Student(int initUnits) {
units = initUnits;
}
• etc.......
Default Ctor
• A constructor with no arguments is known as the "default ctor".
public Student() {
units = 15;
}
• If a class has a default ctor, and a client creates an instance of that class, but without
specifying a ctor, the default ctor is automatically invoked.
• etc....
Method
• A method corresponds to a message that the object responds to
public int getStress() {
return(units * 10);
}
• When a message is sent to an object, the corresponding method runs against that
receiver.
• etc....
Receiver Relative Style (Method, Ctor)
• Method code runs "on" or "against" the receiving object
• Ivar read/write operations in the method code use the ivars of the receiver
• etc...
"this" -- receiver
• "this" in a method
- "this" is a pointer to the receiver
- Don't write "this.units", write: "units"
- Don't write "this.setUnits(5)", write "setUnits(5);"
• Some programmers, like sprinkling "this" around to remind themselves of the OOP
structure involved, but I find it distracting. The nice thing about OOP is the
effortlessness of the receiver-relative style.
• etc....
ivar vs. local var
• Usually, you simply refer to each ivar by its name -- e.g. "units". Sometimes you have a
local var or parameter with the same name as the ivar. In that case, "units" is the
local var, and the expression "this.units" refers to the ivar (see the Student.setUnits()
method). Having a local var with the same name as an ivar is a stylistically
questionable, but it can be handy sometimes. Some people prefer to give ivars a
distinctive name, such as always starting with an "my" -- e.g. myUnits.
FOR ANY FURTHER PLZ REFER THE FOLLOWING INK
http://www.pdftutorials.com/tutorials/java-basic-refresher-pdf+535.html
No comments:
Post a Comment