The Java Tutorial (SUN)
A practical guide for programmers with hundreds of complete, working examples and dozens of trails - groups of lessons on a particular subject.
The URI of Learning Object Model-base(lom) Ontology is not more valid. May be this ontology has been moved to some where else. We will use this ontology instead until the new version of LOM is released or until we will have a relaunch with an another Learning Object ontology.
M. Campione
K. Wallrath
2003-06-03
Learning the Java Lecture
recursiveLearned
Object-Oriented Programming Concepts
recursiveLearned
What Is an Object?
An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life.
What Is a Class?
A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.
What Is Inheritance?
A class inherits state and behavior from its superclass. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs.
What Is an Interface?
A class inherits state and behavior from its superclass. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs.
What Is a Package?
A package is a namespace that organizes a set of related classes and interfaces.
Questions and Exercises: Object-Oriented Concepts
Test your understanding of objects, classes, messages, and so on by doing some exercises and answering some questions.
Language Basics
recursiveLearned
Variables
You use variables in your program to hold data. This section discusses data types, how to initialize variables, and how to refer to variables within blocks of code.
recursiveLearned
Data Types
Arrays
Summary of Variables
Questions and Exercises: Variables
Operators
This section details how you perform various operations, such as arithmetic and assignment operations.
recursiveLearned
Assignment, Arithmetic, and Unary Operators
Equality, Relational, and Conditional Operators
Bitwise and Bit Shift Operators
Summary of Operators
Questions and Exercises: Operators
Expressions, Statements, and Blocks
Questions and Exercises: Expressions, Statements, and Blocks
Control Flow Statements
recursiveLearned
The while and do-while Statements
The for Statement
The if/else Statements
The switch Statement
Branching Statements
Summary of Control Flow Statements
Questions and Exercises: Control Flow
Classes and Objects
recursiveLearned
Classes
Now that we have covered how to create and use objects, and how objects are cleaned up, it is time to show you how to write the classes from which objects are created. This section shows you the main components of a class through a small example that implements a last-in-first-out (LIFO) stack.
recursiveLearned
Passing Information to a Method or a Constructor
The declaration for a method or a constructor declares the number and the type of the arguments for that method or constructor.
Declaring Classes
The left side of the following diagram shows the possible components of a class declaration in the order they should or must appear in your class declaration. The right side describes their purposes. The required components are the class keyword and the class name and are shown in bold. All the other components are optional, and each appears on a line by itself (thus \"extends Super\" is a single component). Italics indicates an identifier such as the name of a class or interface. If you do not explicitly declare the optional items, the Java compiler assumes certain defaults: a nonpublic, nonabstract, nonfinal subclass of Object that implements no interfaces.
Providing Constructors for Your Classes
All Java classes have constructors that are used to initialize a new object of that type. A constructor has the same name as the class.
Declaring Member Variables
Defining Methods
Objects
A typical Java program creates many objects, which interact with one another by sending each other messages. Through these object interactions, a Java program can implement a GUI, run an animation, or send and receive information over a network. Once an object has completed the work for which it was created, it is garbage-collected and its resources are recycled for use by other objects.
recursiveLearned
Creating Objects
As you know, a class provides the blueprint for objects; you create an object from a class.
Using Objects
Once you have created an object, you probably want to use it for something. You may need information from it, want to change its state, or have it perform some action.
More on Classes
recursiveLearned
Returning a Value from a Method
You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value.
Using the this Keyword
Within an instance method or a constructor, this is a reference to the current object.
Initializing Fields
Controlling Access to Members of a Class
One of the benefits of classes is that classes can protect their member variables and methods from access by other objects.
Understanding Instance and Class Members
Summary of Creating and Using Objects
You create an object from a class by using the new operator and a constructor. The new operator returns a reference to the object that was created. You can assign the reference to a variable or use it directly.
Questions and Exercises: Creating and Using Objects
Questions and Exercises: Classes
Nested Classes
recursiveLearned
Inner Class Example
Summary of Nested Classes
Questions and Exercises: Nested Classes
Annotations
Enum Types
Interfaces and Inheritance
recursiveLearned
Interfaces
The Java programming language supports interfaces that you use to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy.
recursiveLearned
Defining an Interface
The figure below shows that an interface definition has two components: the interface declaration and the interface body. The interface declaration declares various attributes about the interface, such as its name and whether it extends other interfaces. The interface body contains the constant and the method declarations for that interface.
Implementing an Interface
An interface defines a protocol of behavior. A class that implements an interface adheres to the protocol defined by that interface. To declare a class that implements an interface, include an implements clause in the class declaration. Your class can implement more than one interface (the Java platform supports multiple inheritance for interfaces), so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class.
Using an Interface as a Type
When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name.
Rewriting Interfaces
Summary of Creating Interfaces
An interface defines a protocol of communication between two objects. An interface definition is comprised of a declaration and a body.
Questions and Exercises: Interfaces
Inheritance
class that is derived from another class is called a subclass (also a derived class, extended class, or child class).
recursiveLearned
Overriding and Hiding Methods
The ability of a subclass to override a method in its superclass allows a class to inherit from a superclass whose behavior is \"close enough\" and then override methods as needed.
Abstract Methods and Classes
Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated. Take, for example, food in the real world. Have you ever seen an instance of food? No. What you see instead are instances of carrot, apple, and (our favorite) chocolate. Food represents the abstract concept of things that we all can eat. It does nott make sense for an instance of food to exist.
Object as a Superclass
The Object class, in the java.lang package, sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class.
Hiding Fields
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different.
Using the Keyword super
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.
Writing Final Classes and Methods
You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses.
Summary of Inheritance
A class inherits fields and methods from all its superclasses, whether direct or indirect.
Questions and Exercises: Inheritance
Numbers and Strings
recursiveLearned
Strings
The Java platform contains three classes that you can use when working with character data.
recursiveLearned
The StringBuilder Class
StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.
Converting Between Numbers and Strings
It is often convenient or necessary to convert to/from Numbers.
Comparing Strings and Portions of Strings
The String class has a number of methods for comparing strings and portions of strings.
Manipulating Characters in a String
You can get the character at a particular index within a string by invoking the charAt() accessor method.
Summary of Strings
Questions and Exercises: Strings
Numbers
This section begins with a discussion of the Number and its subclasses. In particular, this section talks about why you might need these classes, points out the common methods and class variables they have in common, and shows you how to convert instances to strings.
recursiveLearned
The Number Classes
You use an instance of one of the Number classes-Byte, Double, Float, Integer, Long, and Short-to contain a number of primitive type. You can also use BigInteger and BigDecimal for arbitrary-precision numbers.
Formatting Numeric Print Output
Earlier you saw the use of the print() and println() methods for printing strings to standard output (System.out). Since all numbers can be converted to strings (as you will see later in this lesson), you can use these methods to print out an arbitrary mixture of strings and numbers.
Beyond Basic Arithmetic
Summary of Numbers
You use an instance of one of the Number classes-Byte, Double, Float, Integer, Long, and Short-to contain a number of primitive type. You can also use BigInteger and BigDecimal for arbitrary-precision numbers.
Questions and Exercises: Numbers
Characters
The Java compiler also create a Character object for you under some circumstances.
Generics
recursiveLearned
Introduction
Generics add stability to your code by making more of your bugs detectable at compile time. Some programmers choose to learn generics by studying the Java Collections Framework.
Generic Types
Generic Methods and Constructors
Type parameters can also be declared within method and constructor signatures to create generic methods and generic constructors.
Bounded Type Parameters
There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.
Subtyping
Wildcards
In generics, an unknown type is represented by the wildcard character \"?\".
Type Erasure
When a generic type is instantiated, the compiler translates those types by a technique called type erasure.
Summary of Generics
Questions and Exercises: Generics
Packages
To make classes easier to find and to use, to avoid naming conflicts, and to control access, programmers bundle groups of related classes and interfaces into packages.
recursiveLearned
Creating and Using Packages
A package is a grouping of related types providing access protection and name space management.
Managing Source and Class Files
Many implementations of the Java platform rely on hierarchical file systems to manage source and class files, although The Java Language Specification does not require this.
Creating a Package
To create a package, put a type (class, interface, enum, or annotation) in it. To do this, put a package statement at the top of the source file in which the type is defined.
Naming a Package
With programmers worldwide writing classes, interfaces, enums, and annotations using the Java programming language, it is likely that two programmers will use the same name for two different classes.
Using Package Members
Only public package members are accessible outside the package in which they are defined. To use a public package member from outside its package.
Summary of Creating and Using Packages
Questions and Exercises: Creating and Using Packages