SCJP CX310-055 Short Notes: Exam passing tips

November 25th, 2008 by uCertify Leave a reply »

Declarations, Initialization, and Scoping

  • The super keyword can be used when a subclass needs to refer to its immediate superclass.
  • An inner class can be defined as private, default, public, or protected.
  • An inner class declared inside a method is private to the method. Therefore, it can neither be marked with an access modifier (public, private, or protected) nor with the static modifier.
  • A local class can access only those local variables or parameters in its enclosing method that are declared final. However, it can freely access any other variables (i.e., instance variables or static variables) in its enclosing class.
  • An abstract class can have abstract as well as concrete methods.
  • An enum can be declared inside, as well as outside a class. When declared outside a class, it should be declared either public or default. It cannot be private, protected, static, abstract, or final.
  • An enum can be used as an argument to the switch statement.
  • The java.lang.Enum class is the class from where all enums are subclassed.
  • An enumeration is very similar to a class, as it can have a constructor, an instance variable, and a method body.
  • A semicolon in the enum declaration is optional and may be put at the end of enum declaration.
  • For enum constants, the equals and == operations give the same result.
  • An enum constructor can never be invoked directly. They are always called automatically on initialization.
  • A variable declared as an enum constant is final.
  • To import a static member, the import statement should have the name of the package followed by the name of the static member. To import all the members of the static class, the import statement should have the name of the package followed by the class name and an *.
  • Static import allows the static members of the imported class to be treated as if they were declared in the class that uses them.
  • An inner class has access to the variables and methods of its enclosing class. Therefore, it does not need to save a reference to that class.
  • Overloaded methods share a common name but have a different argument list. They can have the same, or different, return types.
  • An abstract class cannot be final.
  • An abstract method only has a semicolon after the method name and parenthetical argument list. The subclasses provide the body of the abstract method.
  • An interface can extend one or more interfaces. However, it cannot extend or implement a class.
  • A method implementing an interface should be public.
  • Java does not allow the static modifier in interfaces.
  • An identifier must begin with a letter, a dollar sign ($), or an underscore (_). Subsequent characters may be letters, dollar signs, digits, or underscore.
  • Of all the variable types, static variables have the longest scope.
  • A local variable must be explicitly initialized before use.
  • The argument to the switch must be an integral expression that must evaluate to a 32-bit or smaller integer type: byte, short, char, or int. An enum can also be used in the switch expression.
  • The default value of array elements is zero.
  • The variable-length arguments must be the last argument in a method’s argument list.
  • Two variable length arguments cannot be written in one method.
  • A setter method should have void as its return type.
  • A static initializer block is used mainly for initialization. It is very similar to a constructor, but it cannot be inherited.
  • A static method cannot reference to the current object using keywords super or this.
  • A private member cannot be accessed outside a class.
  • When a class declares a method whose type signature is the same as the type signature of a method declared by one of its superclasses, it is known as method overriding.
  • Trying to override a final method results in a compile time error.
  • Using the covariant return feature, a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.
  • An array can also be used as a return type for a method.
  • A constructor can have a public, protected, or private access modifier. However, it cannot have a static, or final modifier.
  • A default constructor for a class is not created if the class explicitly defines any other constructor.
  • The access mode of the default constructor depends on the access mode of the class, i.e., if the class is public, the default constructor will also be public.
  • A local class declared in a static method has access only to static members of its enclosing class.

