Java Learning (3)

Source: Internet
Author: User

Accumulation of classes:

When writing classes, security considerations must be made to ensure that the elements in the class are safely used and read, and must conform to the following three principles:

The variables in all domains must be private, the domain access method of public must be set in the class, and the required domain modification methods should be set in the class:

e.g.

 Public classtest{PrivateString name; Private intAge ;  PublicTest (String N,inta) {       This. Name =N;  This. Age =A; }       PublicString GetName () {returnname; }    PublicString getage () {returnAge ; }    Public voidSetage (intA1) {       This. Age =A1; }   }

It is important to note that in the get process, note the value returned when it is a non-basic data type or an immutable class

(

Variable: Byte, short, int, long, double, char, Boolean.

Immutable: Boolean, Byte, Character, Double, Float, Integer, Long, short, String.

)

, it is important to note that references in the assignment process are copied, so it is possible to inadvertently affect other objects that are also referenced or used to the value of the variable during the modification of the value. You need to use the Clone () method. (JAVA2 core technology P107).

Immutable classes: Each method in a class does not change the class of its object, and once created cannot be changed. For example, String. You can refer to the following blog

As the example above, although the change from the outside is the value of D, but because Harry's working life also points to D point to the data type of object, so while modifying the value of D also inexplicably changed Harry's work, this error is very difficult to find, Therefore, in the return method, you need to determine whether the returned value is not a type other than object.

Implicit and realistic parameters in the method:

An implicit parameter is a parameter that appears in a method and is passed in, but does not appear in the method parameter list, usually preceded by a "this."

e.g.

private int Test (int a) {

int tempt = 0;

return tempt = this.b + A;

}

It's easy to see that B is also an incoming parameter, but it doesn't appear in the argument list, so a is an explicit parameter and B is an implicit argument.

The Java method takes the value to pass, the non-argument passes, therefore passes only one copy, but the Java method itself cannot modify the value of the parameter passed in, note that the parameter here is worth the basic data type, but the exception for the object's parameters, the following code can explain:

1  Public classParainputpara {2      Public Static voidMain (String args[]) {3Para p =NewPara (10);4System.out.println ("Before para Change the value of L:" +P.L);5 Getpara (p);6SYSTEM.OUT.PRINTLN ("After para change the value of L:" +P.L);7         intITest = 10;8System.out.println ("Before iTest change the value of L:" +iTest);9 changefor (iTest);TenSystem.out.println ("After iTest change the value of L:" +iTest); One     } A     Private Static voidGetpara (Para p) { -P.parachange (10); -     } the     Private Static voidChangefor (inti) { -i+=10; -     }  - } +  - classpara{ +     intl; A      PublicPara (inty) { at          This. L =y; -     } -      -      Public voidParachange (intx) { -l+=x; -     } in}

Operation Result:

1 before para change the value of L:102 after para change the value of L:203 before iTest C Hange the value of L:104 After iTest change the value of L:10

In summary, except for the underlying type in Java, the other is to pass a reference (that is, the memory address), not the object's clone, so in addition to the underlying type and the immutable type, when the reference is passed to the specified method, the method modifies the referenced object when the reference is modified in disguise.

Also, for an immutable type, a reference is passed, such as String A, when a + "B" is executed, a new object is created and a reference to the object is reassigned, and the old A is recycled when the collection is triggered. Therefore, in terms of performance, the simple string + string will not stringbuffer the Append method of high performance.

In cases where the automatic recovery mechanism of Java does not meet the requirements, in order to better satisfy the release of the resource, it should provide close () or Dispose (), such as InputStream, for all the methods that appear in the code that need to be freed.

Java must first write the default constructor when creating the class, which is a good habit, because if you do not write the default constructor, and you also define a non-default constructor, so in subsequent calls, there may be a variety of problems.

Java Learning (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.