Thursday, 1 October 2009

SCJP Sun Certified Programmer By Kathy Sierra and Bert Bates - Part1

Couple of Days back i started practicing SCJP Sun Certified Programmer for Java 5 Study Guide By Kathy Sierra and Bert Bates.

Great book written by Kathy Sierra and Bert Bates.

I wanted to share few important quotations found from the first chapter.

1) Legal Identifiers

Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number!

After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers.

In practice, there is no limit to the number of characters an identifier can contain.

You can't use a Java keyword as an identifier.

2) Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points.Without that modifier, floating points used in the methods might behave in a platform-dependent way.

3) All variables defined in an interface must be public, static, and final—in other words, interfaces can declare only constants, not instance variables.

Interface methods must not be static.

4) Local variable declared with an access modifier will not compile. In fact, there is only one modifier that can ever be applied to local variables—final.

5) All the combinations of access and visibility of class members is as follows








VisibilityPublicProtectedDefaultPrivate
From the same classYesYesYesYes
From any class in the same packageYesYesYesNo
From a subclass in the same packageYesYesYesNo
From a subclass outside the same packageYesYes, through inheritanceNoNo
From any non-subclass class outside the packageYesNoNoNo


6) Unlike instance variables local variables don't get default values.

It is possible to declare a local variable with the same name as an instance variable. It's known as shadowing.


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();
}
}



7) A final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object.

8) 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.

9) 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.

10) In Enums we can declare constant specific class body, and can use it when we need a particular constant to override a method defined in the enum.


enum MarksSize {
BELOWTEN(8),
ABOVETEN(11),
ABOVEHUNDRED(116) { // start a code block that defines
// the "body" for this constant
public String getLidCode() { // override the method
return "A100";
}
};

int size=0;

MarksSize(int size) {
this.size=size;
}

public String getMarksCode() { // this method is overridden
return "B100"; // the default value we want to return which is below 100
}
}

public class Demo {
public static void main(String[] args) {
System.out.println(MarksSize.ABOVEHUNDRED.getMarksCode());
}

}



11) Local Variables

a) Local (method, automatic, or stack) variable declarations cannot have access modifiers.
b) final is the only modifier available to local variables.
c) Local variables don't get default values, so they must be initialized before use.

12) final reference variables must be initialized before the constructor completes.

13) Enums

a) An enum declared outside a class must NOT be marked static, final, abstract, protected, or private.

b) An enum can be declared outside or inside a class, but NOT in a method.

c) enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized.

d) The semicolon at the end of an enum declaration is optional.

No comments: