Summary of differences between Java and C ++

Source: Internet
Author: User

Java designers once said that the design of this language is mainly inspired by C ++.

In the world, C ++ is first available, and Java is then available. the development history of the Java language is a history of C ++. Therefore, comparing Java with C ++ makes sense. Through these comparisons, we can understand the improvements made by the Java language compared with the C ++ language, which is of great benefit for us to better understand these two languages.

Below I will summarize the differences between Java and C ++.

1. Java can be used to manipulate object references before initialization, while C ++ must point to objects at initialization.

2. Except static and final functions in Java, virtual functions are defined by default, while virtual functions are defined in C ++ using virtual keywords.

3. Java has a single inheritance structure inherited from the object type, while C ++ does not have a class inheritance system built into the standard library.

4. Java can only create objects on the stack through new, except for basic types. C ++ Can Create global objects or stack objects in addition to new.

5. Java has its own garbage collection mechanism, so you don't need to worry about the release of the object's memory. C ++ needs to manually release the object created through new.

6. Java array members can initialize the default value null, providing uninitialized access and out-of-bounds access protection, while C ++ does not provide this mechanism.

7. variables defined in Java local scopes cannot hide variables defined in larger scopes, while those defined in C ++ can be hidden.

8. The basic type member variables of the Java class can be automatically initialized to the default value. If the complex type member variables of the class or the basic type member variables of the function are not initialized, a compilation error will be reported, C ++ does not provide this mechanism.

9. Java is a pure object-oriented language. functions can only be defined in the class, while C ++ can define functions outside the class.

10. When calling Java functions, input parameters are passed. Object type parameters can only be passed for reference, but cannot be passed for reference. Basic type parameters can be passed for value, while C ++ can use Object-based value transfer.

11. Java uses class library naming with Reverse Domain Names to solve the name pollution problem, while C ++ uses namespace to solve the problem.

12. All member function implementations and variable initialization of the Java class must be written in the class definition, while the C ++ class member function declares in the class definition that it is implemented outside the class, member variables can be initialized in the class definition.

13. When Java uses a class name to reference a static member of a class, it uses a class name and A. number instead of a C ++: Number.

14. Each Java file can only have one public class with the same name as the file. This class can be accessed outside the package, but other classes cannot be accessed, and this class has a main function, C ++ does not have this constraint.

15. When Java annotations use a specific format, javadoc can be used to parse them into help documents. C ++ does not provide this mechanism.

16. Support for Java>. The value of C ++ is 0 or 1 based on the value of the first bit.

17. Java does not support Operator overloading, but C ++ supports this mechanism.

18. Java does not have the sizeof operator, because all data types have the same size in all machines, while C ++ supports the sizeof operator and the data types are not necessarily the same in different machines.

19. Java does not support conversions from integer to boolean, whereas C ++ supports this mechanism.

20. Java supports the foreach method to traverse arrays or container loops, but C ++ does not provide this mechanism.

21. Java supports tagged continue and break statements to provide limited functions similar to C ++ goto statements.

22. Java does not support C ++ destructor, but provides a finalize function that will be executed before garbage collection, however, in some special cases, we should use the finalize function to release resources, but release resources explicitly in the program.

23. Static members of the Java class will be initialized only when the class is instantiated for the first time. If the class is not instantiated, it will not be initialized, the static members of the c ++ class are initialized before class instantiation, and it does not matter whether the class is instantiated.

24. Java does not have an initialization list for Class C ++ members. It can only be displayed to class members after class members are defined.

25. Java arrays cannot be written out of bounds. Otherwise, an error is reported, whereas C ++ does not provide this mechanism.

26. Java does not support the pre-compilation macro mechanism because it does not need to consider cross-platform differential compilation control, whereas C ++ supports this mechanism.

27. Java provides the package access control permission to allow classes in the package to access each other, while classes outside the package cannot access classes in the package. C ++ does not provide this mechanism.

28. Java supports setting public or package access control permissions for classes, but C ++ does not.

