java学习笔记(五)Defining Classes II

                      Chapter 5 Defining Classes II

5.1 Static Methods and Static Variables

(1)Static Methods

               java学习笔记(五)Defining Classes II

例如:

                              java学习笔记(五)Defining Classes II

                         java学习笔记(五)Defining Classes II

NB:(1)在静态的方法中,比如main中,调用静态的方法,可以直接使用class_name.method_name() 的形式,但是如果要调用非静态的方法在静态的方法中,那么需要创建一个该非静态方法的类的对象,然后使用该对象对函数进行调用即可。

(2)main函数的位置,可以在创建一个类,比如上文的RoundStuffDemo。也可以放在RoundStuff类中。

--------------------------------------------------------------------------------------------------------------------------

(2)Static Variables

A static variable is a variable that belongs to the class as a whole and not just to one object

However, with a static variable, there is only one copy of the variable, and all the objects can use this one variable.

Thus, a static variable can be used by objects to communicate between the objects.One object can change the static variable, and another object can read that change.

 语法:

                       java学习笔记(五)Defining Classes II

NB:

As we already noted, you cannot directly access an instance variable within the definition of a static method. However, it is perfectly legal to access a static variable within a static method, because a static variable belongs to the class as a whole.

                   java学习笔记(五)Defining Classes II

                     java学习笔记(五)Defining Classes II

输出:

                                            java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

5.2 The Math Class

The class  Math provides a number of standard mathematical methods. The class  Math is provided automatically and requires no  import statement. The Math class is in the java.lang package, so it requires no import statement

常见的函数:(比如使用Math.函数名:因为都是静态函数;如果想使用PI(π),也必须是用Math.PI)

     java学习笔记(五)Defining Classes II

     java学习笔记(五)Defining Classes II

     java学习笔记(五)Defining Classes II

     java学习笔记(五)Defining Classes II

       java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

Wrapper Classes

  Wrapper classes provide a class type corresponding to each of the primitive types so that you can have an object of a class type that behaves somewhat like its corresponding value of a primitive type

  To convert a value of a primitive type to an “equivalent” value of a class type, you create an object of the corresponding wrapper class using the primitive type value as an argument to the wrapper class constructor

  If you want to convert an  int value, such as  42 , to an object of type  Integer , you can do so as follows:
Integer integerObject = new Integer(42);

java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

Automatic Boxing and Unboxing

boxing: a primitive type(int)--> a object of(Integer)
比如:Integer numberOfSamuri = 47;    Double price = 499.99;  Character grade = 'A'; 即可

unboxing:a object of(Integer)--> a primitive type(int)

比如:Integer numberOfSamuri = new Integer(47);   int n = numberOfSamuri; <==> Integer numberOfSamuri = new Integer(47);
int n = numberOfSamuri.intValue();

NB:Java automatically applies the appropriate accessor method ( intValue ,  doubleValue , or  charValue in these cases) to obtain the value of the primitive type that is assigned to the variable

--------------------------------------------------------------------------------------------------------------------------

Character 常用的函数:

java学习笔记(五)Defining Classes II

java学习笔记(五)Defining Classes II

java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

5.2 References and Class Parameters

(1)Variables and Memory

A computer has two forms of memory called secondary memory and main memory.

The secondary memory is used to hold files for more or less permanent storage.

The main memory is used by the computer when it is running a program

Main memory consists of a long list of numbered locations called bytes, each containing eight bits; that is, eight 0/1 digits.

The number that identifies a byte is called its address

基本类型的变量和类的对象在存储上的不同?

对于基元类型的变量,变量的值存储在内存中。但是,类类型的变量只存储对象所在的内存地址。

The object named by the variable is stored in some other location in memory, and the variable contains only the memory address of where the object is stored.This memory address is called a reference (to the object). 

对于基本类型,比如int,存储的数字有大小的限制,因此占据的内容的大小也有限制,但是对于一个对象来说,因为他存储的不是具体的数字内容,而是这个数字内容的存储地址,这样一来,一个类的对象其实就没有了存储空间大小的限制。

比如:看一个类的对象的地址存储

java学习笔记(五)Defining Classes II

java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

(2) class parameters

当类的对象作为传参的对象的时,结果和传递数组相同。因为二者的存储地址都是一个引用地址,因此对传递参数值的改变,也相当于是改变了该对象或者数组中的值,即使不用return的时候。但是如果传递的是int类型的数据,那么必须有return的返回值,否则改变的只是函数中的local variable,调用该函数的参数没有发生改变。

                 java学习笔记(五)Defining Classes II

--------------------------------------------------------------------------------------------------------------------------

(3) The Constant null

                  java学习笔记(五)Defining Classes II

比如:

if (yourObject == null)
          System.out.println("No real object here.");

--------------------------------------------------------------------------------------------------------------------------

 

 

 

 

--------------------------------------------------------------------------------------------------------------------------

 

 

 

--------------------------------------------------------------------------------------------------------------------------

 

 

持续更新!!!! 8.29 17.30!

上一篇:Android 11 修改libcore Cipher AS测试


下一篇:Android ClassLoader介绍