Sunteți pe pagina 1din 36

TWO TRUTHS & A LIE

Learning About Floating-Point Data Types


1. Java supports two floating-point data types: float and double. The double data type
requires more memory and can hold more significant digits.
2. A floating-point constant, such as 5.6, is a float by default.
3. As with integers, you can perform the mathematical operations of addition,
subtraction, multiplication, and division with floating-point numbers.
TWO TRUTHS & A LIE

Learning About Floating-Point Data Types


1. Java supports two floating-point data types: float and double. The double data type
requires more memory and can hold more significant digits.
2. A floating-point constant, such as 5.6, is a float by default.
3. As with integers, you can perform the mathematical operations of addition,
subtraction, multiplication, and division with floating-point numbers.
TWO TRUTHS & A LIE

Understanding Type Conversion


1. When you perform arithmetic operations with operands of unlike types, you must
make an explicit conversion to a unifying type.
2. Summing a double, int, and float results in a double.
3. You can explicitly override the unifying type imposed by Java by performing a type
cast; type casting forces a value of one data type to be used as a value of another type
TWO TRUTHS & A LIE

Understanding Method Calls and Placement


1. Any class can contain an unlimited number of methods.
2. During one program execution, a method might be called any number of times.
3. A method is usually written within another method
TWO TRUTHS & A LIE

Understanding Method Construction


1. A method header is also called an implementation.
2. When a method is declared with public access, methods in other classes can call it.
3. Not all methods return a value, but every method requires a return type.
TWO TRUTHS & A LIE

Adding Parameters to Methods


1. A class can contain any number of methods, and each method can be called any
number of times.
2. Arguments are used in method calls; they are passed to parameters in method
headers.
3. A method header always contains a return type, an identifier, and a parameter list
within parentheses.
TWO TRUTHS & A LIE

Creating Methods That Return Values


1. The return type for a method can be any type used in Java, including int, double,
and void.
2. A method’s declared return type must match the type of the value used in the
parameter list.
3. You cannot place a method within another method, but you can call a method from
within another method.
TWO TRUTHS & A LIE

Learning About Classes and Objects


1. A class is an instantiation of many objects.
2. Objects gain their attributes and methods from their classes.
3. An application or class that instantiates objects of another prewritten class is a class
client.
TWO TRUTHS & A LIE

Creating a Class
1. A class header contains an optional access specifier, the keyword class, and an
identifier.
2. When you instantiate objects, each has its own copy of each static data field in the
class.
3. Most fields in a class are private, and most methods are public.
TWO TRUTHS & A LIE

Creating Instance Methods in a Class


1. The keyword static is used with class wide methods, but not for methods that
“belong” to objects.
2. When you create a class from which objects will be instantiated, most methods are
non-static because they are associated with individual objects.
3. Static methods are instance methods.
TWO TRUTHS & A LIE

Declaring Objects and Using Their Methods


1. When you declare an object, you give it a name and set aside enough memory for
the object to be stored.
2. An object name is a reference; it holds a memory address.
3. When you don’t write a constructor for a class, Java creates one for you; the name
of the constructor is always the same as the name of its class.
TWO TRUTHS & A LIE

An Introduction to Using Constructors


1. In Java, you cannot write a default constructor; it must be supplied for you
automatically.
2. The automatically supplied default constructor sets all numeric fields to 0, character
fields to Unicode ‘\u0000’, Boolean fields to false, and fields that are object references
to null.
3. When you write a constructor, it must have the same name as the class it constructs,
and it cannot have a return type.
TWO TRUTHS & A LIE

Understanding That Classes Are Data Types


1. When you declare a primitive variable or instantiate an object from a class, you
provide both a type and an identifier.
2. Unlike a primitive variable, an instantiated object cannot be passed into or returned
from a method.
3. The address of an instantiated object can be assigned to a declared reference of the
same type.
TWO TRUTHS & A LIE

