Saturday, October 1, 2011

Scjp notes

1)Array Declarations
Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements.
Declaring arrays
*Declaring an Array of Primitives
int[ ] key;  // Square brackets before name (recommended)
int key [ ]; // Square brackets after name (legal but less
            // readable)
*Declaring an Array of Object References
Thread[ ] threads;  // Recommended
Thread threads [ ]; // Legal but less readable
-We can also declare multidimensional arrays, which are in fact arrays of arrays.
This can be done in the following manner:
String[ ][ ][ ] occupantName;
String[ ] ManagerName [ ];

2)Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that
variable once it has been initialized with an explicit value
A reference variable marked final can't ever be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.
--A final class cannot be subclassed.
Final variables have the following properties:
  #final variables cannot be reinitialized once assigned a value.
  #final reference variables cannot refer to a different object once the object has been      assigned to the final variable.
  #final reference variables must be initialized before the constructor
     completes.

3)Transient Variables
If you mark an instance variable as transient, you're telling the JVM to skip (ignore) this variable when you attempt to serialize the object containing it.

4)Volatile Variables
The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.
-volatile and transient  can be applied only to instance variables.

5)Static Variables and Methods
The static modifier is used to create variables and methods that will exist independently of any instances created for the class. In other words, static
members exist before you ever make a new instance of a class, and there will be
only one copy of the static member regardless of the number of instances of that
class. In other words, all instances of a given class share the same value for any given static variable.
We can apply static modifier to the following:
# Methods
# Variables
# A class nested within another class, but not within a method
# Initialization blocks

6)Member Access Modifiers
-- Methods and instance (nonlocal) variables are known as "members."
-- Members can use all four access levels: public, protected, default, private.
-- Member access comes in two forms:
  *   Code in one class can access a member of another class.
  *   A subclass can inherit a member of its superclass.
-- If a class cannot be accessed, its members cannot be accessed.
-- Determine class visibility before determining member visibility.
-- public members can be accessed by all other classes, even in other packages.
-- If a superclass member is public, the subclass inherits it—regardless of package.
-- Members accessed without the dot operator (.) must belong to the same class.
-- this. always refers to the currently executing object.
-- this.aMethod() is the same as just invoking aMethod().
-- private members can be accessed only by code in the same class.
-- private members are not visible to subclasses, so private members cannot
    be inherited.
-- Default and protected members differ only when subclasses are involved:
-- Default members can be accessed only by classes in the same package.
-- protected members can be accessed by other classes in the same
    package, plus subclasses regardless of package.
-- protected = package plus kids (kids meaning subclasses).
-- For subclasses outside the package, the protected member can be
      accessed only through inheritance; a subclass outside the package cannot
      access a protected member by using a reference to a superclass instance
      (in other words, inheritance is the only mechanism for a subclass
      outside the package to access a protected member of its superclass).
-- A protected member inherited by a subclass from another package is
      not accessible to any other class in the subclass package, except for the
      subclass' own subclasses.

7)Local Variables
-- Local (method, automatic, or stack) variable declarations cannot have
    access modifiers.
-- final is the only modifier available to local variables.
-- Local variables don't get default values, so they must be initialized before use.
--Just as the local variable starts its life inside the method, it's also destroyed when the
   method has completed. Local variables are always on the stack, not the heap.
--Local variable declarations can't use most of the modifiers that can be applied to instance variables, such as public (or the other access modifiers), transient,  volatile, abstract, or static, but as we saw earlier, local variables can be marked final.
It is possible to declare a local variable with the same name as an instance
variable. It's known as shadowing, as the following code demonstrates:
class TestServer {
   int count = 9;  // Declare an instance variable named count
   public void logIn() {
      int count = 10;  // Declare a local variable named count
      System.out.println("local variable count is " + count);
   }
   public void count() {
      System.out.println("instance variable count is " + count);
   }
   public static void main(String[] args) {
new TestServer().logIn();
      new TestServer().count();
   }
}
The preceding code produces the following output:
local variable count is 10
instance variable count is 9

8)Synchronized Methods    
The synchronized keyword indicates that a method can be accessed by only one
thread at a time.synchronized modifier can be applied only to methods—not variables, not classes, just methods. A typical synchronized declaration looks like this:
public synchronized Record retrieveUserInfo(int id) { }
You should also know that the synchronized modifier can be matched with any of the four access control levels.

9)Native Methods
 Native is a modifier (thus a reserved keyword) and that native can be applied only to methods—not classes, not variables, just methods. Note that a native method's body must be a semicolon (;) indicating that the implementation is omitted.

10)Strictfp Methods   
Even if you don't declare a class as strictfp, you can still declare an individual method
as strictfp. Remember, strictfp forces floating points (and any floating-point operations) to
adhere to the IEEE 754 standard. With strictfp, you can predict how  your floating points
will behave regardless of the underlying platform the JVM is running on.

11)Methods with Variable Argument Lists (var-args)
*Var-arg type: When you declare a var-arg parameter, you must specify the
type of the argument(s) this parameter of your method can receive. (This can
be a primitive type or an object type.)
*Basic syntax: To declare a method using a var-arg parameter, you follow the
type with an ellipsis (...), a space, and then the name of the array that will
hold the parameters received.
*Other parameters: It's legal to have other parameters in a method that uses
a var-arg.
*Var-args limits: The var-arg must be the last parameter in the method's
signature, and you can have only one var-arg in a method.
Legal:
void doStuff(int... x) { }  // expects from 0 to many ints as parameters
void doStuff2(char c, int... x)  { }  // expects first a char, then 0 to many ints
void doStuff3(Animal... animal) { }   // 0 to many Animals
Illegal:
void doStuff4(int x...) { }             // bad syntax
void doStuff5(int... x, char... y) { }  // too many var-args
void doStuff6(String... s, byte b) { }  // var-arg must be last

12)Constructor Declarations
Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you.
class Foo {
  protected Foo() { }          // this is Foo's constructor
  protected void Foo() { }     // this is a badly named,
                               // but legal, method
}
 A key difference is that a constructor can't ever have a return type! Constructor declarations can however have all of the normal access modifiers, and they can take arguments (including var-args), just like methods. The other BIG RULE, to understand about constructors is that they must have the same name as the class in which they are declared. Constructors can't be marked static (they are after all associated with object instantiation), they can't be marked final or abstract (because they can't be overridden). Here are some legal and illegal constructor declarations:
class Foo2 {

 // legal constructors
  Foo2() { }
  private Foo2(byte b) { }
  Foo2(int x) { }
  Foo2(int x, int... y) { }
  // illegal constructors
  void Foo2() { }            // it's a method, not a constructor
  Foo() { }                  // not a method or a constructor
  Foo2(short s);             // looks like an abstract method
  static Foo2(float f) { }   // can't be static
  final Foo2(long x) { }     // can't be final
  abstract Foo2(char c) { }  // can't be abstract
  Foo2(int... x, int t) { }  // bad var-arg syntax
 }

Type             Bits                 Bytes         
byte                  8                    1
short               16                    2
int                   32                    4
long                64                    8
float                32                    4
double            64                    8

13)Instance Variables
Instance variables are defined inside the class, but outside of any method, and are only initialized when the class is instantiated. Instance variables are the fields that belong to each unique object.
For the exam, you need to know that instance variables
# Can use any of the four access levels (which means they can be marked with
    any of the three access modifiers)
# Can be marked final
# Can be marked transient
# Cannot be marked abstract
# Cannot be marked synchronized
# Cannot be marked strictfp
# Cannot be marked native
# Cannot be marked static, because then they'd become class variables.






No comments:

Post a Comment

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...