The difference between C + + and Java

Source: Internet
Author: User

The most significant difference between Java and C + + is in the processing of objects. In Java, an object variable is internally treated as a pointer. The Java literature indicates that object variables are referenced, but they are not identical to references in C + +. The so-called reference is a thing between a pointer and a variable.
---------is the difference between this essence, will be a lot of Java,ok code, and then C + + is really wrong.    The reference concept------Java and the C + + reference are not the same meaning. Java references are similar to C + + pointers.

    • In a C + + declaration, a class name can be used to declare a value for such an object. There is no way to do this in Java. Objects in Java are not values. In the Java declaration, a class name declares a reference to an object of this class. The equivalent of this in C + + is to use "*" to declare a pointer.
    • In C + +, "." The operator accesses an object as a left-hand parameter to access the member of the object. Because the object is not a value in Java, all objects are accessed by reference, which is just not possible in Java. In Java, the "." operator accesses a member of the object as a left-hand argument. In C + + This approach is equivalent to "--".

Example:
C + +: String a = Null--error error occurs because the default constructor for the string source code is not supported. If the string default constructor support, it is also possible (because he compiled is OK, stating that the syntax is not a problem, when running an error, that the constructor does not support it)

String default constructor Source code:
__clr_or_this_call basic_string (const _elem *_ptr)
: _mybase ()
{//construct from [_ptr, <null>]
_tidy ();
Assign (_PTR); Because _ptr is NULL, there's an error back here.
}


string* a =null---OK pointer can be empty
Java String a=null----OK java, an object variable is internally treated as a pointer

Reference documents:
Http://zh.wikipedia.org/wiki/Java%E5%92%8CC%2B%2B%E7%9A%84%E5%B0%8D%E7%85%A7
http://bbs.csdn.net/topics/190137939

The difference 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.