Understanding Blocks and Scope


1. A variable ceases to exist, or goes out of scope, at the end of the block in which it is
declared.
2. You cannot declare the same variable name more than once within a block, even if a
block contains other blocks.
3. A class’s instance variables override locally declared variables with the same names
that are declared within the class’s methods.
TWO TRUTHS & A LIE

Creating and Calling Constructors with Parameters


1. A default constructor is one that is automatically created.
2. When you write a constructor, it can be written to receive parameters or not.
3. If a class’s only constructor requires an argument, you must provide an argument for
every object of the class that you create.
TWO TRUTHS & A LIE

Learning About the this Reference


1. Usually, you want each instantiation of a class to have its own non-static data fields,
but each object does not need its own copy of most methods.
2. When you use a non-static method, the compiler accesses the correct object’s field
because you implicitly pass an object reference to the method.
3. The this reference is supplied automatically in classes; you cannot use it explicitly.
TWO TRUTHS & A LIE

Using static Fields


1. Methods declared as static receive a this reference that contains a reference to the
object associated with them.
2. Methods declared as static are called class methods.
3. A final static field’s value is shared by every object of a class.
TWO TRUTHS & A LIE

Using Two-Dimensional and Other Multidimensional Arrays


1. Two-dimensional arrays have both rows and columns, so you must use two
subscripts when you access an element in a two-dimensional array.
2. The following array contains two columns and three rows:
int[][] myArray = {{12, 14, 19}, {33, 45, 88}};
3. With a two-dimensional array, the length field holds the number of rows in the array;
each row has a length field that holds the number of columns in the row.
TWO TRUTHS & A LIE

Using Variable Subscripts with an Array


1. When an application contains an array, it is common to perform loops that vary the
loop control variable from 0 to one less than the size of the array.
2. An array’s length field contains the highest value that can be used as the array’s
subscript.
3. The enhanced for loop allows you to cycle through an array without specifying the
starting and ending points for the loop control variable.
TWO TRUTHS & A LIE

Initializing an Array
1. When you declare int[] idNums = new int[35];, each element of the array has a value
of 0.
2. When you declare double[] salaries = new double[10];, each element of the array
has a value of 0.0.
3. When you declare int[] scores = {100, 90, 80};, the first three elements of the array
are assigned the values listed, but all the remaining elements are assigned 0
TWO TRUTHS & A LIE

Declaring Arrays
1. The statement int[] idNums = new int[35]; reserves enough memory for exactly 34
integers.
2. The first element in any array has a subscript of 0, no matter what data type is
stored.
3. In Java, you can use a variable as well as a constant to declare an array’s size.
Automatically Imported,
Prewritten Constants and
Methods
Learning About the this Reference
1. Usually, you want each instantiation of a class to have its own non-static data fields,
but each object does not need its own copy of most methods.
2. When you use a non-static method, the compiler accesses the correct object’s field
because you implicitly pass an object reference to the method.
3. The this reference is supplied automatically in classes; you cannot use it explicitly.
TWO TRUTHS & A LIE

Understanding Blocks and Scope


1. A variable ceases to exist, or goes out of scope, at the end of the block in which it is
declared.
2. You cannot declare the same variable name more than once within a block, even if a
block contains other blocks.
3. A class’s instance variables override locally declared variables with the same names
that are declared within the class’s methods.
TWO TRUTHS & A LIE

Learning About Ambiguity


1. When it is part of the same program as void myMethod(int age, String name), the
following method would be ambiguous:
void myMethod(String name, int age)
2. When it is part of the same program as void myMethod(int age, String name), the
following method would be ambiguous:
String myMethod(int zipCode, String address)
3. When it is part of the same program as void myMethod(int age, String name), the
following method would be ambiguous:
void myMethod(int x, String y)
TWO TRUTHS & A LIE

Creating and Calling Constructors with Parameters


