My Learnings
Friday, October 28, 2011
Monday, July 4, 2011
Java
1. Variables
As you learned in the previous lesson, an object stores its state in fields.int cadence = 0;
int speed = 0;
int gear = 1;
The What Is an Object? discussion introduced you to fields, but you probably have still a few questions, such as: What are the rules and conventions for naming a field? Besides int
, what other data types are there? Do fields have to be initialized when they are declared? Are fields assigned a default value if they are not explicitly initialized? We'll explore the answers to such questions in this lesson, but before we do, there are a few technical distinctions you must first become aware of. In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.
The Java programming language defines the following kinds of variables:
- Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the
static
keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); thecurrentSpeed
of one bicycle is independent from thecurrentSpeed
of another. - Class Variables (Static Fields) A class variable is any field declared with the
static
modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked asstatic
since conceptually the same number of gears will apply to all instances. The codestatic int numGears = 6;
would create such a static field. Additionally, the keywordfinal
could be added to indicate that the number of gears will never change. - Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example,
int count = 0;
). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class. - Parameters You've already seen examples of parameters, both in the
Bicycle
class and in themain
method of the "Hello World!" application. Recall that the signature for themain
method ispublic static void main(String[] args)
. Here, theargs
variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
2. Initializing Fields
public class BedAndBreakfast { public static int capacity = 10; //initialize to 10 private boolean full = false; //initialize to false }
for
loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks. Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.
Static Initialization Blocks
A static initialization block is a normal block of code enclosed in braces,{ }
, and preceded by the static
keyword. Here is an example: static { // whatever code is needed for initialization goes here }
There is an alternative to static blocks — you can write a private static method:
class Whatever { public static varType myVar = initializeClassVariable(); private static varType initializeClassVariable() { //initialization code goes here } }
Initializing Instance Members
Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods. Initializer blocks for instance variables look just like static initializer blocks, but without the static
keyword:
{ // whatever code is needed for initialization goes here }
A final method cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance. Here is an example of using a final method for initializing an instance variable:
class Whatever { private varType myVar = initializeInstanceVariable(); protected final varType initializeInstanceVariable() { //initialization code goes here } }
http://download.oracle.com/javase/tutorial/java/javaOO/initial.html
3. final keyword
http://renaud.waldura.com/doc/java/final-keyword.shtml#vars
-------------------------------------------------------------------------------------------------------------------------
Over riding and over loading in Java
Method Signature
Name of the method + parameters(no.of parameters, types of parameters)
ex: For method int java_riding(int a, float c)
The method signature is java_riding(int a, float c)
METHOD OVERLOADING
Method signature must be different.
METHOD OVERRIDING
An instance method in the subclass said to be overriding a method in super class if it has same signature and return type of the method ,that is present in super class .
* Overriding method can return a subtype of the return type of the overridden method.
METHOD HIDING
Same as Method Overriding, but both the overridden and overriding methods must be static or class methods.
EXCEPTION AND ERRORS
java.lang.Throwable .Both error and excpetion are subclasses of Throwable.
EXCEPTION : It is a reasonable condition that can occur while running program which we might wish to catch.
ERROR : It is an abnormal condition that can occur while running program which we should not try to catch. For Example: The ThreadDeath Error
A method is not required to declare in its
throws
clause any subclasses of Error
that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.Friday, June 3, 2011
C .. My Love
Dennis Ritchie website .. where we can find many links about C.
http://cm.bell-labs.com/cm/cs/who/dmr/index.html
C
http://cm.bell-labs.com/cm/cs/who/dmr/ctut.pdf
The below one explains the lvalue
http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf
http://cm.bell-labs.com/cm/cs/who/dmr/mdmpipe.html
Inter Process communication
http://cm.bell-labs.com/cm/cs/who/dmr/ipcpaper.html
Thursday, June 2, 2011
CALL STACK
http://en.wikipedia.org/wiki/Call_stack
http://en.wikipedia.org/wiki/Calling_convention
The one below explains the call sequence or call stack or stack or execution stack mechanism in C
http://cm.bell-labs.com/cm/cs/who/dmr/clcs.html