A few summaries of the differences between C + + and Java

Source: Internet
Author: User

Before C + +, recently java a bit confusing, summed up:

1. When writing Java programs, you will find that if you write your own class a{... }, this time you have to instantiate, you must be new, but in C + + is not used, C + + you can directly declare A;

Why:

A.java all the variables are references (except the basic type), the reference is the equivalent of a pointer, so you must be new, otherwise you can not use, of course, in C + +, the pointer may be light declaration, throw there is not new, when you use the new, the Java compiler in the security of the optimization.

B.c++ inside, a A; When run, it automatically calls the default constructor, constructs one for you, this is an instance object, not a reference, the field in a is a value, but the value may be something you don't want.

2. With regard to the implementation mechanism of RTTI, Java is different from C + +, but the principle is the same :

A.java If you implement Rtti, it's easy to create a class file for each class in Java. For example, there is now a base class shape whose subclasses have Circle,square and so on. There is a vector i, in Java, if the vector is not generalized, it can be placed into any object. Then you can put the circle,square in, upcasting. When taken out, you need to downcasting back to shape, such as (shape) i[0], this time in Java will be rtti, but this form of parentheses, in C + + will not be rtti, will only be cast to () type . The Rtti how to do it, because each class is a class, loaded into memory, the bytecode is different, so it is convenient to Rtti.

In the b.c++, there are also rtti in dynamic_cast and typeID. It is easy to speculate that to carry out rtti need to know the class information, in Java has class record type information, in fact, in C + + also has type_info, (personally think that these two things, essentially the same), but very few people use. Therefore, in C + +, the Rtti is based on Type_info.

C. Other wood to see, only to see the dynamic_cast operation Principle: Dynamic_cast is a type conversion, upcast better understand, but downcast, how to operate here? When a virtual function exists in a class, the compiler adds a vptr pointer to the virtual function table in the class's member variable , and the Type_info object associated with each class is also referred to by virtual table, usually this type_ The Info object is placed in the first slot of the table. When we do dynamic_cast, the compiler will help us with the grammar check. If the static type of the pointer is the same as the target type, then nothing is done; otherwise, the pointer is first adjusted so that it points to the vftable and passes it to the inner function with the adjusted pointer, adjusted offset, static type, and target type. The last parameter indicates whether the conversion is a pointer or a reference. The only difference between the two is that if the conversion fails, the former returns null, which throws the Bad_cast exception.

A few summaries of the differences between C + + and 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.