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

SCJP Notes - Part 4

Icon Leave Comment
1. The wait() method in the run code of a thread will cause the current thread to go into not-running state until some other method calls notify() for this thread or notifyall() is not called.

2. The suspend() and stop() method are deprecated.

3. After finishing its execution, the thread continues to exist but it ceases to exist a thread-of-execution. The isAlive() method can be used to test whether the thread is dead or not.

4. Each thread maintains its stack which used in case of context switching.

5. The setPriority() method (and not any constructor) can be used to set the priority of a thread. By default, the priority is set to 5.

6. The program shouldn’t depend on priorities for its correctness as there is no standard defined by JLS. However the constants MIN_PRIORITY=1, NORM_PRIORITY=5 and MAX_PRIORITY=10 have been defined.

7. One version of sleep takes milliseconds and other more precise one takes milliseconds and nanoseconds.

8. yield() is always called on the current thread that is executing and causes the thread to come out of running state and go to read-to-run state.

9. The use of yield() makes sense when a thread having high priority uses too much of resources that other threads are always thriving for the resources. The high priority thread can then repeatedly call the yield method to allow other threads to be run.

10. The join() method is used when some other thread must be run before continuing processing in the current thread. It causes the current thread to go out of running state and cause the thread on which the join() is invoked to start running and when the called thread finishes, the current thread becomes active. The join() method also accepts time.

11. The sleep() and yield() methods are static void while setPriority() and join() are instance methods declared as void final. They all are defined in the Thread class.

12. The wait(), notify() and notifyAll() are also final void but are declared in the Object class.

13. The static method currentThread() of the Thread class returns the current thread.

14. The synchronized access modifier makes sure that no other thread enters the thread while one thread had called it and has not finished the execution. Thus if a method goes to wait state after calling synchronized method and not completing it, all threads who want to execute the sybchronized method will have to wait.

15. The synchronized block requires an object on which it has to place the lock for the thread going inside the block. For synchronized methods, the locking object is implicitly this.

16. When a thread is trying to get a lock that has been acquired by some other thread then that thread is said to be in blocking state.

17. A static method can also be synchronized and in that case the lock is the java.lang.Class instance of the class which was created when the class was loaded by JVM.

18. The call to wait() and notify methods should be in a synchronized block/function otherwise an exception of type InterruptedException is thrown which is a checked exception. The synchronized block should specify the object on which the wait/notify is invoked in its arguments.

19. Every object has two kind of pools for threads, one in which the threads requiring the lock over the object are stored and the other pool is of the threads on which the wait() method has been called.

20. A thread goes to wait state when it calls the wait() method on an object and has to wait until some other thread calls the notify() method on the same object.

21. When the wait is invoked in a synchronized block/function, the thread has the lock for that object as declared in the synchronized block/function and it releases the lock after the call to wait (in sleep it doesn’t release the lock). Now when notify occurs the lock is not immediately released by the other thread. The lock is released after the thread has come out of the synchronized block/function.

22. The waiting for a thread can be made for some interval by using the overloaded versions of the wait method.

23. The difference between notify() and notifyAll() is that notify will bring only one thread out of the wait pool for that object to read-to-run method, while notifyAll() will do so for all the threads that are waiting for the notify event.

24. java.lang.Math is a final class and has all of its public methods as static. The public final static double variables in the Math class are PI and E. The methods in Math class return a value of same type as that of the type of argument which is being passed. If the argument type is narrower than int type then it is casted to int and the corresponding version of the method is called.

25. The random() method always returns result of type double and the value of the result is between 0.0 and 1.0. It can be 0.0 but it can’t be 1.0

26. Double.NaN is returned when trying to find the square root of a negative number.

27. There are two versions of round method viz. public static int round(float) and public static long round(double). For rounding numbers 0.5 is added to the number and the result is Floored.

28. String type references may be changed but not the String Objects.

29. String str = new String(“Hello”); will create two objects, one is the String object on heap and the other a String literal object on the literal pool (which is considered as being lost as no refrence points to it).

30. String class is final because its objects can’t be modified and Math class is final because all its methods are static.
the end index has to be index of the character wanted +1 for getting the substring using two parameter version of the substring method.

31. The call to toString() returns the original string refrence itself.

32. indexOf(int ch, int fromIndex) starts the search from fromIndex to find the character ch. There is no limit on fromIndex (-ve means 0 and a value greater than length of string means equal to the length)

33. Arrays have the length as member and Strings have length() as a method.

34. String concatenation is implemented through the StringBuffer class and its append method.

35. String conversions are implemented through toString() method.

36. String class has a constructor that accepts String Buffer object as argument.

37. If trim() has no effect on the current String then the same String object’s reference is returned. Similar is the case with toUpperCase(), toLowerCase(), replace(char old, char new) and concat(String str)

38. System.out.println(2+1+""); will print 3 and System.out.println(“”+2+1); will print 21

39. Program to reverse the words (not characters)

int j=0;

		char[] str = {'M','y',' ','n','a','m','e',' ','S','a','n','d'};
		int temp = str.length-1;
		int i = str.length-1;
		while(i>=0) {
			if(i==0) {
				while(str[i]!= ' ') {
					System.out.print(str[i]);
					i++;
				}
				break;
			}
			if(str[i]== ' ') {
				for(j=i+1;j<=temp;j++) {
					System.out.print(str[j]);
				}
				System.out.print(" ");
				temp=i-1;
			}
			i--;
		}


The formula for calculating hashcode is s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

40. The append method of StringBuffer appends to the same StringBuffer object while the concat() method of String class creates a new String

41. The difference between char and short is the interpretation of the first bit.

42. The float to long is defined as narrow conversion because of possible loss of precision.

43. System.setErr(st); System.setOut(st); can be used to set the default error and out variables of the System class and hence redirect the output.

44. Add a main() method to an applet and it will also become a Java application

45. when one is overriding the equals() method the one should also override the hashcode() method.

46. Two StringBuffer object don’t cause the output of equals() method to be true just because there contents are same.

47. The Void class is a placeholder to hold a reference to a class object representing the primitive type Void.

48. The wrapper classes except Character have two constructors, one that takes a primitive and the other that takes the primitive as a String. And also all wrappers except Character have a valueOf() method that also returns the object by accepting primitives in String format.

49. The classes Byte, Short, Integer or Long have an overloaded valueOf() method that also accepts the radix as its second argument.

50. The objects returned by wrapper classes are immutable.

51. The six numeric wrapper classes have the type value() methods that can return a value having type as any of the six numeric types.

0 Comments On This Entry

 

April 2014

S M T W T F S
  12345
6789101112
13141516 17 1819
20212223242526
27282930   

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

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