SCJP Java Programming Exam Cram Notes : Language Fundamentals

Data Type Size (bits) Initial Value Min Value Max Value
boolean 1 false false true
byte 8 0 -128(-27) 127 (27-1)
short 16 0 -215 215-1
char 16 '\u0000' '\u0000'(0) '\uFFFF' (216-1)
int 32 0 -231 -231-1
long 64 0L -263 -263-1
float 32 0.0F 1.4E-45 3.4028235E38
double 64 0.0 4.9E-324 1.7976931348623157E308

13. All numeric data types are signed. char is the only unsigned integral type.

14. Object reference variables are initialized to null.

15. Octal literals begin with zero. Hex literals begin with 0X or 0x.

16. Char literals are single quoted characters or unicode values (begin with \u).

17. A number is by default an int literal, a decimal number is by default a double literal.

18. 1E-5d is a valid double literal, E2d is not (since it starts with a letter, compiler thinks that it's an identifier)

19. Two types of variables.

1. Member variables

  • Accessible anywhere in the class.
  • Automatically initialized before invoking any constructor.
  • Static variables are initialized at class load time.
  • Can have the same name as the class.

2. Automatic variables (method local)

  • Must be initialized explicitly. (Or, compiler will catch it.) Object references can be initialized to null to make the compiler happy. The following code won't compile. Specify else part or initialize the local variable explicitly.
public String testMethod ( int a) 
{
String tmp; 

if ( a > 0 ) tmp = "Positive"; 

return tmp;

}							
  • Can have the same name as a member variable, resolution is based on scope.

20. Arrays are Java objects. If you create an array of 5 Strings, there will be 6 objects created.

21. Arrays should be

1. Declared. (int[] a; String b[]; Object []c; Size should not be specified now) 

2. Allocated (constructed). ( a = new int[10]; c = new String[arraysize] )

3. Initialized. for (int i = 0; i < a.length; a[i++] = 0) 							

22. The above three can be done in one step.

int a[] = { 1, 2, 3 }; (or )

int a[] = new int[] { 1, 2, 3 }; But never specify the size with the new statement.							

23. Java arrays are static arrays. Size has to be specified at compile time. Array.length returns array's size. (Use Vectors for dynamic purposes).

24. Array size is never specified with the reference variable, it is always maintained with the array object. It is maintained in array.length, which is a final instance variable.

25. Anonymous arrays can be created and used like this: new int[] {1,2,3} or new int[10]

26. Arrays with zero elements can be created. args array to the main method will be a zero element array if no command parameters are specified. In this case args.length is 0.

27. Comma after the last initializer in array declaration is ignored.

int[] i = new int[2] { 5, 10}; // Wrong
int i[5] = { 1, 2, 3, 4, 5}; // Wrong

int[] i[] = {{}, new int[] {} }; // Correct

int i[][] = { {1,2}, new int[2] }; // Correct

int i[] = { 1, 2, 3, 4, } ; // Correct							

28. Array indexes start with 0. Index is an int data type.

29. Square brackets can come after datatype or before/after variable name. White spaces are fine. Compiler just ignores them.

30. Arrays declared even as member variables also need to be allocated memory explicitly.

static int a[];
static int b[] = {1,2,3};

public static void main(String s[]) 

{

System.out.println(a[0]); // Throws a null pointer exception

System.out.println(b[0]); // This code runs fine

System.out.println(a); // Prints 'null'

System.out.println(b); // Prints a string which is returned by toString

}							

31. Once declared and allocated (even for local arrays inside methods), array elements are automatically initialized to the default values.

32. If only declared (not constructed), member array variables default to null, but local array variables will not default to null.

33. Java doesn't support multidimensional arrays formally, but it supports arrays of arrays. From the specification - "The number of bracket pairs indicates the depth of array nesting." So this can perform as a multidimensional array. (no limit to levels of array nesting)

34. In order to be run by JVM, a class should have a main method with the following signature.

public static void main(String args[])
static public void main(String[] s)							

35. args array's name is not important. args[0] is the first argument. args. length gives no. of arguments.

36. main method can be overloaded.

37. main method can be final.

38. A class with a different main signature or w/o main method will compile. But throws a runtime error.

39. args array's name is not important. args[0] is the first argument. args. length gives no. of arguments.

40. main method can be overloaded.

41. main method can be final.

42. A class with a different main signature or w/o main method will compile. But throws a runtime error.

43. A class without a main method can be run by JVM, if its ancestor class has a main method. (main is just a method and is inherited)

44. Primitives are passed by value.

45. Objects (references) are passed by reference. The object reference itself is passed by value. So, it can't be changed. But, the object can be changed via the reference.

46. Garbage collection is a mechanism for reclaiming memory from objects that are no longer in use, and making the memory available for new objects.

47. An object being no longer in use means that it can't be referenced by any 'active' part of the program.

48. Garbage collection runs in a low priority thread. It may kick in when memory is too low. No guarantee.

49. It's not possible to force garbage collection. Invoking System.gc may start garbage collection process.

50. The automatic garbage collection scheme guarantees that a reference to an object is always valid while the object is in use, i.e. the object will not be deleted leaving the reference "dangling".

51. There are no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. gc might not even be run if the program execution does not warrant it. Thus any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the OS or by other means.

52. There are also no guarantees on the order in which the objects will be garbage collected or on the order in which the finalizers are called. Therefore, the program should not make any decisions based on these assumptions.

53. An object is only eligible for garbage collection, if the only references to the object are from other objects that are also eligible for garbage collection. That is, an object can become eligible for garbage collection even if there are references pointing to the object, as long as the objects with the references are also eligible for garbage collection.

54. Circular references do not prevent objects from being garbage collected.

55. We can set the reference variables to null, hinting the gc to garbage collect the objects referred by the variables. Even if we do that, the object may not be gc-ed if it's attached to a listener. (Typical in case of AWT components) Remember to remove the listener first.

56. All objects have a finalize method. It is inherited from the Object class.

57. finalize method is used to release system resources other than memory. (such as file handles and network connections) The order in which finalize methods are called may not reflect the order in which objects are created. Don't rely on it. This is the signature of the finalize method.

protected void finalize() throws Throwable { }
In the descendents this method can be protected or public. Descendents can restrict the exception list that can be thrown by this method. 							

58. finalize is called only once for an object. If any exception is thrown in finalize, the object is still eligible for garbage collection (at the discretion of gc)

59. gc keeps track of unreachable objects and garbage-collects them, but an unreachable object can become reachable again by letting know other objects of its existence from its finalize method (when called by gc). This 'resurrection' can be done only once, since finalize is called only one for an object.

60. finalize can be called explicitly, but it does not garbage collect the object.

61. finalize can be overloaded, but only the method with original finalize signature will be called by gc.

62. finalize is not implicitly chained. A finalize method in sub-class should call finalize in super class explicitly as its last action for proper functioning. But compiler doesn't enforce this check.

63. System.runFinalization can be used to run the finalizers (which have not been executed before) for the objects eligible for garbage collection.

64. The following table specifies the color coding of javadoc standard. (May be not applicable to 1.2)

Member Color

Instance method Red

Static method Green

Final variable Blue

Constructor Yellow

Previous    Contents    Next