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

SCJP Notes - Part 1

Icon Leave Comment
1) Return type and method name always go together

2) Native method declarations have no body

3) A top-level class can be public, abstract/final, strictfp.

4) A top-level interface can be public, abstract, strictfp. abstract is implied by default.

5) Inner classes can be only public, protected, private, static, abstract/final.

6) Inner interfaces can be only public, protected, private, static, abstract.

7) Only one class/interface can be public in a source file and if it is so, the name of the source file should be the name of the public class/interface.

8) For searching a pattern use, the pattern and matcher. E.g
String str=”bcbcbcbabcbbcbcbababbaba”;
String regex=”bc”;
Pattern pattern = Pattern.compile(regex);
Matcher match = pattern.matcher(str);
while(matcher.find()) {
	System.out.println(matcher.start);
}



9) Program to count the number of times a character appears in a string:
Map hmap2 = new HashMap();
 for(int counter1 =0; counter1<str.length();counter1++) {
		if (hmap2.containsKey(String.valueOf(str.charAt(counter1)))) {
			count = (Integer)hmap2.get(String.valueOf(str.charAt(counter1)));
			count++;
		} else {
			count = 1;
		}
		hmap2.put(String.valueOf(str.charAt(counter1)),new Integer(count));
 }
		
 Iterator it = hmap2.keySet().iterator();
 while(it.hasNext()) {
	   key = (String)it.next();
	   System.out.println(key+ "			 " + hmap2.get(key));
			
 }



10) Code to create database connection, execute statement and process results is:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Database","a","b");
Statement stmt = con.createStatement();
ResultSet set = stmt.executeQuery("Select * from abc");
while(set.next()) {

}



11) All the methods of final class are also final as the class can’t be subclassed and hence the methods can’t be overridden.

12) volatile and final can’t go together
transient makes member variables non-persist able while serializing the object.

13) volatile variables maintain a master copy in addition to the local copy of each thread and each thread has to keep its local copy in sync with the global copy. These variables are useful to avoid concurrency problem when synchronization is not an option. They are slower.
To avoid having different people using the same package name, Sun recommends using the reverse of the internet domain as package name. e.g com.sun.java

14) abstract and final are mutually exclusive

15) All java classes must extend a superclass.
All variables in an interface are public static final by default and all methods are public abstract by default. public because compiler knows that interface by itself is abstract and somebody needs to implement it, so even if public is omitted the methods and variables are public by default.

16) Method access modifiers: public, private, protected or none (default)
Method special modifiers: abstract, final, static, synchronized, native
When abstract is used for a method then final, synchronized, strictfp and native can’t be used.

17) Constructor facts:
a. The return type is implicitly this, but this value can’t be collected using assignment operator
b. Constructor can’t be inherited as other superclass methods
c. The constructor can’t be final, abstract, synchronized, native, static (special modifiers)

18) If only one constructor is supplied and is made private, no class can instance or extends that class.

19) As main method is declared static, it can’t use non-static variables or call non-static member functions.

20) Only a constructor can invoke another constructor and that call must be the first statement of the calling constructor. (this() or super())

21) super() is usually used to call parameterized superclass’ constructor. If there is no call to superclass constructor and there is no no-argument constructor then compiler flashes error because compiler automatically inserts a call to no-argument constructor.

22) Superclass Instance Method Superclass Static Method
Subclass Instance Method Overrides Generates a compile-time error
Subclass Static Method Generates a compile-time error Hides
If static is specified in either superclass or subclass for a method the both the other class should also make it static.

23) No variable can be native, abstract or synchronized. Member variables can be transient, final, static, volatile. Local variables can be final only.

24) For same class names, use the fully qualified name. If not done so, the first path in the classpath will be used.

25) const and goto are keywords but are not in use.

26) Local objects and primitive variables must be initialized before first use, not necessarily on the first line.

27) “John” + b is not a String literal.

28) An array of superclass can accommodate objects of subclass by auto typecasting. But the reverse is not true. Even if explicit cast is done, java.lang.ClassCastException will be thrown at runtime.

29) Interfaces can’t have variables with modifier other than public static and final.

30) An interface can’t implement another interface

31) An interface can’t extend any class

32) An interface can only extend other interfaces. (super interfaces)

33) An abstract class can implement an interface.

34) An abstract class can extend other classes.

35) An abstract class can have final methods.

36) static final variable can be a member variable and needs special treatment than that without static keyword. Since it is static, the initialization in constructor may not work. It will work if the variable has been initialized before the class was loaded. This initialization can be in a static block or when declaring the variable. Once done that it becomes definitely assigned and hence can’t be reassigned. Moreover a final global variable must be assigned while a final local variable may not be.

37) If not initialized while declaring a non-static final variable is expected to be initialized in all the constructors.

38) But if there is an infinite loop in a constructor, then the initialization of definitely unassigned non-static final variable can be avoided.

39) For an infinite loop, the definitely unassigned non-static final variable will be unassigned for first iteration and will be assigned for every iteration afterwards. Hence compiler flashes error in both cases.

40) But since an if(true) is iterated only once, the initialization can’t be avoided as far as constructor is concerned. Predict the case for if(false). Yes, the compiler flashes error.

41) If definitely unassigned non-static final variable is assigned in both if(cond) and corresponding else then compiler comes to know that the variable will be initialized in any case and hence no error.

42) For final primitives, the value may not be altered once it is initialized. For objects, the data within the object may be modified but the reference variable may not be changed.

43) Importing packages doesn't recursively import sub-packages.

44) Unicode characters can appear anywhere in the source code. The following code is valid.
ch\u0061r a = 'a';
char \u0062 = 'b';
char c = '\u0063';



45) Static variables are initialized at class load time.

46) If you create an array of 5 Strings, there will be 6 objects created.

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


48) length is final instance variable in case of arrays.

49)
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 


50) Square brackets in case of arrays can come after datatype or before/after variable name

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

16 user(s) viewing

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