Java基础重温(十一)-scjp 3

26. Figure 1-6 shows
that with a byte, for example, there are 256 possible numbers (or 28). Half of these
are negative, and half -1 are positive. The positive range is one less than the
negative range because the number zero is stored as a positive binary number. We
use the formula -2(bits-1) to calculate the negative range, and we use 2(bits-1)–1 for the
positive range.

27. The char type (a character) contains a single, 16-bit Unicode character.
Although the extended ASCII set known as ISO Latin-1 needs only 8 bits (256
different characters), a larger range is needed to represent characters found in
languages other than English. Unicode characters are actually represented by
unsigned 16-bit integers, which means 216 possible values, ranging from 0 to 65535
(216)-1

28. For the exam, you need to know that instance variables
■ Can use any of the four access levels (which means they can be marked with
any of the three access modifiers)
■ Can be marked final
■ Can be marked transient
■ Cannot be marked abstract
■ Cannot be marked synchronized
■ Cannot be marked strictfp
■ Cannot be marked native
■ Cannot be marked static, because then they'd become class variables.

29. Local (Automatic/Stack/Method) Variables
Local variables are variables declared within a method. That means the variable is
not just initialized within the method, but also declared within the method. Just
as the local variable starts its life inside the method, it's also destroyed when the
method has completed. Local variables are always on the stack, not the heap. (We'll
talk more about the stack and the heap in Chapter 3). Although the value of the
variable might be passed into, say, another method that then stores the value in an
instance variable, the variable itself lives only within the scope of the method.
Just don't forget that while the local variable is on the stack, if the variable is an
object reference, the object itself will still be created on the heap. There is no such
thing as a stack object, only a stack variable. You'll often hear programmers use
the phrase, "local object," but what they really mean is, "locally declared reference
variable." So if you hear a programmer use that expression, you'll know that he's just
too lazy to phrase it in a technically precise way. You can tell him we said that—
unless he knows where we live.

30. Local variable declarations can't use most of the modifiers that can be applied
to instance variables, such as public (or the other access modifiers), transient,
volatile, abstract, or static, but as we saw earlier, local variables can be
marked final

31. Typically, you'll initialize a local variable in the same line in which you declare
it, although you might still need to reinitialize it later in the method. The key is
to remember that a local variable must be initialized before you try to use it. The
compiler will reject any code that tries to use a local variable that hasn't been
assigned a value, because—unlike instance variables—local variables don't get
default values.

32. It is never legal to include the size of the array in your declaration.
Yes, we know you can do that in some other languages, which is why you might see a
question or two that include code similar to the following:
int[5] scores;
The preceding code won’t compile. Remember, the JVM doesn’t allocate space until
you actually instantiate the array object. That’s when size matters.

33. but what does it mean to have a final object reference variable? A reference variable
marked final can't ever be reassigned to refer to a different object. The data within
the object can be modified, but the reference variable cannot be changed. In other
words, a final reference still allows you to modify the state of the object it refers
to, but you can't modify the reference variable to make it refer to a different object.
Burn this in: there are no final objects, only final references. We'll explain this in
more detail in Chapter 3.

34. final variable cannot be assigned a new value, once the initial method is made (the initial assignment of a value must happen before the constructor completes).

35. Note: Every enum has a static method, values(), that returns an array of the enum's
values in the order they're declared.

36. Keep in mind that there are no final objects in Java. A reference variable
marked final can never be changed, but the object it refers to can be modified.

37.
a. Local (method, automatic, or stack) variable declarations cannot have
access modifiers.
b. final is the only modifier available to local variables.
c. Local variables don't get default values, so they must be initialized before use.

38. An enum declared outside a class must NOT be marked static, final,
abstract, protected, or private.

39. enum constructors can NEVER be invoked directly in code. They are always
called automatically when an enum is initialized.

40. The semicolon at the eand of an enum declaration is optional. These are legal:
enum Foo { ONE, TWO, THREE}
enum Foo { ONE, TWO, THREE};

41. In OO, the concept of IS-A is based on class inheritance or interface
implementation. IS-A is a way of saying, "this thing is a type of that thing." For
example, a Mustang is a type of horse, so in OO terms we can say, "Mustang IS-A
Horse." Subaru IS-A Car. Broccoli IS-A Vegetable (not a very fun one, but it still
counts). You express the IS-A relationship in Java through the keywords extends
(for class inheritance) and implements (for interface implementation).

42. HAS-A relationships are based on usage, rather than inheritance. In other words,
class A HAS-A B if code in class A has a reference to an instance of class B. For
example, you can say the following,
A Horse IS-A Animal. A Horse HAS-A Halter.
The code might look like this:
public class Animal { }
public class Horse extends Animal {
private Halter myHalter;
}

43. Polymorphic method invocations apply only to instance methods. You can
always refer to an object with a more general reference variable type (a superclass
or interface), but at runtime, the ONLY things that are dynamically
selected based on the actual object (rather than the reference type) are instance
methods. Not static methods. Not variables. Only overridden instance methods
are dynamically invoked based on the real object's type.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值