Sie sind auf Seite 1von 11

Legal Identifers ================ -> Identifiers must start with a letter, a currency character ($), or a connecting character such

as the underscore ( _ ). Identifiers cannot start with a number! -> After the first character, identifiers can contain any combination of letters, currency characters, connecting characters, or numbers. -> In practice, there is no limit to the number of characters an identifier can contain. -> You can't use a Java keyword as an identifier. Table 1-1 lists all of the Java keywords including one new one for 5.0, enum. -> Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Keywords: ========= abstract, , boolean , break , byte , case , catch, char , class , const , continue , default , do , double , else , extends , fnal , fnally , float, for , goto if implements import instanceof, int interface long native new package, private protected public return short static, strictfp super switch synchronized this throw, throws transient try void volatile while, assert enum

Source File Declaration Rules: ============================== -> There can be only one public class per source code file. -> Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the positioning rules discussed here. -> If there is a public class in a file, the name of the file must match the name of the public class. For example, a class declared as public class Dog { } must be in a source code file named Dog.java. -> If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. -> If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file. -> import and package statements apply to all classes within a source code file. In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports. -> A file can have more than one nonpublic class.

-> Files with no public classes can have a name that does not match any of the classes in the file

Class Access: ============= =>What does it mean to access a class? ->Create an instance of class B. ->Extend class B (in other words, become a subclass of class B). ->Access certain methods and variables within class B, depending on the access control of those methods and variables. access means visibility

N-Access Modifiers: =================== -> strictfp, abstract, final, -> abstract and final cannot go together -> strictfp is a keyword and can be used to modify a class or a method, but never a variable. Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points. Without that modifier, floating points used in the methods might behave in a platform-dependent way. If you don't declare a class as strictfp, you can still get strictfp behavior on a method-by-method basis, by declaring a method as strictfp. If you don't know the IEEE 754 standard, now's not the time to learn it. -> even a single method is abstract, the whole class must be declared abstract.

=> Creating Abrstract Classes ( Problem Statement ) Create an abstract superclass named Fruit and a concrete subclass named Apple. The superclass should belong to a package called food and the subclass can belong to the default package (meaning it isn't put into a package explicitly). Make the superclass public and give the subclass default access.

Interfaces:

Implmenting class must implement the methods with a public modifier.

-> All interface methods are implicitly public and abstract. In other words,

you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract. -> All variables defined in an interface must be public, static, and final in other words, interfaces can declare only constants, not instance variables. Interface methods must not be static. -> Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.) -> An interface can extend one or more other interfaces. -> An interface cannot extend anything but another interface. -> An interface cannot implement another interface or class. -> An interface must be declared with the keyword interface. -> Interface types can be used polymorphically (see Chapter 2 for more details). Typing in the abstract modifier is considered redundant; interfaces are implicitly abstract whether you type abstract or not. You just need to know that both of these declarations are legal, and functionally identical: public abstract interface Rollable { } public interface Rollable { } interface variables are constants ( public static final)

Access Modifiers:

Public Access:

Private Access: What about a subclass that tries to inherit a private member of its superclass? When a member is declared private, a subclass can't inherit it. For the exam, you need to recognize that a subclass can't see, use, or even think about the private members of its superclass. You can, however, declare a matching method in the

