aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JLS tips Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JLS tips" Watch "JLS tips" New topic
Author

JLS tips

Kishan Kumar
Ranch Hand

Joined: Sep 26, 2000
Posts: 130
Friends,
These points were grabbed from Java Language Specification.
Again useful for last minute preperation.
VARIABLES, NUMBER TYPES
------------------------
1. Primitive variables can hold values only of that type,
2. A variable of class type can hold a null reference or a object
whose type is that class type or any subclass of that type.
3. A variable of an interface type can hold a null reference or a reference to an
instance of any class that implements the interface

4. A variable of an array type can hold a null reference or a reference to an array.
5. A variable of class type Object can hold a null reference or a reference to any object,
whether class instance or array.
6. All integral operations does not produce a exception except for the
divide by zero Arithmetic exception when used with / or %.
7. Negative zero and positive zero are compared equal. ie. -0 == 0 is
true.
8. converting float to integer truncates the decimal part.
9. class variables are eplicitly specified static variables.
interface variables are implicitly static.
10. Arrays have class names which are not so useful.
nre int[10].getclass().getname() gives "[I".
=======================================================================
EXECUTION AND CLASSES
---------------------

11. When a class is initialized its superclass should be initialized however the interface
which it is implementing need not.
12. A class is initialized when
a. call to its constructor.
b. call to its method which declared here, methods inherited from superclass when
invoked does not initialize the class.
c. Referencing any nonfinal value of that class.
13. Static and class variable initializers are executed in textual order.
14. During class initialization instance variables are first initialized and then constructor
is called.
15. finalize is run when object is going to be GC'ed, in finalize if you bring the object to
life then the finalize method will not be called when this object is GC'ed because
finalize is called only once for a object. However you invoke the finalize method
explicitly and this does not change the state of the object.
16. Like constructors finalize will not call superclass finalizers implicitly, if needed
eplicit calls are to be made which is a good design.
17. When a uncaught eception occurs then this is ignored and the finalize terminates here
ie. rest of the lines are not executed after the exception occurs, just that the exception
is not thrown but ignored.
18. JVM exits only when
a. there is only daemon threads running.
b. the exit method of Runtime or System class is invoked.
note that there is no special preference to main method, it is just another method which
is not daemon, when non daemon threads including or excluding main or still alive the JVM\
does not eit.
19. Static initializers are executed only once whereas Instance initializers are executed for
each object.
20. Class name cannot be a name of a imported class.
import java.lang.String;
class String { // NOT LEGAL
import java.lang.*
class String { // LEGAL
21. A compile time occurs when two abstract methods have same signature but different return
types.
interface a { void method();}
abstract class b implements a {
int method(); } // NOT LEGAL since any subclass that implements these methods cannot
satisfy both the methods defined in the interface and abstract class.
22. A class is not abstract if it implements all superinterfaces either directly or inherited
from superclass.
eg. class a { void method() {} }
interface ab { void method(); }
class abc extends a implements ab {} // PERFECTLY LEGAL since superclass has
implemented the interface method signature.
23. No forward referencing of variables are allowed.
class a {
int j = i; // NOT LEGAL
int i = 1;
24. However a non static member variable can forward reference a static variable since static
variables are initialized much before the member variables are initalized.
class a {
int i = j;
static j = 10; // LEGAL.

25. Static initializers should not use the "this" and "super" variable.
26. Instance variable initializers can use super and this.
27. A class can inherit fields having same name but while using it variables should be
referenced unambiguously by prefixing with the class or interface name.
28. A methods signature is only determined by method name with number and type of arguments
not with access specifiers or return type or the throws clause.
29. Abstract methods should not have
a. final, private because u cant override them.
b. static , for the same reason as above. Note static methods are hidden not overriden.
c. native I assume because native is implicitly abstract u need not specify it explicitly.
d. synchronized - i do not know.
30. Abstract method decleration
signature + return type + throws clause.
31. A non abstract method can be overridden by a abstract method, but note that a subclass
of this class should not make attempt to call super() since the superclass method is abstrat
it results in compile time error.
32. A abstract method can override another abstract method.

33. A overriding method(Instance methods) or a hiding method(static methods) should not
throw exception that are not subclasses of eceptions defined in super class however
unchecked exceptions can be thrown even if they are not thrown in superclass. This applies
even for interfaces.
34. It is possible for a method declared to return a value, not containing a return statement
at all, but the method should not normally in that case.
35. A class can only hide member varibles, it cannot override.
Member variables can be inherited , but be careful with access specifiers.
36. A method should NOT be overridden to be more private, either same access specifier may
be used or a access specifier which is more public can be used.
class a {
protected void method(){}
}
class b extends a {
private void method(){} // NOT LEGAL. either protected or public is allowed here.
}
37. A return statement should not occur in the staic initializer.
38. A return statement should not have a expression in a constructor since constructor do not
return anything, however a return; is accepted.
=================================================================================================
ARRAYS
------
39. If a array is of reference type A then it can be assigned a array of reference type B if
and only if B is assignable to A.
Object a[] = new Exception[]; //LEGAL because Object = Exception is allowed.
40. If an long variable is used as a index value in an array compile time error occurs because
unary numeric promotion occurs and all are converted to int.
so byte,short,char,int can be used as array index variables. float and double //NO.
41. Cloneable is implemented by arrays.
A clone returns a different array object meaning
int[] a = {1,2};
int[] b = (int[])a.clone();
System.out.println(b == a); // Prints false.
42. However in a multidimensional array the only one newarray is created and the subarays are
shared meaning,
int[][] a = {{1,2},{3,4}}
int[][] b = (int[][])a.clone();
System.out.println(b == a); // Prints false
System.out.println(b[0] == a[0]); //Prints true, since subarrays are not created but shared.
=================================================================================================
ExCEPTIONS
----------
43. At compile time programs are checked for handlers of checked exceptions, if a method is
bound to throw a checked exception or include the statements of the method which could
cause a checked exception to be thrown in a try/catch block.
44. A checked exception should not occur at static and instance initializers of the class.
45. eg: of checked exceptions:
a. IOException and its subclasses.
b. MalFormedURLException and its subclasses
c. ClassNotFoundException
d. CloneNotSupported
e. IllegalAccessException
f. InstantiationException.
46. One should not try to catch a Error and its subclasses of Exceptions, if you do so it
is not a error, It is not a good design.
47. Exception Hierarchy:
Throwable
|
--------------------------------------------------
| |
Exception Error
|
RuntimeException
48. Both JVM and java programs are capable of throwing exceptions.
49. Never catch a superclass exception first and then a subclass of that, it results in
a compile time error that the code is unreachable as the superclass exception catches
all its exceptions and also its subclasses.
=================================================================================================
THREADS AND LOCKS
-----------------
50. Non volatile double and long variables are treated as two 32 bits for its store,load,write
and read operations. The reading of two 32 bits happenn at any time and there may be a
time difference between these two operations. so there is a consequent change by another
thread between the two read operation of 32 bits the final result may be a combination
of two.
51. Just after a Lock occurs the threads working area is flushed and copies of variables from
the main memory is copied to the thread working memory.
52. Just Before an unlocl operation occurs the threads variables are stored in to the main
memory.
53. wait method unlocks all the locks that the thread has got with the object.
54. A subsequent notify will bring the notified threads to scheduling mechanism and the
thread will have the same state as it was before when the wait was called.
=================================================================================================
EXPRESSIONS
-----------
55. In Array array reference is first evaluated, then the index is evaluated and only then
the array reference is checked to be null or not. if it is null a null pointer exception
is thrown.
56. If you are trying to access a static method of the class then the reference variable
of the type of that class can also point to a null reference. No nullpointer exception
is thrown.
eg. stclass s = new stclass();
s = null;
s.amethod(); //VALID ONLY IF amethod() IS A STATIC METHOD OF CLASS stclass.
57. If an Instance method is being accessed then the reference must contain a valid object.
If it contains null then the nullpointer exception is thrown.
58.
=================================================================================================
INTERFACES
----------
58. It hides Constants defined in superinterfaces.
59. A class can be assigned to interface if it or its superclass implements all the methods by
specifying the implements keyword explicitly or else a compile time error results.
60. Only public and abstract is allowed.
final,static,private // NOT ALLOWED becuae they dont allow overriding.
native,synchronized // NOT ALLOWED.
abstract //Dosent prodce compile or runtime error but this is obselete since interface
is implicitly abstract.
61. All Interface members are implicitly public
62. Every field decleration in a interface is implicitly public static and final and only these
modifiers are allowed to be explicitly specified.
Transient,synchronized,volatile // Not allowed.
63. this and new cannot be used with interfaces.
64. Interface can extend any number of interfaces unlike classes which can extend only 1 class.
65. In method decleration
static not allowed // Since abstract methods cannot be static
native/synchronized// not allowed, no specific reason
but these methods while implementation can be static,native,synchronized,abstract.
=================================================================DECLERATION
-----------
66. int i
for(int i; //NOT LEGAL i declared twice.
67. A break statement should be enclosed in a switch,for,do,while statements.
68. The target of a break statement can be anything, so any statement can contain a label.
69. A label must be a legal identifier but this name does not hide any variables declared
before. Duplicate labels not allowed.
70. A continue statement should occur only in a while,do,for statements refferred to as
iteration statements.
71. The target for a continue statement should be a while , for , do statements only.
72. A return statement should not occur in a static initializer.
73. Static Initializers and Instance Initializers should not throw any checked exceptions.
=================================================================
Copy it to the notepad and view in the full page for best view.
Hope this helps
V.Kishan Kumar


Regards,<BR>V. Kishan Kumar
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
Kishan,
People like you make life so easy yaar....Thanks a ton for these great notes...........we shld all learn from from you....
Harpal
Manish Singhal
Ranch Hand

Joined: Sep 21, 2000
Posts: 104

Excellent work Kishan
keep it up.

Manish
Rob Whelan
Ranch Hand

Joined: Oct 18, 2000
Posts: 33
Great compilation! Thanks a lot!
Just something I noticed in the "Interfaces" section that might be misleading:
"this and new cannot be used with interfaces."
True... except for the special case of creating anonymous classes. Here's code that is valid, but uses "new Interface()":

Again, this only works in the context of anonymous inner classes, but I thought people should know...
-Rob
shanks iyer
Ranch Hand

Joined: Oct 16, 2000
Posts: 47
Kishan Thanks a lot buddy people like you make life easy.
Cheers
Shankar.
[This message has been edited by shanks iyer (edited November 03, 2000).]
Rob Whelan
Ranch Hand

Joined: Oct 18, 2000
Posts: 33
I took some time to go through all of these tips, and cleaned up some of the more problematic ones. It's a nice compilation of tips, but some of them were worded in a very misleading way.
Hope this helps...
-Rob
VARIABLES, NUMBER TYPES
------------------------
6. All integral operations does not produce a exception except for the
divide by zero Arithmetic exception when used with / or %.
Important addition: floats and doubles don't even do that. They return constants for positive infinity, negative infinity, and NaN (not a number) results.

9. class variables are explicitly specified static variables.
interface variables are implicitly static.
To reword: a class won't be static unless you put the keyword in there. An Interface, on the other hand, is automatically static (whether you specify it or not).
=======================================================================
EXECUTION AND CLASSES
---------------------
12. A class is initialized when
a. call to its constructor.
b. call to its method which declared here, methods inherited from superclass when
invoked does not initialize the class.
c. Referencing any nonfinal value of that class.
Note that this is talking about class initialization, NOT instance initialization. This is defining when a class' static initializer, etc. will be run, NOT how objects get created.

13. Static and class variable initializers are executed in textual order.
"Class" means the same thing as static (see the comment below). This should read "Static and instance variable initializers are executed in textual order." I should point out, though, that the static ones will be run first (see the rule above to know when that will happen), so instance initializers at the very beginning can reference static variables that are initialized later on in the class text:
public int my_int = 4 + CONSTANT_INT;
// ..later
static int CONSTANT_INT = 3;

14. During class initialization instance variables are first initialized and then constructor
is called.
This is talking about instance initialization, not class initialization. "Class" refers to the class itself, which is initialized beforehand (i.e., static initializers are run, etc.). Use the word "instance" to refer to an instance of that class.

16. Like constructors finalize will not call superclass finalizers implicitly, if needed
explicit calls are to be made which is a good design.
No -- I think the first word here is supposed to be "Unlike". It is correct that finalize() does not implicitly call super.finalize(), but superclass constructors ARE invoked implicitly.

17. When a uncaught eception occurs then this is ignored and the finalize terminates here
ie. rest of the lines are not executed after the exception occurs, just that the exception
is not thrown but ignored.
I'm not sure what this one is talking about... Here's my guess: if an uncaught exception is thrown from within a "finally" block, the rest of the block is not executed, and that exception keeps going up until it's caught. If control was in the finally block because an uncaught exception was thrown from within the try, that older exception is lost totally. Java never has more than one exception at a time per thread.

21. A compile time occurs when two abstract methods have same signature but different return
types.
interface a { void method();}
abstract class b implements a {
int method(); } // NOT LEGAL since any subclass that implements these methods cannot
satisfy both the methods defined in the interface and abstract class.
This requirement has nothing to do with abstract methods or interfaces -- ANY 2 methods can't do that. Overloading/overriding with methods that differ only by return type will cause a compiler error in any situation.

27. A class can inherit fields having same name but while using it variables should be
referenced unambiguously by prefixing with the class or interface name.
I don't know what "having the same name" refers to -- having the same name as what? -but I can say that the last detail applies only to static variables. You can't access an instance variable with the class/interface name.

30. Abstract method decleration
signature + return type + throws clause.
Not sure what this one is saying -- it looks wrong, at any rate.

34. It is possible for a method declared to return a value, not containing a return statement at all, but the method should not normally in that case.
In other words, if you method always throws an exception (and the compiler can see that), you don't need any return statements even though you might have a return type in the signature:
public int throwIt() throws Exception
{
throw new Exception();
}
=================================================================================================
THREADS AND LOCKS
-----------------
54. A subsequent notify will bring the notified threads to scheduling mechanism and the
thread will have the same state as it was before when the wait was called.
Not exactly -- the thread still has to compete to get the lock(s) back before it can continue executing.
=================================================================================================
INTERFACES
----------
58. It hides Constants defined in superinterfaces.
Only methods can be overridden, so this restriction applies to classes and interfaces, and any kind of variable, instance, or static.
=================================================================DECLARATION
-----------
68. The target of a break statement can be anything, so any statement can contain a label.
This needs clarification which I can't provide off the top of my head -- where can the label be? I know it's not true that the target can be anywhere. Also, I know that declarations (i.e., int i = 10 cannot be labelled.
Harpal Singh
Ranch Hand

Joined: Oct 10, 2000
Posts: 229
thanks rob
great work
Harpal
Subha Guhan
Greenhorn

Joined: Nov 06, 2000
Posts: 23
Good work Kishan!
I was wanting to do something like this from KHALID, e'one seems
to be having RHE but not Khalid, but only in my copious free
time or some good samaritan can beat me to it.
Subha
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hey Kishore, I think JLS is the best information available for core Java. You have done a great job briefing the imp points.
Good luck if you are taking exam,
Santhosh.
SHALINI PATEL
Ranch Hand

Joined: Oct 31, 2000
Posts: 41
hi kishan,
Thanks a bunch for that compilation. I am reading the moughal book and I dont know if that is enough for the certification. Everybody is talking about JLS. Can I do away with your notes?

SHALINI
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLS tips