Differences between Java and C ++ programming (1)

Source: Internet
Author: User

1. Data Types and variables

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

C ++ has the short and unsigned types to store numbers more effectively. (Effective means higher space utilization .) It is best to avoid using these types unless the validity of space utilization is really important to your system.

In C ++, boolean is represented by bool, rather than boolean in Java.

The string type in C ++ is represented by string. It is very similar to the String type in Java, but the following differences must be made one by one:

1. C ++ strings store ASCII characters instead of standard Unicode characters

2. The C ++ string can be modified, while the Java string content cannot be modified (immutable ).

3. the sub-string operation is called substr in C ++. The command s. substr (I, n) obtains the substring from string s with the length of n starting from position I.

4. In C ++, you can only concatenate a string with other string objects without concatenating any objects.

5. In C ++, you can directly use the Relational operators = ,! =, <, <=,>,> = To compare strings. The following four operators are compared alphabetically. This is much easier than using the equals and compareTo functions in Java.

2. Variables and constants

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

int n = 5;

In fact, this is an important difference between C ++ and Java. The C ++ compiler does not perform initialization checks on local variables, so it is easy to forget to initialize a variable in C ++. In this case, variable value the variable occupies the memory area and there is a random value currently. This is obviously a cause of program errors.

Like Java, C ++ classes can have data domains and static variables. The difference is that variables in C ++ can be defined outside functions or even classes. These so-called global variables can be accessed in any function of the program, therefore, it is difficult to be well managed. Avoid using global variables in C ++.

In C ++, constants can be defined anywhere (remember that constants must be static data of classes in Java ). C ++ uses the keyword const to define constants, while Java uses final. For example:

const int DAYS_PER_YEAR = 365;

3. Class C ++

The definition of classes is somewhat different from that of Java. Here is an example: A Point class of C ++ version:

class Point /* C++ */{public:Point();Point(double xval, double yval);void move(double dx, double dy);double getX() const;double getY() const;private:double x;double y;};

The important differences are as follows:

1. The class definition of C ++ is divided into public and private sections, starting with the public and private keywords respectively. In Java, each element must be public or private;

2. The class definition in C ++ only contains the declaration of the function, and the actual implementation is listed separately;

3. Access Function (accessor methods) is marked with the keyword const, indicating that this function will not change the element value of this object;

4. The class definition ends with a semicolon.

The implementation of functions in the class follows the definition of the class. Because the function is defined outside the class, you must prefix the class name before each function name and use the double Colon: operator to separate the class name and function name. The access function of the implicit parameter value (that is, the value of the current object) is marked with const. The following shows the implementation of functions in the above class definition:

Point::Point() { x = 0; y = 0; }void Point::move(double dx, double dy){x = x + dx;y = y + dy;}double Point::getX() const{return x;}


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.