Java differs from C + + programming

Source: Internet
Author: User

1. Data types and variables

Variable types in C + + are similar to Java. Like Java, C + + has int and double types. However, the range of values for these numeric types is dependent on the machine. For example, on a 16-bit system, such as a PC running DOS or Windows 3.x, int is a double-byte (2-byte), and the value range is much smaller than that of the Java 4-byte Int. On these machines, if int is not enough, you need to use a long integer.

C + + has the short and unsigned types to more efficiently store numbers. (I think the so-called effective means higher space utilization.) It is best to avoid using these types unless the effectiveness of space use is really important to your system.

Boolean in C + + is represented in bool, not as Boolean in Java.

The string type in C + + is represented by a string. It's very similar to the string type in Java, but there are a few different ways to do this:

1.c++ strings store ASCII characters instead of standard code Unicode characters

The 2.c++ string can be modified, while the contents of the Java string are not modifiable (immutable).

3. The operation of the substring is called substr in C + +, and this command s.substr (i, N) obtains from the string s a substring from position I from the length of N.

4. In C + +, you can only concatenate strings with other string objects (concatenate) without being able to concatenate with any object.

5.c++ can be used to compare strings directly using the relational operator = =,!=, <, <=, >, >=, where four operators are compared in alphabetical order. This is much more convenient than using the function equals and CompareTo in Java.

2. Variables and Constants

In C + +, the definition of a local variable looks the same as in Java, for example:

int n = 5;

This is actually one of the important differences between C + + and Java. The C + + compiler does not initialize local variables, so it is easy to forget to initialize a variable in C + +, in which case the value of the variable has exactly the current random value in the memory area. This is obviously a very easy place to create a program error.

As with Java, classes in C + + can have data fields and static variables. The difference is that variables in C + + can be defined outside of a function or even a class, and these so-called global variables can be accessed in any function of the program, and thus are not easily managed properly. You should try to avoid using global variables in C + +.

In C + +, constants can be defined anywhere (remember that in Java, constants must be static data for a class). C + + uses the keyword const to define constants, which are final in Java. For example:

const int days_per_year = 365;

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.