Generic Bounded Type Parameters
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. To declare […]
Generic Methods and Constructors
Generic Methods and Constructors Type parameters can also be declared within method and constructor signatures to create generic methods and generic constructors. This is similar to declaring a generic type, but the type parameter’s scope is limited to the method or constructor in which it’s declared. /** * This version introduces a generic method. */ […]
Generic Types
Generic Types Let’s update our Box class to use generics. We’ll first create a generic type declaration by changing the code “public class Box” to “public class Box<T>“; this introduces one type variable, named T, that can be used anywhere inside the class. This same technique can be applied to interfaces as well. There’s nothing […]
Generics Introduction
Introduction In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they’ll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity. Fortunately, […]
Numbers and Strings
Numbers and Strings Numbers This section begins with a discussion of the Number class (in the java.lang package) and its subclasses. In particular, this section talks about the situations where you would use instantiations of these classes rather than the primitive data types. Additionally, this section talks about other classes you might need to work […]
Strings
Strings Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings. Creating StringsThe most direct way to create a string is to write: String greeting = “Hello world!”; In this case, “Hello […]
Numbers
Numbers This section begins with a discussion of the Number class in the java.lang package, its subclasses, and the situations where you would use instantiations of these classes rather than the primitive number types. This section also presents the PrintStream and DecimalFormat classes, which provide methods for writing formatted numerical output. Finally, the Math class […]
Interfaces and Inheritance
Interfaces and Inheritance Interfaces You saw an example of implementing an interface in the previous lesson. You can read more about interfaces here—what they are for, why you might want to write one, and how to write one. Inheritance This section describes the way in which you can derive one class from another. That is, […]
Inheritance
Inheritance In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes. Definitions: A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class […]
Interfaces
Interfaces There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are […]