Java and C + + comparisons (1)--virtual functions and pointers

Source: Internet
Author: User
Tags final functions garbage collection reference
c++| Comparison | function
1.java has a virtual function, but no pointers, that may have the problem, that Java is how to achieve polymorphism?
Java can be sure to have a virtual function, and if we do not declare the final form, he defaults to the virtual function. Do not vitual affirm.
"In-Java, you don't need to declare a as virtual." Dynamic binding is the default behavior. If you don't want a to be virtual, you tag it as final "--from <core java2:volum i>.
Not shown in Java and pointers in C + +, but he has the concept of an address. And all the "objects" we affirm are actually addresses (or handles), and assigning him to an internal deposit is a point of reference. Of course, we can also change his point of reference. In fact, we "object" assignment operations are all addresses in the east, The object is still unchanged. Then maybe someone would say that's not going to produce a lot of memory areas that don't have an "object" (handle). Yes, it will, but the Java garbage collection mechanism will give us the memory "leak". So we only care about the objects we're working on. Other objects we do not use for the funeral garbage collection will be for us to do.
Understand the "objects" in Java rather than the objects we understand in C + +. And actually the pointer (handle) that polymorphism can certainly be achieved.
The following example of an "object" assignment (reference) shows that all addresses are changing, not real memory units.
public class Number {
private int number;

Public number (int number) {
This.number = number;
}

public int GetNumber () {
return this.number;
}

public void Setnumber (int number) {
This.number = number;
}

public static void Main (string[] args) {
Number one = new number (1);
Number two = new number (2);
System.out.println ("beginning:");
System.out.println ("one =" + One.getnumber ());
System.out.println ("two =" + Two.getnumber ());

Assign two to one
two = one;//here two the value of this handle (that is, the address of an object) to the value of one of the handles. Thus two and one refers to the internal deposit (object) that was pointed to by the same//to the same point. and two The object that was previously pointed to is now an object that has no handle attached. may be garbage collected
System.out.println ("\nafter assigning two to one:");
System.out.println ("one =" + One.getnumber ());
System.out.println ("two =" + Two.getnumber ());

Change the value of two
Two.setnumber (3);//two and one point to the same unit, so he changes the unit, one can perceive.
SYSTEM.OUT.PRINTLN ("\nafter modifying Two:");
System.out.println ("one =" + One.getnumber ());
System.out.println ("two =" + Two.getnumber ());
}
}
Run Result:
Beginning:
one = 1
two = 2
After assigning two to one:
one = 1
two = 1
After modifying two:
one = 3
two = 3
Press any key to continue ...
Java Novice and Learning C + + A little feel, if there is something wrong, also hope to point out.

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.