Java notes: Classes and methods

Source: Internet
Author: User

First, object reference

Java does not allow pointers to be used instead of object references. An object reference can be understood as a pointer to an object, but cannot point to anywhere in memory like a real pointer, or manipulate an object reference like an action address. All types except the base type are objects, all objects are reference types, and object references are key to Java security.

class Solution {    publicstaticvoid  main (string[] args) {        Object obj; // declaring object references        New Object (); // instantiating an object     }}

Second, garbage collection

Java's garbage collection mechanism can automatically free up memory. When an object no longer has a reference, it is assumed that the object is no longer needed, and the Java virtual Opportunity automatically reclaims the memory occupied by that object. You do not need to explicitly free memory as you would in C + +. Like a C + + destructor, Java can override the object's Finalize method to show what to do when the object is disposed.

class Solution {    protectedvoid  Finalize () {        //finalization code     }}

Third, access rights

Access rights Similar Same package Sub-class Different packages
Public
Protected X
Default X X
Private X X X

Iv. parameter passing

class Solution {    publicstaticvoid change (String s) {        + = "Java";    }    publicstaticvoid  main (string[] args) {        = "Hello"  ;        Change (s);        System.out.println (s); // output Hello rather than Hello Java     }}

The string type does not change the original object when it changes, but instead instantiates a new object and changes the reference. Modifying a reference type in theory alters the value of the original object, but the conclusion does not apply to the string type. The modified string reference points to the new object, and the original object has not changed. Methods that need to implement modifying strings should use StringBuilder or StringBuffer.

Five, variable length parameters

classSolution { Public Static intSumint... nums) {        intres = 0;  for(inti:nums) Res+=i; returnRes; }     Public Static voidMain (string[] args) {intA = SUM (1, 2, 3, 4, 5);    System.out.println (a); }}

When you need to pass an indeterminate number of arguments, we choose to put the parameters in the array and pass them as parameters. But the steps to put an array into an array are tedious, and we want to pass an indeterminate number of variables directly as parameters. The variable-length parameter is actually an implicitly declared array. The variable-length parameter must be the last parameter and only one variable-length parameter can exist, declaring that the second variable-length parameter is illegal. Arguments for overloaded methods also do not allow ambiguity.

Java notes: Classes and methods

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.