Flow Control

  • The switch statement takes an argument that must be of byte, short, int, or char data type. No two case constants for a switch statement can have identical values.
  • As soon as a break statement is encountered, the program control is transferred to the first line of code that follows the entire switch statement.
  • If none of the constants after the case statements match the value passed to the switch statement, the default statement will be executed.
  • If the number of statements after the if clause is more than one, it is necessary to put them inside the
    curly braces. However, in case of a single statement, the curly braces are optional.
  • The initialization portion of a for loop is generally an expression that sets the value of the loop control variable. It executes only once. The condition portion must be a boolean expression. The iteration portion is usually an expression that increments or decrements the loop control variable.
  • All the three components, i.e., initialization, condition, and iteration are optional. In case there is only
    a single statement in the body of the loop, the curly braces can be omitted.
  • A for-each loop is an enhanced for loop. It is used to iterate over arrays and collections.
  • A for-each loop can be used to iterate over elements of a collection only in the forward direction but not in the reverse order.
  • The java.lang.AssertionError class is a direct subclass of the java.lang.Error class.
  • The assert statement can be used in two forms as shown below:
    1. assert expr1;
    2. assert expr1: expr2;
  • Assertions are disabled at runtime.
  • To disable assertion, -da ClassName or -da PackageName is used, and to enable assertion, -ea ClassName or -ea PackageName is used.
  • A try block must be followed by a catch block or a finally block.
  • There cannot be any code between the try and catch/finally block.
  • If an exception occurs within the try block at a particular line, the rest of the lines in the try block will
    never be executed.
  • The order of catching an exception is important. The superclass exception can be thrown after the
    subclass exception, but the reverse is not possible.
  • Command line arguments are used to provide input to programs. These arguments are supplied as
    parameters to the application program at the time of invoking it for execution.
  • ArrayIndexOutOfBoundsException is thrown at runtime to indicate that an array has been accessed
    with an illegal index.
  • Both the arithmetic division and modulus operators may throw an exception of type
    ArithmeticException. Floating-point division by zero does not cause this exception.
  • NumberFormat is an abstract class that is used to format numbers and currency values.

API Contents

  • The operator concatenates two strings, producing a new String object as the result.
  • The String objects are immutable, i.e., once an object of the String class is created, the string it
    contains cannot be changed.
  • The trim() method removes any leading and trailing blank spaces of a String.
  • String, StringBuffer, and StringBuilder are all different types. They are all derived from the Object
    class and none of them extend one another.
  • Autoboxing is the automatic conversion of numeric objects to primitives.
  • Void is a Wrapper class. However, it does not wrap any primitive value and cannot be instantiated.
  • The StringBuilder class can act as an alternative to StringBuffer in places where the environment is
    not multithreaded, i.e, where synchronization or multithreading is not significant.
  • The wrapper classes Integer, Double, Float, and Long provide the toHexString() method.
  • The toHexString() method converts a double value to a hexadecimal string.
  • The Writer class is used to write characters to output streams using the desired character encoding.
  • A FileReader object can be wrapped in a BufferedReader object and a FileWriter object is
    wrapped in a PrintWriter object to achieve better performance.
  • The FileReader class is a subclass of the Reader class and is used to read 16-bit Unicode characters from a file.
  • The FileWriter class is a subclass of the Writer class and is used to write 16-bit Unicode characters to a file.
  • The File class is used to access the file and directory objects. It provides access to the files and
    directories in the current working directory.
  • The File class is useful only for querying and modifying the attributes of a file. It does not have any
    method for reading and writing to and from a file.
  • The createNewFile() method returns true if the file has been created, else it returns false.
  • The write() method writes its argument to a file when it is called using an instance of the FileWriter class.
  • The read() method reads data from a file when this method is called using an instance of the FileReader class.
  • The FileInputStream class is used to perform simple file input operations. It is used to read binary data from a file. It reads data in a sequential manner.
  • Serializable is a marker interface and does not define any method.
  • Static variables cannot be serialized.
  • Transient variables cannot be serialized.
  • DateFormat is an abstract class and hence cannot be instantiated.
  • The Calendar class does not provide any public constructor.
  • The Calendar class provides methods to convert the time in milliseconds to hours and minutes.
  • The month field of the calendar starts from zero.
  • The java.util.Locale class is used to format dates, numbers, and currency for specific locales.
  • A split() function belongs to the String class. It simplifies the task of breaking (splitting) a string into substrings or tokens.
  • Scanner is a class present in the java.util package. Like the BufferedReader class, this class is used
    to take user inputs. However, unlike BufferedReader, which throws checked IOException, it does not throw any checked exception.
  • The printf() method is used to output a formatted string to the console. This method automatically
    uses the Formatter to create a formatted string.
  • The %d format is used to format the digits to display using the System.out.format() method. %n is a
    newline character. Moreover, %% is used to display the % sign.
  • Following are the flags used with the Formatter:
    1. %-: Left justify the arguments
    2. %0: Pad the number with zeros
    3. %n: Newline character
    4. %%: Insert a % sign
  • The hasNextInt() method returns true if the next token in this scanner’s input can be interpreted as an
    int value.
  • The hasNext() method determines whether the given iterator has more elements, and the next()
    method returns the next element in an iteration.

Concurrency

  • A class implementing the Runnable interface must implement the run() method.
  • The return type of the run() method must be void.
  • When a class extends the Thread class, the run() method should be overridden to implement the
    code that will be executed by the thread.
  • The thread.start() method calls the run() method of a thread.
  • The start() method belongs to the java.lang.thread class. When a thread is instantiated, it is said to
    be in the newborn state until its start() method is called.
  • Threads have four states in their lifetime, which are ready, running, waiting and dead.
  • The sleep() method is a static method and always acts on the currently running thread.
  • The sleep() method throws InterruptedException if it is not handled properly by using the try/catch
    block.
  • Methods such as join() and sleep() throw an InterruptedException. Hence, they must either be
    written in the try-catch block or the calling method must declare the throws clause.
  • While a thread is inside a synchronized method, all other threads that try to call it at the same instance have to wait.
  • A thread can obtain a mutually exclusive lock on a synchronized method of an object.
  • The wait(), notify(), and notifyAll() methods must be called within a synchronized method.
  • Only methods or blocks can be synchronized, not variables or classes. A class can have both
    synchronized and non-synchronized methods.
  • Each thread has a separate copy of a method-local variable. Hence, if two or more threads try to
    access the variable, they will never interrupt each other, as each thread will use its own copy of the variable.

OO Concepts

  • A static variable cannot use this or super as a reference.
  • Good Object oriented programming requires low coupling and high cohesion.
  • While overriding a method, the version of the method being called is determined at runtime, rather
    than at compile-time.
  • A ClassCastException extends from the RuntimeException class. It is thrown when an attempt is
    made to cast an object, which is not of the appropriate run-time type.
  • The word polymorphism combines poly with morphism, which means many forms. Polymorphism is
    a feature that allows an interface in Java to be used by many classes for different purposes.
  • When a class is instantiated, the runtime system creates a separate copy of each instance variable
    defined by the class. Therefore, each instance of a class has its own copy of all instance variables defined by the class.
  • Initialization blocks do not have names and return types, and they cannot take arguments.
  • Initialization blocks execute in the same order in which they appear. They run once when the class is first loaded. An instance initialization block runs every time a class instance is created.
  • Static blocks, static methods, and static variables are given priority by the compiler. Static initializer blocks are called first. This is followed by a constructor, and then the main method is called. Anything other than these will be called after the main method.
  • A class with private constructor cannot be inherited.
  • In Java, if a class does not define any constructor, an implicit default constructor is supplied for the
    class. The default constructor takes no parameters and simply calls the no-parameter constructor in the superclass.
  • In a class hierarchy, constructors are executed in the order of derivation from superclass to subclass. In other words, the first statement that executes in a constructor in a subclass is a call to the constructor in the superclass.
  • The “is a” relationship implies that one class extends another class, or that one class is a kind of another class.
  • The “has a” relationship implies that a class owns a reference to different objects.
  • Method overriding is a way of changing the behavior of an existing base class method. When a class
    declares a method whose type signature is the same as the type signature of a method declared by one of its superclasses, it is known as method overriding.

