Java and C + +

Source: Internet
Author: User
Tags html form java reference

Java built-in multithreading support. You can create a new thread by inheriting the thread class (overriding the Run () method). Mutexes occur at the object level using the synchronized keyword as the type modifier adornment method. At any one time, only one thread can access the synchronized method of a particular object. In other words, when entering a synchronized method, the object is "locked" first, which also works for other synchronized methods that use the object, and "unlocks" the object when exiting the method. There is no explicit lock, and locking and unlocking are automatic. You still need to implement more complex thread synchronization by creating your Own "monitor" class. The recursive synchronization method works fine. Time slice allocations are not guaranteed between threads of the same priority.

public , private , and protected ) is placed at the definition of each member of the class. Without an explicit access modifier, an element defaults to "friendly" and can be accessed by other elements under the same package (they are equivalent to the private keywords are rarely used in Java because "friendly" access is generally more useful than excluding access to other classes under the same package. (However, for multi-threading, the correct use of private is necessary.) In Java, the protected keyword means "accessible to inheritors and classes under the same package." "This is different from protected in C + +, meaning" Allow only inheritors access "(

nested class. In C + +, nesting a class is intended for name hiding and code organization (but C + + namespaces eliminate the need for name hiding). The Java package mechanism provides a namespace-like effect, so name concealment is not a problem for Java. The inner class of Java 1.1 looks like an inline class. However, an inner class object is a possession of a reference to an external class object that participates in the creation of an inner class object. This means that the inner class object can access the members of the outer class object without restrictions, as if those members were directly subordinate to the inner class. This provides a more concise solution to the callback problem, using pointers in C + + to resolve the problem. Because of the inner class described in the previous point, there are no pointers to members in Java. There is no inline method. The Java compiler can decide on its own inline method, but you don't have much control over it. You can use the final keyword to suggest a method inline. However, the inline function is also a recommendation to the C + + compiler. Java inheritance has the same effect as C + +, but the specific syntax is different. Java uses the extends keyword to inherit from the base class, using the super the keyword only allows access to methods in the immediate parent class, and can only be traced up one level. In C + +, access to deeper base class methods is allowed. The constructor of the base class is also called using the super keyword. As mentioned earlier, all classes eventually inherit from the object class. Unlike C + + in Java, there is no explicit construction of an initialization list, but the Java compiler forces you to initialize the base class first in the constructor method, and the compiler does not allow you to initialize the base class later. In Java, the initialization of a member is guaranteed by combining automatic initialization with an uninitialized object reference exception.

Inheritance in Java does not change the level of protection for members in a base class. Unlike Java and C + +, you cannot specify public,private, or protected inheritance. Similarly, methods that override base classes do not reduce access to base class methods. For example, if the method in the base class is public, if you override the method, the access permissions for the method you override must also be public (this is checked by the compiler).

Java provides the interface keyword to create a class that is similar to an abstract base class that contains only abstract methods and no data members. This mechanism will be designed to be used only as an interface and to make a clear distinction between extending existing functionality through the extends keyword. It is worth mentioning that theabstract keyword produces a similar effect and cannot create objects of that class. An abstract class can include an abstraction method (although it is not required to be included), but it can also contain implementations, which are limited to single inheritance. The use of interfaces is a mechanism that prevents the need for virtual base classes like C + +.
Use the implements keyword to create a version of interface that can be instantiated, with syntax that looks similar to inheritance

There is no virtual keyword in Java because all non-static methods in Java use dynamic binding. Therefore, Java programmers do not have to decide whether to use dynamic binding. The virtual keyword exists in C + + because it allows you to perform performance tuning without using it for a slight performance gain (or, in other words, "If you don't use it, you can be immune from it"), which often causes confusion and surprises. The final keyword provides some leeway for performance optimization – it lets the compiler know that the modified method cannot be overridden, so it may be statically bound (it will be inline, so it is equivalent to a non-virtual method Call of C + +). These optimizations depend on the compiler.

Java does not support multiple inheritance (MI), at least not in the same sense as C + + (supported). Like protected, MI seems to be good, but you know that you only need it when you face a design problem. Because Java uses a single root inheritance, you will rarely encounter situations that require MI. The interface keyword is responsible for (allowing you to) combine (use) multiple interfaces.

The run-time type checking functionality is similar to that in C + +.

and C in the same manner as coercion type conversions. The compiler does not require additional syntax, and it automatically calls the dynamic conversion mechanism. While this does not have the self-explanatory benefit of "new casts" in C + +, Java checks its usage and throws exceptions at the right time, without the usual erroneous conversions in C + +.

Due to the absence of destructors in Java, exception handling differs from C + +. You can add a finally clause block to enforce the necessary cleanup statements. All exception types in Java are subclasses of the Throwable class, which guarantees that you can have a common interface.

Java's exception specification has a great advantage over C + +. Unlike C + +, when a function is called at run time, the Java exception specification is checked and executed at compile time when an exception to an error is thrown. In addition, the overridden method must follow the exception specification: The overridden method can throw an exception of the same type or subtype as the overridden method, compared to the overridden method in the base class. This provides a more robust exception handling code.

Java has method overloads, but no operator overloads. The String class uses the + and + = operators to concatenate strings, and string expressions Use automatic type conversions, but this is a special built-in case.

The const problem in C + + is naturally avoided in Java. You can only pass references to objects and do not automatically generate local copies for you. If you want to pass the same value as C + +, you can call clone () to generate a local copy of the parameter (although the clone () mechanism is poorly designed – see chapter 12th).
Copy constructors are not automatically called in Java.
To create a compile-time constant, you can do this:

Because of the security problem, writing a "program" is very different from writing an "applet". An important issue is that the applet will not let you write disks, because this will allow a program to download from an unknown machine and stain your disk. This situation has changed somewhat with the application of the Java 1.1 digital signature, which gives you a clear idea of the special permissions that each program author has on your system (one of which may have soiled your disk; you need to know which one and what happened.) )。 Java 1.2 also promises more powerful support for applets.

Because Java is highly restrictive in some cases, you may be prevented from doing important tasks, such as accessing the hard disk directly. Java solves this problem by using the native method, which allows you to invoke programs written in another language (only C and C + + are currently supported). This way, you can deal with platform-specific issues (in a relatively non-portable manner, however, the code is separate). Applets cannot invoke local methods, only applications can.

Java provides built-in support for annotation documents, so source code files can contain their own documents that are then stripped and reformatted by a program into HTML form. This is good news for document maintenance and use.

Java includes standard libraries that address specific tasks. C + + relies on third-party non-standard libraries. These tasks include (or will soon include):
-Network
-Database connection (via JDBC)
-Multithreading
-Distributed objects (via RMI and CORBA)
-Commerce
The practicality of these libraries and the nature of standards make it possible to develop programs faster.

Java 1.1 includes the Java Beans Standard, which can be used to create components in a visual programming environment. This drives the development of visual components that can be used in all developer development environments. Because you are associated with a visual component that is designed by a specific developer, this makes the component more selective and useful. In addition, Java beans's design is better understood by programmers, and component frameworks for specific developers tend to have steep learning curves.

If access to a Java reference fails, an exception is thrown. This does not occur until the reference is used, and the Java specification simply indicates that the exception must be thrown in some way. Many C + + runtime systems can also throw exceptions for bad pointers.

Java and C + +

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.