Thinking in Java笔记 2

p.43 Special case: primitive types



high-precision numbers
Java includes two classes for performing high-precision arithmetic: BigIntegerand
BigDecimal. Although these approximately fit into the same category as the “wrapper”
classes, neither one has a primitive analogue.
Both classes have methods that provide analogues for the operations that you perform on
primitive types. That is, you can do anything with a BigInteger or BigDecimal that you
can with an int or float, it’s just that you must use method calls instead of operators. Also,
since there’s more involved, the operations will be slower. You’re exchanging speed for
accuracy.
BigInteger supports arbitrary-precision integers. This means that you can accurately
represent integral values of any size without losing any information during operations.
BigDecimal is for arbitrary-precision fixed-point numbers; you can use these for accurate
monetary calculations, for example



p.51 The static keyword

You can achieve both of these effects with the static keyword. When you say something is
static, it means that particular field or method is not tied to any particular object instance of
that class. So even if you’ve never created an object of that class you can call a static method
or access a static field. With ordinary, non-static fields and methods, you must create an
object and use that object to access the field or method, since non-static fields and methods
must know the particular object they are working with. 
Some object-oriented languages use the terms class data and class methods, meaning that
the data and methods exist only for the class as a whole, and not for any particular objects of
the class. Sometimes the Java literature uses these terms too.
To make a field or method static, you simply place the keyword before the definition. For
example, the following produces a static field and initializes it:
class StaticTest {
static int i = 47;
}
Now even if you make two StaticTest objects, there will still be only one piece of storage for
StaticTest.i. Both objects will share the same i. Consider:
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
                                                           
4 Of course, since static methods don’t need any objects to be created before they are used, they cannot directly access
non-static members or methods by simply calling those other members without referring to a named object (since non-
static members and methods must be tied to a particular object).
At this point, both st1.i and st2.i have the same value of 47 since they refer to the same piece
of memory.
There are two ways to refer to a static variable. As the preceding example indicates, you can
name it via an object, by saying, for example, st2.i. You can also refer to it directly through
its class name, something you cannot do with a non-static member.
StaticTest.i++;
The ++ operator adds one to the variable. At this point, both st1.i and st2.i will have the
value 48.
Using the class name is the preferred way to refer to a static variable. Not only does it
emphasize that variable’s static nature, but in some cases it gives the compiler better
opportunities for optimization.
Similar logic applies to static methods. You can refer to a static method either through an
object as you can with any method, or with the special additional syntax
ClassName.method( ). You define a static method in a similar way:
class Incrementable {
static void increment() { StaticTest.i++; }
}
You can see that the Incrementable method increment( ) increments the static data i
using the ++ operator. You can call increment( ) in the typical way, through an object:
Incrementable sf = new Incrementable();
sf.increment();
Or, because increment( ) is a static method, you can call it directly through its class:
Incrementable.increment();
Although static, when applied to a field, definitely changes the way the data is created (one
for each class versus the non-static one for each object), when applied to a method it’s not so
dramatic. An important use of static for methods is to allow you to call that method without
creating an object. This is essential, as you will see, in defining the main( ) method that is
the entry point for running an application.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值