29. All non-basic type objects in Java have a special toString method, which is used to convert an object into a String object. C ++ relies on the String operator processing function.

30. Java supports functions with the same name as the parent class in the subclass, and the super keyword can be used to display the functions that call the parent class, the subclass of C ++ directly overwrites the Same Name function of the parent class.

31. In Java, the sub-class constructor uses the super keyword to display the constructor for initializing the parent class, while C ++ directly calls the parent class constructor for initialization.

32. Java supports the keyword final for defining constants, function parameters that cannot be changed, methods that cannot be overwritten, and classes that cannot be inherited, C ++ supports the first two types by using the define and const keywords, but not the last two types.

33. Java uses abstract keywords to represent abstract methods of classes, while C ++ uses similar pure virtual functions.

34. Java supports defining an interface class as an interface class. Only abstract methods can exist in the interface class and implements can be used to implement this interface class. C ++ does not provide this mechanism.

35. Multi-inheritance of Java can only be derived from an abstract base class or a specific base class plus multiple interface base classes, or from multiple abstract internal classes, C ++ supports deriving from any number of abstract base classes or specific base classes.

36. Java has internal classes similar to the closure function and can access members of external classes. The nested class of C ++ does not support this mechanism.

37. The Java container has a common collection class. If the container does not specify a type, the objects stored in the container are of the Object type by default, but the C ++ container does not have these features.

38. Java exception mechanism supports the finally keyword, which ensures that code with the finally keyword is executed no matter whether the exception is matched or not. C ++ does not provide this mechanism.

39. Java standard library supports regular expressions, but C ++ does not provide this mechanism.

40. Java supports the reflection mechanism. What distinguishes it from RTTI is that the class type information does not need to be determined during compilation, but is determined at runtime, class classes can be used to obtain Class names, member variables, methods, and create Class objects. C ++ only supports the RTTI mechanism.

41. In Java generics, the basic type cannot be used as a generic parameter and the packaging type is required. For example, the int type must be encapsulated as an Interger type because the generic Object is erased as an Object type, the Object type cannot store the int type but can only store the Interger type. C ++ does not have this problem.

42. Java generics cannot obtain the type information of generic parameters during runtime. After the type consistency check is completed during compilation, the object type information is erased, converted to the Object type. during runtime, you cannot create objects of this parameter type, and you can only call Methods owned by objects or objects without generic parameters, one way to compensate is to add a boundary for the generic type. The generic type will only erase the first boundary of the generic type, and the method of this boundary type can be called by setting the boundary, the C ++ template does not have this problem.

43. Java wildcard supports the wildcard mechanism, Indicates a type derived from type T, Indicates the base type of a certain type of T, Indicates a type of non-Object, which is not supported by C ++.

44. Java provides the Arrays class for Array Operations, including some static functions. C ++ performs some operations through the C library, but it does not have the rich and powerful Arrays class of Java.

45. Java containers support optional operations. An object of a certain type does not support these interfaces. If you call this interface for this object, an UnsupportedOperationException exception will be thrown, this dynamic language feature is not supported by C ++.

46. The standard input and output objects of Java are System. in/System. out, while C ++ is cin/cout.

47. The enum type in Java is similar to a class and can have methods, except that it cannot be inherited. The enum of C ++ does not provide this mechanism.

48. Java supports the annotation mechanism and can embed metadata in a certain format in the code, which is not provided by C ++.

49. The Java Thread Library provides the Thread. yield () method, which can take the initiative to grant the Thread execution permission. C ++ does not support this feature.

50. The Java language supports the built-in synchronized keyword to mark a method or statement block as protected by a lock, all synchronized methods of an object or all static synchronized methods of a class share the same lock, but C ++ does not provide this mechanism.

51. Java Object. wait () suspends the current thread into sleep state, and releases the lock held when another process calls the Object. when the sleep thread re-holds the lock and is awakened to continue execution, C ++ calls similar functions of the operating system, you cannot sleep when you hold the lock, because sleep does not release the lock.

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.