Integer a = 1000; Integer b = 1000; if (a == b) System.out.println("o.k.");
Explain how it is possible for the program state illustrated by your picture to come about.
JVM implementations are allowed to allocate separate instances for the same
values outside the range [-128..127]. If identical values are not collapsed
into the same instance, than identity (==
) does not have the same meaning
as equality. Page 212.
The answers, approximately verbatim, in no particular order, and with comments in italic:
Since the ==
operator only compares the location of a to
the location of b, the program will produce no output, unless both a and b
have the same location.1
1: True, but why did the if-statement guard evaluate to false?
Since both a and b are any reference types then the if-statement is only comparing the reference types and not the actual values that the are pointing to.2
Even though they appear to be equal, they point to different objects. The '==' operator compares the addresses of what a and b are point to not the values held in the values in the Integer instances.
The if statement if (a == b) evaluates false because the references “a” and “b” are not equal. Autounboxing does not occur because the JVM thinks you want to compare the references.
To compare the values you would need to write
if (a.intValue() == b.intValue()) {...}
Only for Integer values between -128 & 128 exclusive, do Integers map to the same location. For Integer values greater than or less than this range are Integer instances mapped to separate locations. The two different mappings cause the equivalency test to be false because Integer b and Integer a do not point to the same location.
a & b are objects that reference a place in memory. No output will happen when a & b reference different memory addresses. (The test (a == b) tests to see if the memory addresses are the same.)
The containing class is the class that contains the entity (class, method, or variable) or is the entity tagged with the access permission. The containing package is the package in that contains the containing class.
Page 192, although the list there is unordered.
The answers, approximately verbatim, in no particular order, and with comments in italic:
1) public — any one has access to them.1
32) protected — only the ancestor classes of the
methods3 can use them.
2) package — only those classes that are part of the
package has access.
4) private — only the class in which the methods reside
has access.
1: What are “them”?
2: Why aren’t these listed in order?
3: Methods can’t have ancestors; only classes can.
Public access — allows access to any piece of a program. If a method or variable is public, any part of the program can modify4 and call the method or variable.
Final5 — This key word does not allow any part of the program to change the reference is stored, however, the value6 can be modified.
Protected. This allows access only to the class that contains the method or variable, and its subclasses.7
Private. This limits access to the class itself, and does not permit access to any other classes.
4: public
doesn’t determine what happens when an entity is
accessed; it only determines if the entity can be accessed.
5: True, but final
doesn’t indicate whether or not an entity
can be accessed; it only indicates what can be done if access is allowed.
6: Do you mean the value (or instnace) referenced?
7: And also everything in the containing package.
public: any other class anywhere can have access to that
method.8
static:
private: when only that class has access to that particular method.
8: Or variable. Or class. Or other things we’ll cover eventually.
Static9 — can be accessed by any class in any
package and without an instance.
Public — can be accessed by any class in any package.
Protected — can be accessed by the class within the
package its in and any children of the class.
Private — can only be accessed by the class using
it.10
9: static
isn’t an access indicator; it’s a (more or
less) scope indicator.
10: By it
I’m assuming you mean the private
keyword as
opposed to the entity being tagged by the private keyword.
Public — | access | for | all | ||
Protected — | " | " | the | package | and subclasses |
default — | " | " | " | " | only |
Private — | " | " | none |
public — all classes11 can access fields of
this class.
protected — subclass can access field and other classes can
access methods of this class.12
private — classes methods and fields are not
accessible.13 Only accessed by subclasses when called with
super.method().14
no permission.15
11: All classes in any package, you mean.
12: But subclasses can’t access methods and other classes can’t access fields? Is that what you mean?
13: To which entities?
14: private
means private to the declaring class. An instance
calling super is necessarily from a child class and can’t access its
parent’s private entities.
15: Is this the default access permission?
This page last modified on 19 February 2009. |
|