Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
SCJP 5.0 cheat sheet

SCJP 5.0 cheat sheet

Ratings: (0)|Views: 413|Likes:
Published by Radu Ovidiu Candea
SCJP 5.0 cheat sheet
Autor: Radu Candea
SCJP 5.0 cheat sheet
Autor: Radu Candea

More info:

Published by: Radu Ovidiu Candea on Aug 07, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC or read online from Scribd
See more
See less

11/12/2012

 
SCJP 5.0. review
JAVA 5.0 KEYWORDSRANGES FOR JAVA PRIMITIVESINITIALIZATIONOVERLOADING EXMPLE
 
ASCII CODESCORE
A valid identifier must start with a letter a "$" or "_", and the rest of the characters can be letters, digits, "$", "_". No "-", "*","?"....... are accepted.32 is the ASCII code for space, 48 is the ASCII code for 0, 65 is the ASCII value of the character “A”, 96 represents the ASCIIcode for “a”.All comparisons (accept NaN != NaN) including NaN will result in falseAll operations including NaN (+, -,* ,/) will result in NaNYou can return a short in a method that is supposed to return an int, but you cannot return a long without an explicit cast.2.5 is implicitly a double, 2 is implicitly an intWhen a floating-point number is cast to an
int 
, it simply loses the digits after the decimal.A
long 
can be cast into a
byte
. If the
long 
is over 127, it loses its most significant (leftmost) bits.x/y will have the type of the “larger” variable. Watch out for “possible loose of precision” error. (you may need an explicit cast).double vi(){ return 1/2; } returns 0.0 // int implicitly cast to doubledouble vi(){ return 1/2.0; } returns 0.5An array’s elements are initialized with the default value (‘\u0000’ for char, 0.0f for float ..)When you create an array by enumerating the elements you don't specify the size (it will be taken from the number of enumeratedelements)If a dimension of an array has not been initialized, attempting to access an un-initialized object element raises a NullPointerException.You can create an array of size -1, without compilation errors, but you get a java.lang.NegativeArraySizeException. You cancreate an array of size 0, without compilation errors.argCopy, an array declared: String [][] argCopy = new String[2][2]; can be reassigned to argCopy = new String []{"1", "2", "3","4"}; without compilation or runtime error Switch statements can evaluate only to enum, byte, short, int, and char data types (literals or final variables) => passing a long to aswitch causes a compiler error.You cannot have duplicate values in a switch. Make sure you check the value for the final expressions used inside the switchStrictfp can be used in combination with classes, interfaces and methods, meaning all values used during floating-pointcalculations are made in the IEEE-standard float or double sizes.
OPERATORS
The ^ operator (called the "logical XOR"), returns true if exactly one operand is true. boolean x = false; if (x = true) { } // an assignment, so x will always be true!When you use modulo (%) operator, drop all the negative signs, compute the modulo and attach a "-" only if the left operand isnegative (-5 % -2) = - (5 % 2) = -1The & operator has a higher precedence than the | operator The conditional operators have higher precedence than assignment operators(s instanceof String) can be used only to test objects (or null) against class types that are in the same class hierarchy. => you geterror if they are not in the same class hierarchy. (should be used only when overriding equals())If equals() is well defined -> use of instanceof operator, will always return false when comparing Objects from different classesIf you don't override equals(), your objects won't be useful hashing keys, and different object can’t be considered equal. Whenoverriding equals(), use the instanceof operator to be sure you're evaluating an appropriate class.The instanceof operator returns false if the object on the left side is null. The object on the left side needs to be always explicitinitialized. (even if used in a constructor)When you use = = you can get an incomparable types error, if the 2 objects you want to compare are differentIn static methods there is no need to initialize variables
GOTCHAS
Things to check first:- see if any local variable is used before initialization
 
- see if you have any block variable used outside the block - see if any non-static variable or method used inside static context- see if we don't have a possible loss of precision error (when we have a lot of numerical calculation)- see that all methods return what they are supposed to return (special attention to methods that include a switch)Regular classes can only be marked public (or default).If a method is taking a short as a parameter, an int needs to be cast before sending it as a parameter to that method.The final modifier assures that a reference variable cannot be referred to a different object, but final doesn’t keep the object’s statefrom changing.Watch out for assignments in if() clauses, can affect the rest of the if () clausesStatic methods can’t use the reference this.You can get an error "illegal forward reference", if you're trying to use a variable that has not been initialized. This happens onlyin really obvious cases, when constructors and other methods are not involved in the processhashCode() is an native methodclone() is declared protected in Object class, so you can't invoke it from other packagesThe Object equals() method returns true only it the 2 objects compared are = =
THEORY
Javac Class.java and then java Class (no .java required this time) => a NoClassDefFoundError will be thrownWhen you specify a class path in the command, you will overwrite the system class path (the first one from the left will beconsidered when you have more than one file with the same name)Coupling refers to the degree to which one class knows about or uses members of another class.When using interface types, not a class and the interface provides a small number of methods you are promoting low coupling.Cohesion refers to the degree in which a class has a single, well-defined role or responsibility.Has-a relationships always rely on instance variables, and has-a relationships are not necessary always tightly coupled.Static initialization blocks run once, when the class is first loaded.Instance initialization blocks run every time a new instance is created. They run after all super-constructors and before theconstructor's code has run.
INTERFACES:
Interface’s methods are by default public and abstract — explicit declaration of these modifiers is optional.Interface’s variables are by default public, final and static — explicit declaration of these modifiers is optional. Watch out whentrying to modify a variable declared in an interface in the body of an implementing classInterface methods can not be final or static.Interface variables cannot be protected or transient
EXCEPTIONS
IllegalArgumentException is the superclass for NumberFormatExceptionA try-finally block doesn’t handle the Exception, so after the finally is executed the Exception will be propagatedYou can have a try inside a catchIllegalMonitorStateException is thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.Error and RuntimeException are not checked so you can throw them in an overridden method.IllegalArgumentException, IllegalStateException and NumberFormatException, are not thrown by the JVM (are thrown bymethods when some conditions are not appropriate)StackOverflowError is thrown when a stack overflow occurs because an application recourses too deeply.When casting, a ClassCastException will be thrown at runtime (It won’t be a compiler error)When throwing exception manually in a block of code, watch out for "unreachable statement" compiler error The assertion statement has two forms: assert
 Expression
1
; and assert
 Expression
1
:
 Expression
2
; where
 Expression
1
is a booleanexpression and
 Expression
2
is an expression that has a value. (It cannot be an invocation of a method that is declared void.)Do not use assertions to validate arguments to public methods.

Activity (4)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Gareth Thomas liked this
date12345678 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->