Академический Документы
Профессиональный Документы
Культура Документы
technology. In 1992, the Green Project was spun off and its interest directed toward building highly interactive devices for the cable TV industry. This failed to materialize. In 1994, the focus of the original team was re-targeted, this time to the use of Internet technology. A small web browser called HotJava was written. Oak was renamed to Java after learning that Oak had already been trademarked. In 1995, Java was first publicly released. In 1996, Java Development Kit (JDK) 1.0 was released. In 2002, JDK 1.4 (codename Merlin) was released, the most widely used version. In 2004, JDK 5.0 (codename Tiger) was released, the latest version.
James Gosling
James Gosling is generally credited as the inventor of the Java programming language He was the first designer of Java and implemented its original compiler and virtual machine He is also known as the Father of Java He is currently the Chief Technical Officer of Sun Microsystems
What is Java? A multi-platform, network-centric, object-oriented programming language Multi-platform It can run on almost any computer platform Network-centric Designed with network in mind the network is the computer Designed for building applications for the Internet Object-oriented It incorporates object-oriented programming model
A Java Platform is the set of APIs, class libraries, and other programs used in developing Java programs for specific applications There are 3 Java Platform Editions 1. Java 2 Platform, Standard Edition (J2SE) Core Java Platform targeting applications running on workstations 2. Java 2 Platform, Enterprise Edition (J2EE) Component-based approach to developing distributed, multi-tier enterprise applications 3. Java 2 Platform, Micro Edition (J2ME) Targeted at small, stand-alone or connectable consumer and embedded devices
JDK JRE
MyProgram.java Java API Java Virtual Machine Hardware - Based Platform
Key Points
A Java source file can include package, import and class declarations in that order The main() method is the start of execution of a Java application Each Java statement is terminated by a semicolon ; Identifiers are case-sensitive Java keywords cannot be used as identifiers Each variable must be declared with a data type There are 8 primitive data types: boolean, char, byte, short, int, long, float and double There are 3 reference data types: class, array and interface
Use unary, arithmetic operators for basic mathematical operations Use string operator to concatenate strings Use relational operators to compare objects Use conditional operator as alternative to if-else() statement Use logical operators to compare boolean values Use assignment operators to assign values to variables Get familiar with object, shift and bitwise operators Java evaluates operators in order of precedence Casting is converting one data type to another
Key Points
if() and switch() are used for branching statements while(), do-while() and for() are used for iterating statements
Key Points
An array is a collection of values (either primitive or objects) all of which have the same data type There are 3 steps in creating an array:
Key Points
A method refers to what an object or class can do A method must have a return type, a name, and optional parameters The method signature refers to a method name and its parameters Return statement returns a value to its caller or returns control to its caller A method that does not return a value must specify void as a return type Calling a method should match its method signature When calling a method in the same class, use
What is an Exception?
An event during program execution that prevents the program from continuing normally An error condition that changes the normal flow of control in a program A signal that some unexpected
Types of Exceptions
Unchecked Exceptions
are usually irrecoverable and not handled explicitly are not checked by the compiler
are exceptions derived from Exception class excluding the RuntimeException class must be handled explicitly
Checked Exceptions
Key Points
Exceptional conditions sometimes occur in properly written code and must be handled Exception handling in Java is build around the throw-and-catch paradigm If a method cant handle the exception, it can propagate the exception to the call stack All exceptions are objects of Throwable class Only Checked exceptions must be handled explicitly
Defining a Class
A Class acts as the template from which an instance of an object is created. The class defines the properties of the object and the methods used to control the object's behavior. A Class specifies the structure of data as well as the methods which manipulate that data. Such data and methods are contained in each instance of the class. A Class is a model or template that can be
Defining an Object
Abstraction is the process of extracting common features from specific examples Abstraction is a process of defining the essential concepts while ignoring the inessential details
Defining Abstraction
Data Abstraction Programming languages define constructs to simplify the way information is presented to the programmer. Functional Abstraction Programming languages have constructs that gift wrap very complex and low level instructions into instructions that are much more readable. Object Abstraction OOP languages take the concept even further and abstract programming constructs as objects.
A Java Class denotes a category of objects, and acts as a blueprint for creating such objects. It defines its members referred to as fields and methods. The fields (also known as variables or attributes) refer to the properties of the class. The methods (also known as operations) refer to behaviors that the class exhibits.
Class Members
A class member refers to one of the fields or methods of a class. Static members are variables and methods belonging to a class where only a single copy of variables and methods are shared by each object. Instance members are variables and methods belonging to objects where a copy of each variable and method is created for each object instantiated.
Class Modifiers
Class modifiers change the way a class can be used. Access modifiers describe how a class can be accessed. Non-access modifiers describe how a class can be manipulated. Description
Modifier
(no modifier)
public abstract
final strictfp
Access Modifiers
private Private features of the Sample class can only be accessed from within the class itself. Class Package Sample Class default Only classes that are in the package may access default features of classes that are in the package
Class
Class
protected Classes that are in the package and all its subclasses may access protected features of the Sample class. Class Class
public All classes may access public features of the Sample class.
* Default is not a modifier; it is just the name of the access level if no access modifier is specified.
Accessibility Scope
Accessibility scope defines the boundary of access to a class and its members Scope Access
static
static code can access static members but not instance members non-static non-static code can access both static members and instance members package a class and its members can be accessed within the package they are declared class class members can be accessed within the class block local variables can be accessed only within a block
Encapsulation is the process of hiding an objects implementation from another object, while presenting only the interfaces that should be visible.
Encapsulating a Class
Members of a class must always be declared with the minimum level of visibility. Provide setters and getters (also known as accessors/mutators) to allow controlled access to private data. Provide other public methods (known as interfaces ) that other objects must adhere to in order to interact with the object.
Key Points
by extracting common properties of instances. A class is an abstraction of its instances. A Java Class denotes a category of objects. Class members refer to its fields and methods. Static members are variables and methods belonging to a class. Instance members are variables and methods belonging to objects. Instantiating a class means creating objects of its own type. Class modifiers include: (no modifier), public, abstract, final and strictfp. Member modifiers include: (no modifier), public, protected, private, static, final, abstract, strictfp, synchronized, native, transient and volatile.
Encapsulation hides implementation details of a class. Encapsulating a class means declaring members with minimum level of visibility. Setters are methods whose only function is to alter the state of an object in a controlled manner. Getters are methods which only function is to return information about the state of an object. Constructors are methods which set the initial state of an object upon creation of the object.
Defining Inheritance
existing ones. A derived class ("subclass") inherits the instance variables and methods of the base class ("superclass"), and may add new instance variables and methods. Inheritance defines a hierarchical relationship among classes wherein one class shares the attributes and methods defined in one or more classes. Inheritance is a relationship among classes in which one class shares the structure and behavior of another. A subclass inherits from a superclass.
Relationships of Inheritance is-a relationship a subclass can be used wherever a superclass can be used implemented in Java by extending a class has-a relationship a whole-class relationship between a class and its parts also known as composition or aggregation implemented in Java by instantiating an object inside a class has-a relationship is-a relationship Vehicle Engine Car Bus Car Wheel
It can be used to distinguish instance variables from local variables It can be assigned to other references, or passed as a parameter, or cast to other types It cannot be used in a static context super is a reference to the object of a superclass
Used to access hidden members of the superclass It cannot be assigned to other references, nor passed as a parameter, nor cast to other types It cannot be used in a static context
Casting
Casting is converting from one data type to another Implicit casting is an implied casting operation Explicit casting is a required casting operation Primitive casting is converting a primitive data type to
another data type
a narrower data type Reference casting is converting a reference data type to another Upcasting is conversion up the inheritance hierarchy Downcasting is conversion down the inheritance hierarchy Casting between primitive and reference type is not allowed In Java, casting is implemented using () operator
Key Points
Defining Polymorphism
respond to the same message in different ways. This means that different objects can have very different method implementations for the same message. Polymorphism is the ability of a new object to implement the base functionality of a parent object in a new way. Polymorphism is an object's ability to behave differently depending on its type. Polymorphism is the ability of objects belonging to different types to respond to methods of the same name, each one according to the appropriate typespecific behavior.
Method Overloading
Overloaded methods must change the argument list. Overloaded methods can change the return type.
2.
3.
4.
Method Overriding
The key benefit of overriding is the ability to define/defer behavior specific to subclasses. Which overridden method to call is based on object type and decided at runtime.
2.
3. 4.
5.
Overloading vs Overriding
Criteria Overloaded Method Overridden Method
Same
Same Cannot be wider Cannot be narrower Based on object type and decided at runtime
Argument Different list Return Can change type Exceptions Can change
Access level Can change
An Abstract Class is a class that provides common behavior across a set of subclasses, but is not itself designed to have instances of its own A class that dictates certain behavior but allows its subclasses to provide implementation A class designed only as a parent from which sub-classes may be derived, but which is not itself suitable for instantiation A class often used to "abstract out" incomplete sets of features which may then be shared by a group of sibling sub-classes which add different variations of the missing pieces
An Abstract Class cannot be instantiated An abstract class can SHOULD be extended An abstract class can have any number of abstract methods or none at all A class with at least one abstract method must be declared an abstract class A subclass can provide partial or full implementations of the inherited abstract methods A class that has at least one abstract method, whether declared or inherited from an abstract class, must be declared abstract.
Defining Interface
An Interface defines a contract by specifying a set of method prototypes for which each class that implements it must adhere An interface is 100% abstract class An interface provides only a form for a class but no implementation An interface defines what a class can do but not how the class will do it
Implementing Interface
Implementing an interface means providing implementations for its methods Interfaces are implemented using the implements keyword Rules on implementing the interface methods 1. Must have the same method signature and return type 2. Cannot narrow the method accessibility 3. Cannot specify broader checked exceptions Interface variables are implicitly public final static Interface methods are implicitly public abstract
A Final Class is considered complete, it cannot be improved or specialized A final class ensures that its state and behavior cannot be changed for safety and security To re-use a final class, you must utilize composition instead of inheritance
A final class cannot be extended or subclassed All methods of a final class have implementations
What is a Collection?
A Collection (also known as container) is an object that contains a group of objects treated as a single unit. Any type of objects can be stored, retrieved and manipulated as elements of collections.
Main Parts of Collections Framework Interfaces Core interfaces defining common functionality exhibited by collections Implementations Concrete classes of the core interfaces providing data structures Operations Methods that perform various operations on collections
Collections Hierarchy
Set
implements
List
extends
implements
HashSet
SortedSet
extends
LinkedHashSet
implements
TreeSet LinkedList Vector ArrayList
List
value
index Paul 0
Mark
1
John 2
Paul 3
Luke
4
Set
Paul Peter John Mark Luke Fred
Map
key
Pl Paul
Ma
Mark
Jn John
ul Paul
Le
Luke
value