Collections / Generics

  • The elements in the ArrayList collection are ordered. The ArrayList class extends AbstractList and implements the List interface. The ArrayList is an ordered collection. It supports dynamic arrays that can grow as needed.
  • The ArrayList collection is not immutable because it may contain duplicate elements.
  • A marker interface has no method.
  • A TreeSet does not allow duplicate elements to be added to itself. Moreover, elements from a
    TreeSet are returned in ascending alphabetical order.
  • The hash value of an empty string is zero.
  • Objects that are equal according to the equals(Object) method always produce the same hash code
    value. However, it is not necessary for the objects that are unequal according to the equals(java.lang.Object) method to produce different hash code values.
  • The sort(int[] a, int startIndex, int endIndex) method sorts the specified range of the specified array
    of ints in an ascending numerical order. The range to be sorted extends from index startIndex, inclusive, to index endIndex, exclusive.
  • The Map does not extend the Collection interface.
  • A List is an ordered collection of elements. The elements of a List are stored in a sequence. They
    can be inserted or accessed by their position in the list, using a zero-based index.
  • All types, except an enum type, an anonymous inner class, and an exception class, can be used as
    generics.
  • The J2SE 5 compiler allows the pre-generics code to compile but prints a warning during compilation.
  • A PriorityQueue stores elements either in the natural order or in the order specified by a
    comparator.
  • Generics checks for type safety only at compile-time. There is no runtime type checking in generics.
  • The Arrays class contains methods to search for a specific element. The following rules are used for
    search operation in arrays:

    1. Searches are performed using the binarySearch() method.
    2. Successful searches return the int index of the element being searched.
  • The remove() and poll() methods differ only in their behavior when the queue is empty. The
    remove() method throws an exception, while the poll() method returns null. The poll() method is associated with Queues.
  • Both the Comparator and the Comparable interfaces compare the first object in the statement with
    the second object. Both these interfaces return negative, zero, or positive values when the second object is greater than, equal to, or smaller than the first object respectively.
  • The Collections.sort() method of the java.util package is used to sort the elements in the ArrayList.
  • If the Comparator is not used either at the time of sorting or while searching, the result will be
    unpredictable.
  • The PriorityQueue class implements the Comparable interface. Hence, it should override the
    compareTo() method of the Comparable interface.
  • The offer() method of the PriorityQueue class is used to insert the elements in the Queue.

Fundamentals

  • A native method is one for which the body of the method is defined elsewhere, entirely outside the Java Virtual Machine, in a library. Only methods can be declared as native.
  • It is not necessary to initialize a primitive type final variable in the same statement in which it is
    declared.
  • The package statement must be the first statement in code.
  • The correct signature of the main() method is not needed by the Java compiler to compile a class,
    but it is required by the Java interpreter to execute it.
  • Command line arguments are parameters that are supplied to the application program at the time of
    invoking it for execution.
  • The arguments passed through command-line are stored as strings in the String array passed to the
    main() method.
  • The length field of an array always has a non-negative value. If the length of an array is zero, it is
    called an empty array.
  • In Java, whatever is entered in the command line starts from the zeroth index of the array declared in
    the main() method. It does not include the Java command and the name of the program.
  • Garbage collection is a feature in Java that automatically reclaims the memory resources used by an
    object. If an object is no longer referenced by any variable, it becomes eligible for garbage collection.
  • As long as an object is accessible from a live thread, the object remains valid and the memory
    associated with it cannot be reused.
  • The gc() methods in the System and Runtime classes make it more likely that the JVM garbage
    collection will run.
  • The initial value of a reference variable depends on its place of declaration. Class variables and
    instance variables are initialized with a special null value, whereas variables declared inside code blocks are not automatically initialized.
  • JAR (Java Archive) file is a utility for bundling and deploying Java programs. It is a kind of zip file,
    created using the JAR tool.
  • Usually, a JAR file contains class files, image files, and audio/video files. It also contains a manifest
    file, which contains information such as whether the class file is a javabean, or which class contains the main method, etc.
  • The syntax for creating a JAR file is as follows:
    jar cmf mainfilename jarfilename.jar dirname
  • The % operator is known as modulus operator. It is used to calculate the remainder in a division
    operation.
  • The increment operator is a unary operator that increases the value of its operand by one. For example, the expression a++ increases the value of a by 1. This statement is equivalent to the expression a = a + 1.
  • An int is a signed 32-bit value that has a range from -231 through 231 -1, whereas long is a signed
    64-bit value that has a range from -263 through 263 -1.
  • In the prefix form, it appears before the operand. For example, –a;
  • In the postfix form, it appears after the operand. For example, a–;
  • A widening conversion does not need any explicit cast.
  • In call-by-reference method call, a reference to an argument (not the value of the argument) is
    passed to the parameter.
  • The modulus operation evaluates to a negative value only when the dividend is negative.
  • The short-circuit AND operator evaluates its second operand only when its first operand evaluates
    to true.
  • The short circuit OR operator does not evaluate its second operand if its first operand evaluates to true.
  • The instanceof operator is a binary operator that determines at runtime whether its left operand is an
    instance of its right operand.
  • Operators that work on only one operand are called unary operators. The unary operator is used to
    emphasize the sign of the operand, whereas the unary – operator is used to arithmetically negate the value of the operand.
Like this article? Share it with others
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.