Subscribe to Java/SCJP        RSS Feed
-----

SCJP Notes - Part 3

Icon Leave Comment
1. A non-nested class/interface is a top level class/interface

2. Within a class a code block is known as instance code block and is invoked whenever an instance is created.

3. Create strings as literals instead of creating String objects using 'new' key word whenever possible

4. Use String.intern() method if you want to add number of equal objects whenever you create String objects using 'new' key word.

5. + operator gives best performance for String concatenation if Strings resolve at compile time

6. StringBuffer with proper initial size gives best performance for String concatenation if Strings resolve at run time.

7. Inner classes help in implementing the behavior, not suited for the type of class but is required by the application like a tree doing a binary search.

8. Nested classes can be categorized as
a. Inner classes
. Member Inner classes //class members
. Local Inner classes //method body or code block
. Anonymous Inner classes //Any expression
b. Static Nested classes or top level nested classes

9. Class files are generated for the inner classes too. Inner classes may be private/protected/default.

10. The instances of inner classes can be created by only using the instance of the outer class.

11. OuterOne.InnerOne inner = new OuterOne().new InnerOne();

12. Within inner class this refers to the inner class’s instance and outer.this refers to the outer class’s instance.

13. A method local variable can’t be accessed in a method-local class whereas a method local final variable can very well be accessed. The reason being that local variables die after method finishes its execution, but final variables don’t die.

14.Anonymous inner classes:
Bicycle mountainBike = new Bicycle() {
	   public void paddle() {
		   System.out.println(“paddling really hard!”);
	   }
}; 

This class is overriding the paddle() method of base class. These classes have only one instance

15. An assignment operator or a function call returning boolean value is a valid argument for if statement.

16. For two case statements with switch having same value, compiler flashes an error.

17. Posted Image

18. Errors refer to the conditions like JVM running out of memory. The programmer need not handle them.

19. Checked exceptions are related to correctly coded programs. They are not programming problems like null referencing but may be FileNotFound type.

20. The unchecked exceptions are the subclasses of RuntimeException class.

21. It is recommended to create the exception object only when creating it because the stack trace will print all function calls after the object was created.

22. If assert statement fails then java.lang.Assertionerror is thrown at runtime.

23. (assert(condition) : information) can be used to print additional information whenever an assertion error is thrown.

24. –ea/ -enableassertions enables assertions –da/ -disableassertions disables assertions and –source make the program assertions aware.

25. If a package specified with assertion doesn’t exist then the execution is not effected. The system classes are the classes in the java platform libraries and can be enabled and disabled by using –esa/-dsa option.

26. Garbage collector is a low-priority thread.
Reference counting
In reference counting, the garbage collectors distinguish the reachable objects by keeping a count for each object on the heap. The count keeps track of the number of references to that particular object. The object with reference count 0 is no longer reachable and hence considered as garbage. This technique has the advantage that it can reclaim objects promptly as the object is marked as garbage the moment reference-count is 0. This makes it particularly useful for real-time environments where the program can't be interrupted for garbage collection cycles for very long time. A disadvantage of reference counting is that it does not detect cyclic references (two or more objects that refer to each other) These objects will never have a reference count of zero even though they may be unreachable from the root references. We will see example cyclic references later in the chapter. Another disadvantage is the overhead caused while storing the reference count for each object and keeping it up-to-date.
Tracing
Some garbage collectors actually trace out the references starting from the root references to determine the reachability. Objects that are encountered during the trace are marked as reachable. After the trace is complete, unmarked objects are known to be unreachable and can be garbage collected. The basic tracing algorithm is called mark and sweep. As the name suggests, it follows a two-phase approach. In the mark phase, the garbage collector traverses the tree of references and marks each object it encounters. In the sweep phase, memory allocated to the unmarked objects is freed and is made available to the executing program

27. Cyclic references and references returned by methods are complex scenarios of object not getting garbage collected.

28. String literals are stored in a pool inside the heap.

29. If the compiler runs the finalize method and the current object is made reachable then it may be disastrous as the compiler can garbage collect the object at any time without executing the finalize() method again.

30. A array is serializable and cloneable too.

31. The bitwise complement operator (~) always works on char, byte, short, int and long only.

32. When using the modulo operator with negative numbers, the sign of the first operand matters, the sign of second operand is irrelevant. And the modulo operator can be applied to floating point operands too.

33. The toString() method from object class if not overrided prints the name of class followed by @ and then some unique value.

34. The division by 0 for integral operands results in ArithmeticException but for floating numbers, it results a NaN value.

35. Java wraps the shifting operand in shift operators so as to minimize the execution. For int, it calculates %32 of the bit shifting operand so as to find the number of effective bit shifts to be done.

36. The comparison operator can be used with any numeric values.

37. A StringBuffer object can never be equal to a String object.

38. instanceof operator checks for the type and not the object and this is why it can be applied to interfaces too.
int [] arr = new int[5];
boolean isAnIntArray = arr instanceof int[]; // true

39. When null is the first operand for instanceof operator, it always returns false.

40. The bitwise operators & and | can also be applied to boolean operands but then they are not short circuit as && and ||.

41. The operands are evaluated before applying any operator in an expression and then the operators are applied.

42. float primitive variable is 32 bit but it can store 64 bit long values due to the use of exponent.

43. The automatic narrowing conversion takes place when assigning a literal to a variable because the compiler can check for the compatibility of literal and the variable. But this conversion doesn’t take place for floating numbers and takes place only for numeric values.

44. The automatic conversion also takes place during a method call.

45. The unary operators +,- and ~ return an int as the result. ++ and -- don’t any promotion.

46. byte a = 8; a += 3; is equivalent to a = (byte) a +3

47. The array of primitives is not type compatible, They can only be assigned to references of Object, Serializable and Cloneable interfaces.

48. If the contents of two arrays are type compatible then they can be assigned to depending upon the generality.

49. Explicit casting is allowed if any of the classes is the superclass of the other.

50. Any non-final class object can be assigned to an interface. This is done in the anticipation that some of the subclass of non-final class may implement the interface.

51. Array references of primitive types can’t be but of object can be explicitly type casted.

52. The term before IS-A represents a class and the term after IS-A represents its superclass. Similarly a term just before HAS-A represents a class and all the terms after HAS-A represent its member variables.

53. Overloaded methods may include the inherited methods of a class.

54. The superclass version of an overridden method can be called by using the super keyword.

55. The constructors return nothing (not even void), so if a method is declared with same name and arguments as a constructor but with different return type, it is considered as being overloaded method. This is not true for other member methods.

56. The compiler also checks for the recursive constructor calls between the various constructors.

57. To create a thread, you always need a instance of Thread class. It has a constructor which accepts argument of type runnable and invokes the run method on that object and that is why it is possible to implement multithreaded application using the runnable interface.

58. Calling the run() method will cause the application to be single threaded as then run() will be executed as if an ordinary function.

59. It is the thread scheduler which decides when to start which thread based on some algorithm.

60. The sleep() method in the run code of a thread will cause the current thread to sleep for some period of time which means that the thread is itself going to not-running state and is coming to ready-to-run state after some time, automatically.

0 Comments On This Entry

 

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Recent Entries

Recent Comments

Search My Blog

15 user(s) viewing

15 Guests
0 member(s)
0 anonymous member(s)