Problems encountered when C ++ is switched to Java

Source: Internet
Author: User

1. The default value cannot be set for the form parameter in Java


In the method, give the parameter some initial values or write them as classes and use different constructors.

For example:

Public void yourmethod (stringa, string B ){

String AA = "default ";

String BB = "dfadfa ";

If (A = NULL ){

A = AA;





You can also write the numbers and types of different parameters for multiple overloading, and call each other to satisfy the default parameters.
F (int I ){

F (I, 100 );



F (int I, Int J ){




2. the const does not exist in Java and is replaced by final.

The const parameter is replaced by the final parameter.


(1) Final defines constants in Java and can act on basic types or class types. if it acts on class types, such types cannot be inherited as parent classes, that is to say, there cannot be subclasses under it. Such classes are called atomic classes.
Const in C ++ defines constants.
(2) If final in Java is of the basic type, it is the same as C ++ const, but it is different for objects.
(3) Final indicates that the handle cannot be changed.
Final object OBJ = (object) New String ("");
OBJ = (object) New String ("hello"); is invalid
However, you can still call the OBJ method. For example, (string) OBJ). Length () is valid, and C ++ cannot call the object method if an object is defined as Const. Unless this method is defined as Const.

Package test;

/* Final indicates that the handle cannot be changed.
Final object OBJ = (object) New String ("");
OBJ = (object) New String ("hello"); is invalid
However, you can still call the OBJ method. For example, (string) OBJ). Length () is valid */

Public class test {

Public static void main (string [] ARGs ){

Final object OBJ = (object) New String ("");
// OBJ = (object) New String ("hello"); // the final state local variable OBJ cannot be assigned a value.
System. Out. println (string) OBJ). Length (); // you can still call the OBJ method.



Three final scenarios are used: data, methods, and classes.

(1). Data

1. Blank final

It refers to a field declared as final but not assigned an initial value. However, the final field must be assigned a value in each constructor.

2. Final Parameters

It means that you cannot change the object to which the parameter reference points in the method.

Class dog


Public void eat (){}


Public class animal


Void Do (final dog DG)


DG = new dog (); // Error



(2) Final Method

Prevent any inheritance class from modifying its meaning, so that it will not be overwritten.

The private method used in the class is implicitly specified without final. Because the private method cannot be used, it cannot be overwritten. Therefore, adding final to the private method is meaningless.

(3) final class

Indicates that you do not intend to inherit this class, which is equivalent to sealed in C.

Sealed modifier indicates sealed

When used for a class, it indicates that the class cannot be inherited and cannot be used with abstract at the same time, because these two modifiers are mutually exclusive in meaning.

When used for methods and attributes, it indicates that the method or attribute cannot be inherited and must be used together with the override keyword, because the method or attribute using the sealed modifier must be a virtual Member of the base class.

It is usually used to implement third-party class libraries that do not want to be inherited by the client, or for classes that do not need to be inherited to prevent hierarchy confusion caused by misuse of inheritance.

The proper use of the sealed modifier can also improve the running efficiency, because the inherited class will overwrite the member.


3. Transfer references

If I have two int-type variables A and B, I want to write a method to exchange their values. What should I do? There is no good way. At present, you only need to replace your integer basic type int with an integer of the outer covering class, or assemble it in the form of an array, and then use it to implement "Reference.


Conclusions about referenced parameters:

When the basic type and basic type variables are passed as parameters to the method, they are passed as values. In a method object, you cannot assign a value to the original variable or change its value.

When an object or referenced variable is passed as a parameter to a method, the original variable cannot be assigned a value in the method object (that is, the value of the real parameter cannot be changed ), however, you can change the property of the object to which it points.


That is, there is no parameter reference in Java.


4. Java does not support Operator Overloading and is replaced by the overload method.


5. There is no namespace in Java, and Java replaces the namespace with a package.


6 Java generics should pay attention to the following:

You cannot replace the basic type with the type parameter. Therefore, there is no pair <double> and only pair <double>. The reason is type erasure. After deletion, the pair class has an object-type domain, and the object cannot store the double value. The solution is to use the packaging class, or you can use an independent class and method for processing.


Java generics are only valid during compilation to ensure the type security during compilation. during runtime, all generic types are converted into objects. This is calledType Erasure.

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: 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.