-
Similarities and differences between C++ and Java
1. Pointer ★★★★★
Pointers in C C++ provide great flexibility, but flexibility also brings dangers. Improper operation of pointers can easily cause problems such as memory leaks or dangling pointers.
Java cancels pointers. But in fact, the name of all reference data types declared in Java can be understood as a pointer. The name is stored in the stack memory and points to the space opened by new in the memory.
like:
int[] array = new int[10]
The integer array name array is on the stack memory, and a 10*4-byte space is opened on the heap memory, and array points to the block of memory.
Array can be understood as a pointer, and the address stored in it is the space created by new.
like:
class Person{
…
}
Person p = new Person();
The object name p is opened in the stack memory, and new is used to open up space in the heap memory for the object. The object name p points to the heap memory.
But in fact, the name is not like a pointer in C++, especially when passing parameters.
Java has stated that parameter passing is all by value.
But when the reference data type is used as a function parameter, when a declared object p1 is passed in, a copy of the object p2 is actually generated. This p2 points to p1, so when calling the members of p1 through p2, it can be completed. Modify, wait until the function call is completed, and retain the modification. like:
class Person{
public String name;
public int age ;
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
public class Test{
public static void main(String[] args){
Person p = new Person("Zhang San", 10);
System.out.println("Before modification-->Name: "+ p.name+", Age: "+p.age);
changePro(p); //Object p is passed in, and a copy of p is generated. Assume it is p1, which points to p. //Through this copy p1, members of p can be called.
System.out.println("After modification-->Name: "+ p.name+", Age: "+p.age);
}
public static void changePro(Person p){ //Members of the original object can be called through the copy
p.name = "李思";
p.age = 30;
}
}
result:
Before modification-->Name: Zhang San, age: 10
After modification-->Name: Li Si, age: 30
But when you pass p1 in, a copy of p2 is generated, and then trying to change the pointing of p1 through p2 is obviously impossible. At this time, only the pointing of p2 is changed. After the function call is completed, the pointing of p1 remains unchanged. like:
class Person{
public String name;
public int age ;
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
public class Test{
public static void main(String[] args){
Person p = new Person("Zhang San", 10);
System.out.println("Before modification-->Name: "+ p.name+", Age: "+p.age);
changeObj(p); //The object p is passed in, which is a copy of p. Assume it is p1, which points to p. //In the function, only the pointer of this copy is changed.
System.out.println("After modification-->Name: "+ p.name+", Age: "+p.age);
}
public static Person newP = new Person("李思", 30);
public static void changeObj(Person p){
p = newP; //Attempt to change the pointer, but what actually changes is the pointer of the copy.
//After the function ends, the pointing of the original object will not change
}
}
result:
Before modification-->Name: Zhang San, age: 10
After modification-->Name: Zhang San, age: 10
2. Dynamic memory allocation
In C++, new and delete are used to dynamically allocate and recycle memory. New is to open up space on the heap memory. After the memory is used, delete must be used manually to recycle it.
As long as a reference data type is declared in Java, new must be used to open up the memory space before use. But after the object dies, there is no need to manually reclaim the memory. Java's own memory recycling mechanism will automatically recycle garbage objects (the so-called garbage objects refer to the previously opened object memory, which is no longer referenced by the stack memory). Of course, manual recycling can also be performed through the System.gc() method.
3. Destructor
The function of the C++ destructor (no parameters, no return value) is to release the dynamically allocated memory space in the constructor, that is, to call (this call can be called through the object. destructor, or the system can automatically wait until the object lifetime ends. Call) destructor.
There is no destructor in Java, and garbage objects are automatically recycled through the garbage collection mechanism. However, you can achieve the same effect as the destructor in C++ by overriding the fanalize() method in the Object class. When the object is destroyed manually or automatically, the fanalize() method will be automatically called.
4. Contents in empty classes
The empty class of C++ must have 4 functions: default constructor, default destructor, and default copy constructor.
Java's empty classes include: default constructor, methods inherited from the Object class, such as
Default attributes in classes There are three types of member access permissions in C++ classes: public>protected>private. If not declared, the default permission is private.
There are four types of member access permissions in Java classes: public>protected>defalt>private. The default is default permission.
5. Implementation of member functions in classes
It is customary in C++. h The function is declared in the class of the header file; outside the class. To implement the function in the cpp file, #include the header file.
like:
//demo.h
Class Person{
Public:
Void fun(); //Declared in class
}
//demo.cpp
#include “demo.h”
Void Person::fun(){ //Outside-class implementation
. . . . //implementation body
}
Java is a declaration + implementation method in a class. If it is not implemented in a class, adding the abstract keyword is an abstract method.
like:
class Person{
Public void fun(){//Declaration + implementation in class
. . . . //implementation body
}
}
6. Instantiation of objects
class Person{
private:
int age;
public:
Person(){}
Person(int a){
age = a ;
}
void fun(){….}
}
. . . . //Start of main function
Person p1; //The constructor without parameters is called
Person p2(18); //Call the parameterized constructor
p1.fun(); //Call member function
p2.fun();
To instantiate an object in Java, you must use the new keyword.
class Person{
private String name;
private int age ;
public Person(){}
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void fun() {…..}
}
. . . . . //Start of main function
Person p1 = null ;
p1 = new Person(); //You must use the new keyword to open up memory space and call the parameterless constructor.
Person p2 = new Person("Zhang San", 18); //Call the parameterized constructor.
p1.fun(); //Call method
p2.fun();
7. This keyword
It is called this pointer in C++. When an object is instantiated, a this pointer will be generated by default to point to this object. It is used by the compiler to distinguish different objects of the same class. That is, as an object. When using a member function, you know which object it is through the this pointer, and call the member function to operate the member properties of the object.
This has three uses in Java:
1. Represents the attributes or methods in this class. Such as this. Method, this. property.
2. Represents the current object.
3. Call the constructor method of this class. Such as this(), this(parameter 1, parameter 2...).
[The functions of uses 1 and 2 are similar to the this pointer in C++. 】
8. Calling object members
C++ passes objects. Member function, or class pointer->member function to call.
In Java, you can only pass objects. Member function call.
The members of the Static attribute of the two can be directly passed through the class name. Member functions are called directly.
9. Subclass-->Parent class, the parameters passed by the constructor have something in common: If the constructor in the subclass does not clearly indicate which constructor of the parent class to call, the system defaults to calling the parameterless constructor of the parent class. At the same time, if the parent class defines a constructor with parameters, it is best to define a constructor without parameters.
class Person{
private:
int age;
public:
Person(){}
Person(int a){
age = a ;
}
}
class Student: public Person{
private:
int score ;
public:
Student(int a, int s):Person(a){ //Pass to parent class constructor
score = s;
}
}
class Person{
private String name;
private int age ;
public Person(){}
public Person(String name, int age){
this.name = name;
this.age = age;
}
}
class Student extends Person{
private int score;
public Student(String name, int age, int score){
super(name,age); //pass to parent class constructor
this.score = score;
}
}
10. Polymorphism
Polymorphism in C++ must be achieved by [virtual function or pure virtual function + subclass coverage of virtual function or pure virtual function].
Virtual functions are declared with virtual,
like:
virtual void fun(); //Declaration within class
void class name: fun() {….}//outside-class implementation
Java uses subclasses to override ordinary methods in ordinary parent classes, subclasses to override ordinary methods or abstract methods in abstract classes, and subclasses to override abstract methods in interfaces. +Transform upward.
Abstract methods are declared with abstract and have no content implementation.
like:
abstract void fun(); //No implementation within the class
11. Abstract classes Neither abstract class can instantiate objects. Pure virtual functions and abstract methods have similar concepts and similar functions.
It can also be said that there are abstract classes in C++, classes with pure virtual functions.
A pure virtual function is a virtual function with no content implementation and "=0", and cannot instantiate objects.
like:
virtual void fun() = 0; //Declared as =0 within the class, it is not implemented outside the class.
An abstract class in Java is a class declared with the abstract keyword and contains abstract methods. Object cannot be instantiated.
An interface in Java is a special class, or a special abstract class. It is composed of all static constants and abstract functions.
12. Access rights
C++ uses three inheritance methods to change the access rights of members between subclasses and parent classes.
class Student: public Person{
public:
. . . . . .
Private:
. . . . . .
};
class worker: protected Person{
public:
. . . . . .
Private:
. . . . . .
};
Class farmer: private Person{
public:
. . . . . .
Private:
. . . . . .
};
Java implements access permissions to members between different classes through the package mechanism.
Package org.tyut.a
class Person{
private…..
private……
public…….
public…
}
package org.tuyt.b
class Person{
private…..
private……
public…….
public…
}
package org.tuyt.c
class Person{
private…..
private……
public…….
public…
}
13. The idea of C++ preprocessing & java import package is the same: when you want to use a class other than the current class,
In C++, use the #include precompilation directive before the class definition to include the class library to be included.
Standard class libraries use angle brackets < > without h. Use double quotes "" with h in the custom class library, and it will be searched from the current path first.
like:
#include <iostream>
#include “demo.h”
In Java, to import the class you want to use, use the import command and indicate the package in which the class is located.
like:
imort java. Lang. *;
import org. tyut. *;