📄 tips.html
字号:
<body bgcolor="#FFFFFF">
QUICK REVISION TIPS OF SCJP 1.4
<ol>
<li><b> Declaration And Access Control</b><br>
<br>
1. An identifier in java must begin with a letter, a dollar sign($), or an
underscore (_); subsequent characters may be letters,
dollar signs, underscores, or digits.<br>
2. All the keywords in java are comprised of lower case characters only.<br>
3. There are three top-level elements that may appear in a file. None of these
elements is required. If they are present, then they
must appear in the following order:<br>
-package declaration<br>
-import statements<br>
-class definitions<br>
4. A Java source file (Java file) cannot have more than one public class,
interface or combination of both.<br>
5. The variables in an interface are implicitly public, final and static.
If the interface, itself, is declared as public the methods and
variables are implicitly public.<br>
6. Variables cannot be synchronized.<br>
7. The variables in Java can have the same name as method or class.<br>
8. A transient variable may not be serialized.<br>
9. The transient keyword is applicable to variables only.<br>
10. The native keyword is applicable to methods only.<br>
11. The final keyword is applicable to methods, variables and classes.<br>
12. The abstract keyword is applicable to methods and classes.<br>
13. The static keyword is applicable to variables, methods or a block of code
called static initializers.<br>
14. A native method cannot be abstract but it can throw exception(s).<br>
15. A final class cannot have abstract methods. <br>
16. An abstract class might not have any abstract methods.<br>
17. All methods of a final class are automatically final.<br>
18. Interfaces cannot be final and should not be declared abstract.<br>
19. The visibility of the class is not limited by the visibility of its members.
i.e. A class will all the members declared private can
still be declared public.<br>
20. Interface methods cannot be native, static, synchronized, final, private,
protected or abstract. They are implicitly public,
abstract and non-static.<br>
21. The statement float f = 5.0; will give compilation error as default type
for floating values is double and double cannot be directly assigned to float
without casting. But f=5.0f is ok.<br>
22. A constructor cannot be native, abstract, static, synchronized or final.
<br>
23. Be careful for static and abstract keyword in the program.<br>
24. Also be careful for private keyword in front of a class as top level classes
or interfaces cannot be private.<br>
25. friendly is not a java keyword. This is often used as an alternate word
in java literature to indicate the default access level by
placing no modifier like public, private or protected in front of members
of classes and interfaces.<br>
26. A local variable, already declared in an enclosing block and therefore
visible in a nested block, cannot be redeclared in the
nested block.<br>
27. Array in Java can be declared and defined like --- <br>
int[] count = null; int []count = null;
int count[] = null;<br>
int count[] = new int[50]; int count[]
= {5,10,15,20,25}<br>
<br>
</li>
<li><b>Flow Control, Assertions And Exception Handling</b>
<ol>
<br>
<li type=1 value=28> Three types of statements regarding flow controls are
used in Java: <br>
* Selection statements ' if…else,
switch…case, try…catch…finally<br>
* Iteration statement ' while, do…while,
for<br>
* Transfer statement ' return, break,
continue<br>
</li>
<li> The argument to switch can be either byte, short, char or int. Be careful
about long, float, double or boolean as argument to switch.<br>
</li>
<li> The expression for an if and while statement in Java must be a boolean.<br>
</li>
<li>Breaking to a label (using break <labelname>;) means that the
loop at the label will be terminated and any outer loop will keep iterating.
While a continue to a label (using continue <labelname>;) continues
execution with the next iteration of the labeled loop.<br>
</li>
<li>The if() statement in Java takes only boolean as an argument. Note that
if (a = true){}, provided a is of type boolean is a valid statement then
code inside the if block will be executed otherwise skipped.<br>
</li>
<li>The (-0.0 == 0.0) will return true, while (5.0 == -5.0) will return
false.<br>
34. An assertion is a conditional expression that should evaluate to true
if and only if your code is working correctly. If the expression
evaluates to false, an error is signaled. Assertions are like error checks,
except that they can be turned completely
off, and they have a simpler syntax.<br>
</li>
<li> AssertionError is the immediate subclass of java.lang.Error.<br>
</li>
<li>assert is a java keyword from JDK1.4. So it cannot be used as an identifier
from JDK1.4 or later. But as other JDK versions (JDK1.3 or earlier) had
no keyword named assert, an interesting backward compatibility problem
arises for those programs that used assert as a java identifier. <br>
</li>
<li> Assertion checks can be turned on and off at runtime. By default, assertion
mechanism is turned off. When assertions are off,
they don't use system resources.<br>
</li>
<li>The command for compiling a source using Java's new assertion feature
is javac -source 1.4 filename.java. But if -source argument is not mentioned
like javac filename.java, it will be assumed like javac -source 1.3 filename.java
so that existing code compiles correctly even if it uses assert as a regular
identifier.<br>
</li>
<li> Remember that Assertions can be enabled and disabled for specific packages
as well as specific classes. For example, assertions can be enabled in
general but disabled for a particular package.<br>
</li>
<li> One of the most common uses of assertions is to ensure that the program
remains in a consistent state. Assertions are not alternative to exception
handling rather complementary mechanism to improve discipline during development
phase.<br>
</li>
<li>Assertions may be used in the situations like ---<br>
* to enforce internal assumptions about
aspects of data structures.<br>
* to enforce constraints on arguments to
private methods.<br>
* to check conditions at the end of any
kind of method.<br>
* to check for conditional cases that should
never happen.<br>
* to check related conditions at the start
of any method.<br>
* to check things in the middle of a long-lived
loop.<br>
</li>
<li>Assertions may not be used in the situations like ---<br>
* to enforce command- line usage.<br>
* to enforce constraints on arguments to
public methods.<br>
* to enforce public usage patterns or protocols.<br>
* to enforce a property of a piece of user
supplied information.<br>
* as a shorthand for if ( something) error();<br>
* as an externally controllable conditional.<br>
* as a check on the correctness of your
compiler, operating system, or hardware, unless you have a specific </li>
<li>reason to believe there is something wrong with it and are in the process
of debugging it.<br>
</li>
<li> An overriding method may not throw a checked exception unless the overridden
method also throws that exception or a superclass of that exception.<br>
</li>
<li> The java.lang.Throwable class has two subclasses: Exception and Error.<br>
</li>
<li>An Error indicates serious problems that a reasonable application should
not try to catch. Most such errors are abnormal conditions.<br>
</li>
<li> The two kinds of exceptions in Java are: Compile time (Checked) and
Run time (Unchecked) exceptions. All subclasses of Exception except the
RunTimeException and its subclasses are checked exceptions.<br>
Examples of Checked exception: IOException,
ClassNotFoundException.<br>
Examples of Runtime exception: ArrayIndexOutOfBoundsException,
NullPointerException, ClassCastException, ArithmeticException, NumberFormatException.<br>
</li>
<li>The unchecked exceptions do not have to be caught.<br>
</li>
<li> A try block may not be followed by a catch but in that case, finally
block must follow try.<br>
</li>
<li> While using multiple catch blocks, the type of exception caught must
progress from the most specific exception to catch to the superclass(es)
of these exceptions.<br>
</li>
<li> More than one exception can be listed in the throws clause of a method
using commas. e.g. public void myMethod() throws IOException, ArithmeticException.<br>
</li>
<li> Dividing an integer by 0 in Java will throw ArithmeticException.<br>
</li>
</ol>
</li>
<br>
<li><b>Garbage Collection</b><br>
52. Setting the object reference to null makes the object a candidate (or
eligible) for garbage collection.<br>
53. Garbage collection mechanism in java is implemented through a low priority
thread, the behavior of which may differ from
one implementation to another. Nothing can be guaranteed about garbage collection
except the execution of finalize().<br>
54. Garbage collection in Java cannot be forced. The methods used to call
garbage collection thread are System.gc() and Runtime.gc().<br>
55. To perform some task when an object is about to be garbage collected,
you can override the finalize() method of the Object
class. The JVM only invokes finalize() method once per object. The signature
of finalize() method of Object class is
: protected void finalize() throws Throwable.</li>
<br>
<br><li><b>Language Fundamental</b>
<ol>
<li value=56>const and goto are two keywords in Java that are not currently
in use.</li>
<li>null, true, false are reserved literals in Java.</li>
<li>null statement may be used after any statement in any number, in Java
if the statement is not unreachable (like after return statement) without
any effect in the program. Like --- if (i<50) i += 5;;;;;;;</li>
<li>NULL is not a reserved-word, but null is a reserved-word in Java. Java
is a case-sensitive language.</li>
<li>The initialization values for different data types in Java is as follows<br>
byte = 0, int = 0, short = 0, char = '\u0000', long = 0L, float = 0.0f,
double = 0.0d, boolean = false, object deference (of any object) = null.</li>
<li>A static method cannot refer to "this" or "super".
</li>
<li>A final variable is a constant and a static variable is like a global
variable.</li>
<li>A static method can only call static variables or other static methods,
without using the instance of the class. e.g. main() method cannot directly
access any non static method or variable, but using the instance of the
class it can. </li>
<li>instanceof is a Java keyword not instanceOf. </li>
<li>The following definition of main method is valid:<br>
static public void main(String[] args) </li>
<li>The main() method can be declared final.</li>
<li>this() and super() must be the first statement in any constructor and
so both cannot be used together in the same constructor.</li>
<li>The example of array declaration along with initialization - int k[]
= new int[]{1,2,3,4,9};</li>
<li>The size of an array is given by arrayname.length. </li>
<li>The local variables (variables declared inside method) are not initialized
by default. But the array elements are always initialized wherever they
are defined be it class level or method level. </li>
<li>In an array, the first element is at index 0 and the last at length-1.
Please note that length is a special array variable and not a method.
</li>
<li>The octal number in Java is preceded by 0 while the hexadecimal by 0x
(x may be in small case or upper case)<br>
e.g. octal :022 and hexadecimal :0x12 </li>
</ol>
</li>
<br>
<li> <b>Operators and Assignments</b>
<ol>
<li value=73> Bit-wise operators - &, ^ and | operate on numeric and
boolean operands.</li>
<li>The short circuit logical operators && and || provide logical
AND and OR operations on boolean types and unlike & and | , these
are not applicable to integral types. The valuable additional feature
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -