References to Java

Source: Internet
Author: User

It is well known that the Java language style is similar to C and C + +, inheriting the core of the object-oriented technology of the C + + language, and removing pointers from the C + + language instead of using references (refrence). So why would you want to remove the pointer and use a reference instead of a reference and a pointer?

1. What is a pointer? The pointer is the soul of C, and the pointer (variable) is the variable that stores the address, which is essentially an abstraction of "indirection", that is, to find the desired address through a variable. Here is a simple example of a C language pointer:
#include <stdio.h> #include <stdlib.h>int main () {    int a = 3, b = 4;    Swap (&a, &b);    printf ("a =%d \ n", a);    printf ("B =%d", b);    return 0;} void swap (int *a, int *b) {    int temp = *a;    *a = *b;    *b = temp;}

 It is convenient and easy to use pointers for the underlying operation of variables, which makes the code very concise, but the pointers are too flexible, too powerful, and can easily cause memory leaks and generate off-the-end pointers. Programs written with pointers are also more prone to suppress a wide variety of errors, and the readability of the program is greatly compromised. So Java removes pointers and uses more secure and reliable references.

2. What is the reference? If the pointer is an abstraction of an address, that reference (refrence) is an abstraction of the identity. is not the same level of concept. A reference is an address that encapsulates an object, but it is still different from pointers, and using references allows Java programmers to manipulate objects as if they were basic data types, which are restricted, cannot address operations on references, and can only be assigned operations.
Object o;   Here is the handle created in the stack (Handle) o = new Object ();    Creates an object in the heap and points the handle to the object

  The reference or handle is equivalent to the remote control of the TV, the operation of the remote control, you can operate the TV, in fact, but we press the "small volume", we actually operate the remote control, and then the remote control operation of the TV, equivalent to directly operate the TV. When we walk around, just take the remote control, without having to hold the TV. References are remote controls that reference objects, and we cannot manipulate objects directly, but only by reference.

personally, a reference in Java is an abstraction of an object, a programmer using a reference is equivalent to using an object, and of course the reference interior certainly contains the address of the object. 3. The difference between a reference and a pointerC and C + +Pointers allow the user to quickly perform the underlying operation of the variable, which can be manipulated by pointers to access the variables pointed to by the pointer.the reference in Java is an abstraction of an object, and the Java Designer lets the user manipulate the object indirectly by means of a method operation that resembles the basic data type, and the programmer cannot get the referenced value and cannot manipulate the reference. Only assignment operations can be performed. in summary, references in Java can be thought of as restricted pointers, which cannot be determined by the pointer program or operate on pointers. 4. Java values pass Java's variable type to two types, base type and reference type. (1) parameter passing of the basic type: The parameter passing of the basic type is similar to the value passing in C. Examples are as follows:
Package com.cang.refrence; public class Testprimitive {public            static void Main (string[] args) {            int a = 3, b = 4;            System. Out.println ("Before swap the A and b:a =" + A + ", B =" + b);            Swap (A, b);           System. Out.println ("After swap the A and b:a =" + A + ", B =" + B);     }       public static void Swap (int a, int b) {            int temp = A;            A = b;            b = temp;     }}

  Operation Result:

Memory Abstraction: (2) parameter passing of a reference type
public class Testrefrence {public                 static void Main (string[] args) {                     stringbuffer buffer= new StringBuffer ("Colin ");                 Schange (buffer);                 System. OUT.PRINTLN (buffer);             }                          public static void Schange (StringBuffer str) {                 str= new StringBuffer ("Huang");             }      }

Results: Colin

Memory abstraction: As shown, when passing a reference, a copy of the reference is passed to the parameter of the method, but because the copy of the reference is assigned in the method, the copy of the reference points to the other object, not the original object.
public class TestRefrence2 {public         static void Main (string[] args) {                     stringbuffer buffer= new StringBuffer ("Coli n ");                 Schange (buffer);                 System. OUT.PRINTLN (buffer);             }                          public static void Schange (StringBuffer str) {           str.append ("Huang");         }            }

Results: Colinhuang

Memory abstraction: In summary, the Java method passes only the value pass, the basic data type pass is its value, and the reference type passes the reference variable. 

References to Java

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.