subclass. But regardless of how it looks, it is not an overriding method! It is simply a method that happens to have the same name as a private method (which you're not supposed to know about) in the superclass. Hence - Can a private method be overridden by a subclass? That's an interesting question, but the answer is technically no. Since the subclass, as we've seen, cannot inherit a private method, it therefore cannot override the methodoverriding depends on inheritance. We'll cover the implications of this in more detail a little

Protected and Default Members


The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.

But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.

package certification; public class Oscar { protected void test() { System.out.println("Hi"); } } package otherPackage; import certification.Oscar; public class Amy extends Oscar{ /** * @param args */ public void est(){ // TODO Auto-generated method stub Amy osc = new Amy(); osc.test(); Oscar osc1 = new Oscar(); osc1.test(); // Compile Time Error! As you can not access protected method in other package via superclass reference } } package otherPackage; public class KebabMeHaddi extends Amy{ public void haddi(){ test(); Amy a = new Amy(); a.test(); // Compile Time Error! } }

For a subclass outside the package, the protected member can be accessed only through inheritance. Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) Sbecomes private to any code outside the subclass, with the exception of subclasses of the subclass.

Local Variables
You can be certain that any local variable declared with an access modifier will not compile. In fact, there is only one modifier that can ever be applied to local variablesfinal

Final
Methods cannot be overridden. Value cannot be changed for final arguments. Abstract
If even one method in the class is abstract, the class has to be marked abstract You can, however, have an abstract class with no abstract methods. The first concrete subclass of an abstract class must implement all abstract methods of the superclass.
public abstract class Vehicle { private String type; public abstract void goUpHill(); // Abstract method public String getType() { // Non-abstract method return type; } } public abstract class Car extends Vehicle { public abstract void goUpHill(); // Still abstract public void doCarThings() { // special car code goes here } } public class Mini extends Car { public void goUpHill() { // Mini-specific going uphill code } }

Synchronized, Native, strictfp, var-args (TO-DO)

Constructors:
class Foo2 { // legal constructors Foo2() { }

private Foo2(byte b) { } Foo2(int x) { } Foo2(int x, int... y) { } // illegal constructors void Foo2() { } // it's a method, not a constructor Foo() { } // not a method or a constructor Foo2(short s); // looks like an abstract method

static Foo2(float f) { } // can't be static final Foo2(long x) { } // can't be final abstract Foo2(char c) { } // can't be abstract Foo2(int... x, int t) { } // bad var-arg syntax }

Primitive Types:

For boolean types there is not a range; a boolean can be only true or false. If

someone asks you for the bit depth of a boolean, look them straight in the eye and say, "That's virtual-machine dependent." The char type (a character) contains a single, 16-bit Unicode character. Although the extended ASCII set known as ISO Latin-1 needs only 8 bits (256 different characters), a larger range is needed to represent characters found in languages other than English. Unicode characters are actually represented by unsigned 16-bit integers, which means 216 possible values, ranging from 0 to 65535 (216)-1. You'll learn in Chapter 3 that because a char is really an integer type, it can be assigned to any number type large enough to hold 65535 (which means anything larger than a short. Although both chars and shorts are 16-bit types, remember that a short uses 1 bit to represent the sign, so fewer positive numbers are acceptable in a short).

Local variable The compiler will reject any code that tries to use a local variable that hasn't been assigned a value, becauseunlike instance variableslocal variables don't get default values.

ArrayList Think of this as a growable array. It gives you fast iteration and fast
random access. To state the obvious: it is an ordered collection (by index), but not sorted. You might want to know that as of version 1.4, ArrayList now implements the new RandomAccess interfacea marker interface (meaning it has no methods) that says, "this list supports fast (generally constant time) random access." Choose this over a LinkedList when you need fast iteration but aren't as likely to be doing a lot of insertion and deletion. Vector Vector is a holdover from the earliest days of Java; Vector and Hashtable were the two original collections, the rest were added with Java 2 versions 1.2 and 1.4. A Vector is basically the same as an ArrayList, but Vector methods are synchronized for thread safety. You'll normally want to use ArrayList instead of Vector because the synchronized methods add a performance hit you might not need. And if you do need thread safety, there are utility methods in class Collections that can help. Vector is the only class other than ArrayList to implement RandomAccess. LinkedList A LinkedList is ordered by index position, like ArrayList, except that the elements are doubly-linked to one another. This linkage gives you new methods (beyond what you get from the List interface) for adding and removing from the beginning or end, which makes it an easy choice for implementing a stack or queue. Keep in mind that a LinkedList may iterate more slowly than an ArrayList, but it's a good choice when you need fast insertion and deletion. As of Java 5, the LinkedList class has been enhanced to implement the java.util.Queue interface. As such, it now supports the common queue methods: peek(), poll(), and offer().

Set Interface
A Set cares about uniquenessit doesn't allow duplicates. Your good friend the equals() method determines whether two objects are identical (in which case only one can be in the set). The three Set implementations are described in the following sections. HashSet A HashSet is an unsorted, unordered Set. It uses the hashcode of the object being inserted, so the more efficient your hashCode() implementation the better access performance you'll get. Use this class when you want a collection with no duplicates and you don't care about order when you iterate through it. LinkedHashSet A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. Use this class instead of HashSet

when you care about the iteration order. When you iterate through a HashSet the order is unpredictable, while a LinkedHashSet lets you iterate through the elements in the order in which they were inserted. TreeSet The TreeSet is one of two sorted collections (the other being TreeMap). It uses a Red-Black tree structure (but you knew that), and guarantees that the elements will be in ascending order, according to natural order. Optionally, you can construct a TreeSet with a constructor that lets you give the collection your own rules for what the order should be (rather than relying on the ordering defined by the elements' class) by using a Comparable or Comparator.

Map Interface
A Map cares about unique identifiers. You map a unique key (the ID) to a specific value, where both the key and the value are, of course, objects. You're probably quite familiar with Maps since many languages support data structures that use a key/value or name/value pair. The Map implementations let you do things like search for a value based on the key, ask for a collection of just the values, or ask for a collection of just the keys. Like Sets, Maps rely on the equals() method to determine whether two keys are the same or different. HashMap The HashMap gives you an unsorted, unordered Map. When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the way to go; the other maps add a little more overhead. Where the keys land in the Map is based on the key's hashcode, so, like HashSet, the more efficient your hashCode() implementation, the better access performance you'll get. HashMap allows one null key and multiple null values in a collection.
So What Do You Do with a Collection? (Exam Objective 6.1)

When using HashSet or LinkedHashSet, the objects you add to them must override hashCode(). If they dont override hashCode(), the default Object. hashCode() method will allow multiple objects that you might consider "meaningfully equal" to be added to your "no duplicates allowed" set. Hashtable Like Vector, Hashtable has existed from prehistoric Java times. For fun, don't forget to note the naming inconsistency: HashMap vs. Hashtable. Where's the capitalization of t? Oh well, you won't be expected to spell it. Anyway, just as Vector is a synchronized counterpart to the sleeker, more modern ArrayList, Hashtable is the synchronized counterpart to HashMap. Remember that you don't synchronize a class, so when we say that Vector and Hashtable are synchronized, we just mean that the key methods of the class are synchronized. Another difference, though, is that while HashMap lets you have null values as well as one null key, a Hashtable doesn't let you have anything that's null. LinkedHashMap Like its Set counterpart, LinkedHashSet, the LinkedHashMap collection maintains insertion order (or, optionally, access order). Although it will be somewhat slower than HashMap for adding and removing elements, you can expect faster iteration with a LinkedHashMap. TreeMap You can probably guess by now that a TreeMap is a sorted Map. And you already know that by default, this means "sorted by the natural order of the elements." Like TreeSet, TreeMap lets you define a custom sort order (via a Comparable or Comparator) when you construct a TreeMap, that specifies how the elements should be compared to one another when they're being ordered.

Queue Interface
A Queue is designed to hold a list of "to-dos," or things to be processed in some way. Although other orders are possible, queues are typically thought of as FIFO (first-in, first-out). Queues support all of the standard Collection methods and they also add methods to add and subtract elements and review queue elements. PriorityQueue This class is new with Java 5. Since the LinkedList class has been enhanced to implement the Queue interface, basic queues can be handled with a LinkedList. The purpose of a PriorityQueue is to create a "priority-in, priority out"

queue as opposed to a typical FIFO queue. A PriorityQueue's elements are ordered either by natural ordering (in which case the elements that are sorted first will be accessed first) or according to a Comparator. In either case, the elements' ordering represents their relative priority. Table 7-2 summarizes the 11 of the 13 concrete collection-oriented classes you'll need to understand for the exam. (Arrays and Collections are coming right up!)
So What Do You Do with a Collection

You can easily eliminate some answers right away if you recognize that, for example, a Map cant be the class to choose when you need a name/value pair collection, since Map is an interface and not a concrete implementation class. The wording on the exam

Das könnte Ihnen auch gefallen