1. A default constructor is one that is automatically created.
2. When you write a constructor, it can be written to receive parameters or not.
3. If a class’s only constructor requires an argument, you must provide an argument for
every object of the class that you create
TWO TRUTHS & A LIE

Learning About the this Reference


1. Usually, you want each instantiation of a class to have its own non-static data fields,
but each object does not need its own copy of most methods.
2. When you use a non-static method, the compiler accesses the correct object’s field
because you implicitly pass an object reference to the method.
3. The this reference is supplied automatically in classes; you cannot use it Explicitly.
TWO TRUTHS & A LIE

Using static Fields


1. Methods declared as static receive a this reference that contains a reference to the
object associated with them.
2. Methods declared as static are called class methods.
3. A final static field’s value is shared by every object of a class
TWO TRUTHS & A LIE

Understanding Composition and Nested Classes


1. Composition describes the relationship between classes when an object of one class
is a data field within another class.
2. When you use an object as a data member of another object, you must remember to
supply values for the contained object if it has no default constructor.
3. A nested class resides within another class.
TWO TRUTHS & A LIE

Learning About the StringBuilder and StringBuffer Classes


1. When you create a String, you have the option of omitting the keyword new, but when
you initialize a StringBuilder object, you must use the keyword new, the constructor
name, and an initializing value between the constructor’s parentheses.
2. When you create a StringBuilder object with an initial value of “Juan”, its capacity is
16.
3. If a StringBuilder named myAddress contains “817”, then myAddress.append(" Maple
Lane"); alters myAddress to contain “817 Maple Lane”
TWO TRUTHS & A LIE

Initializing an Array
1. When you declare int[] idNums = new int[35];, each element of the array has a value
of 0.
2. When you declare double[] salaries = new double[10];, each element of the array has
a value of 0.0.
3. When you declare int[] scores = {100, 90, 80};, the first three elements of the array
are assigned the values listed, but all the remaining elements are assigned 0.
TWO TRUTHS & A LIE

Using the Arrays Class

1. The Arrays class contains methods for manipulating arrays, such as binarySearch(),
fill(), and sort().
2. You can use the Arrays class binarySearch() method successfully on any array as
soon as you have assigned values to the array elements.
3. The binarySearch() method works by continuously deciding whether the element
sought is above or below the halfway point in sublists of the original list.
TWO TRUTHS & A LIE

Extending Classes
1. You use the keyword inherits to achieve inheritance in Java.
2. A derived class has access to all its parents’ nonprivate methods.
3. Subclasses are more specific than the superclass they extend
TWO TRUTHS & A LIE

Overriding Superclass Methods

1. Any child class object has all the attributes of its parent, but all of those attributes
might not be directly accessible.
2. You override a parent class method by creating a child class method with the same
identifier but a different parameter list or return type.
3. When a child class method overrides a parent class method, and you use the
method name with a child class object, the child class method version executes.
TWO TRUTHS & A LIE

Calling Constructors During Inheritance


1. When you create any subclass object, the subclass constructor executes first, and
then the superclass constructor executes.
2. When constructors initialize variables, you usually want the superclass constructor
to initialize the data fields that originate in the superclass and the subclass constructor
to initialize the data fields that are specific to the
subclass.
3. When a superclass contains only nondefault constructors, you must include at least
one constructor for each subclass you create.
TWO TRUTHS & A LIE

Accessing Superclass Methods

1. You can use the keyword this from within a derived class method to access an
overridden base class method.
2. You can use the keyword super from within a derived class method to access an
overridden base class method.
3. You can use the keyword super from within a derived class method to access a base
class method that has not been overridden.
TWO TRUTHS & A LIE

Methods You Cannot Override


1. A subclass cannot override methods that are declared static in the superclass.
2. A subclass cannot override methods that are declared final in the superclass.
3. A subclass cannot override methods that are declared private in the
superclass

S-ar putea să vă placă și