Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie
Discuss newsgroups and documents
Technorati labels: Java, C ++, Python
Java is not just another programming language. -- anonymous
Learning the updated language helps you understand what others are dissatisfied with the old language. -- Anonymous
Preface
First, describe the writing habits of this article. The writing process is in the order of reading Java programming language, generally, we take a different Java and C ++ features that I think are worth mentioning as a section. Each section should be titled Java's new features as much as possible. First, we will compare C ++, Java, the differences between Python and Python are discussed later. No one can become an expert in every field. I am also a beginner in Python, and I have not learned Java or C ++ for 2 or 3 years. This learning method is purely for comparison, there is no difference. Although I often have a few books in my hand and read them from time to time, the technical injury in this article is just like my own level, which is difficult to avoid. please correct me.
1. determine the size of the native Integer type
C ++ -- the type size is generally the minimum value, and the integer type is rich.
Java -- the size of the native Integer type is completely determined by the standard, and the integer type is rich
Python -- integers are only divided into standard and long integer types. The standard Integer type has uncertain length and long integer type.
This feature of Java is not much changed, but it is more convenient than C ++. C ++ inherits the tradition of C, and the original type of the standard is only the minimum recommended size. The specific right is left to the compiler, because of this feature, the popular saying is "one hundred compiler manufacturers have one hundred types of C ++. Therefore, in C ++, we use typedef to obtain the desired size (which can be easily changed at that time), reduce int usage, and use short, long, with a more specific length, reduce the portability problem caused by uncertain integer size.
In fact, this feature may take into account the difference in execution speed of different hardware values of different sizes. For example, a 16-bit machine can process 16-bit integers at a time with the highest efficiency, 32-bit machines are 32-bit machines, while 64-bit machines are 64-bit machines. c ++ is a language that is not completely isolated from hardware and does not abandon the underlying development market, so int occurs. This type is used to adapt to the most suitable type of the current machine, and Other types are not forcibly specified, which makes it difficult to avoid sizeof, especially when calculating the length of an array, sizeof is simply a standard usage. Java abandons this usage, determines the size of all native types, and simplifies the program code to a certain extent.
The native Integer type of Python basically uses a standard integer, and can be converted by default with a long integer that supports unlimited length, which is more radical than Java and is actually more convenient to use, the most important thing is to solve the numeric overflow problem in both C ++ and Java. However, it is easier to provide fewer choices for programmers, and there is no way to make it easier to use the memory ....
The numeric overflow problem is even more of a big problem. This is the result of an abnormal number of tricks. when defining a numeric value, the original possible scale is not predicted, and we have to expand it, it is also a nightmare in large-scale programs. (I have encountered more than once at work) when there are a large number of programmers, different integer types are used for values of the same meaning, and the overflow bug occurs when passing between them, it is also very difficult to debug, and it will make it clear that the module that has passed its own unit test finally has problems, these may only be placed in the radical python, we can solve this problem by cutting away the memory saving measures. In C ++ and Java, more programmer requirements may be required to slightly ease this situation.
2. Remove the use of unsigned integers
C ++ -- whether the integer type can be signed or not
Java -- all native integer types are signed by standard dead
Python-all signed
In C ++, you can select whether an integer type is unsigned by default. Although the principle of improving the integer type is followed, automatic conversion with or without symbols can be performed in computing, which often leads to problems. In Expert C Programming 1.10, Peter van der Linden described this, we recommend that you do not use the unsigned type in the code to avoid unnecessary complexity. But in fact, in my work experience, there are still many unsigned applications in the company, especially for values that do not have negative numbers, or even prefer to use unsigned integers. However, it cannot be useless to describe unsigned integers. During bitwise operations, especially bitwise offset operations, unsigned integers can reduce the consideration of multiple situations and simplify code, in Expert C Programming, we recommend that you use unsigned numbers only when bit segments and binary masks are used.
I am most impressed with the symbol usage in C ++. (It cannot be all identified as a problem caused by the presence or absence of symbols) in the work, many legacy codes like to use-1 to represent the maximum number of unsigned numbers. In fact, this is only safe when the type is int. In other cases, the maximum unsigned number can be obtained, but it may cause problems during comparison, if you don't know, you will often get depressed during debugging. At that time, the program that was developed often communicated over the network (the program on the server side), increasing the seriousness of the problem, I even helped my colleagues debug these problems.
For example:
#include
using namespace std;int main(){ unsigned int d = -1; cout d endl; if(d == -1) { cout " d == -1" endl; } else { cout " d != -1" endl; } unsigned short s = -1; cout s endl; if(s == -1) { cout " s == -1" endl; } else { cout " s != -1" endl; if(s == (unsigned short)-1) { cout " s == (unsigned short)-1" endl; } else { cout " s != (unsigned short)-1" endl; } } return 0;}
Output:
4294967295
D =-1
65535
S! =-1
S = (unsigned short)-1
Note: In C ++, after unsigned short assigns a value with-1, it is not true in comparison with-1. To avoid this, You can generally use two methods, one is to add a definite type to all-1 usage, as shown in the above example. The other is to discard-1, but use 0 xFFFF ..... .
After the symbols are unified, these problems will naturally not exist.
3. class member variables are initialized by default + non-initialized variables on the stack cannot be compiled
C ++ -- when an uninitialized variable is used, the variable value is uncertain. The residual data in the memory is actually used, and the class and stack usage are the same.
Java -- class member variables are initialized by default + non-initialized variables on the stack cannot be compiled
Python-dynamic type language, variables do not need to be declared, use undefined variables will throw runtime exceptions, nameerror
C ++'s behavior is simply a total of anger ....... Well, of course, in fact, the company and C ++ users have a lot more rules to avoid this. For example, they should initialize the variables first, even if they will be used immediately, for example, if memset is used for all memory allocated, all arrays are equal to {0}, pointers are used, and all arrays are equal to null. In addition, the definition of variables is postponed, variables are not defined until they are used to prevent initialization from being forgotten.
As a matter of fact, I personally think it is still a matter of efficiency that makes C ++ so (maybe because it inherits C). If we do not take efficiency as our mission like Java, c ++ should be much better. In the design philosophy of C ++, variables that can be not initialized must be initialized first. Why? Haha, when all the variables in C language must be defined before the block, it is indeed consumption to give each variable a meaningless initial value.
Needless to say, it is another example that facilitates the improvement of programmers (but delays the efficiency.
4. the array contains length information.
C ++ -- native arrays have no length information, but containers such as vector have
Java -- the array itself contains length information
Python -- the array itself contains length information
The native array in C ++ does not contain any information except its own elements. This method is compatible with C, but to be honest, it is not very easy to use, making it inconvenient to use arrays, for example, when passing a parameter, You need to specify an additional method to indicate the end mode, either passing the length, passing the end pointer, or if the string ends with zero, it is not very good, although vector is provided as a replacement solution in STL, native arrays are often used due to various factors (compatible with C and memory saving, having used to this kind of thinking is actually not a problem. Of course, for beginners, Java and Python are all objects, and arrays are itself a class object, which is easier to accept, in fact, in the simplest way, when passing a parameter, the number of parameters will be smaller, which is easy to use and difficult to make mistakes.
5. array access range detection
C ++ -- the array access range is not detected, and some implementations provide optional Detection
Java -- array access range detection. arrayindexoutofboundsexception is thrown out of the range.
Python -- array access range detection. If the access range is out of the range, indexerror: tuple Index out of range is thrown.
Array access range detection does not completely solve common cross-border access problems in C ++. It only throws an exception when it is encountered during runtime, facilitating debugging and identifying problems. Although such problems can be avoided through good habits, once there is a very difficult debugging, it is difficult to come up with inexplicable bugs, the first response of senior programmers in the company is the problem caused by out-of-bounds array access (in fact, the ratio is relatively small). Therefore, I still quite support this kind of runtime detection, however, during the interview, the company's programmer boss said that when I used Java, I knew how slow the Java array access was, and thought that the reason was the range detection during the runtime, because I am new to Java, it is right or wrong, and I am not sure yet, but this should be of reference value.
Postscript:
This article was written when I just passed the interview with a new company and had not yet entered the company, because it was still incomplete and has not been released. I thought I would use Java in the new company, so I have not yet entered the company to consolidate it (I have little knowledge about Java, but I have only read thinking in Java). However, after I entered the company, or do C ++ work ....... Therefore, this series of incomplete articles were first published. Without an accident, this series will end ............ (You may not know how to start again if you have time or work in the future)
References
1. Thinking in Java, English version, 4th, by Bruce Eckel, Mechanical Industry Press
2. Java programming language, English version, 4th, Ken Arnold, James Gosling, David Holmes, people's post and telecommunications Press
3. JDK 6 documentation, Java online document set
4. the Java language specification, Third Edition
5. Java platform, Standard Edition 6 API Specification
6. C expert programming by Peter van der Linden, translated by Xu Bo, people's post and telecommunications Press
7. "Python core programming" by Wesley J. Chun, translated by Song Jiguang, people's post and telecommunications Press
The author of the original article retains the copyright reprinted. Please indicate the original author and give a link
Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie