26.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.
27. Unlike declarations that use control blocks for C + +, access modifiers(public,private, and protected) are 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 friends in C + +), but cannot be accessed by elements outside the package. class, and each method in the class, has an access qualifier to determine whether it is visible outside the file. Sometimes the private keyword is 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 proper use ofprivate is necessary.) In Java, the protected keyword means "accessible to inheritors and classes under the same package." "This differs from the protected in C + +, which means" only inheritable access allowed "(private protected is used to achieve the same effect, but the use of the keyword is canceled).
28. Nested classes. 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.
29. Because of the inner class described in the previous point, there is no pointer to a member in Java.
30. 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 an inline method. However, theinline function is only a recommendation to the C + + compiler.
The inheritance of 31.Java 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 keyword to invoke a method in the base class with the same name as the subclass (however, the Super keyword in Java only allows access to methods in the immediate parent class. 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 with 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.
public class foo extends bar { public foo (String msg) {super (msg); //Calls base constructor public Baz (int i) {//Override super . Baz (i); //Calls base method } }}
Inheritance in 32.Java does not change the level of protection for members in the 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).
33.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 an interface version that can be instantiated with syntax that looks similar to inheritance:
public interface face { Span class= "Hljs-keyword" >public void smile ();} public class baz extends bar implements Span class= "Hljs-title" >face { public void smile () {System.out.println ( "a warm smile.") ); }}
The virtual keyword is not available in 34.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.
35.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.
36. The Run-time type check function is similar to that in C + +. You can do this in order to learn the information that references X , for example:
X.getClass().getName();
For type-safe down-conversion, you can:
derived d = (derived)base;
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 37.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.
publicvoidfthrows IOException { myresource mr = b.createResource(); try { mr.UseResource(); catch (MyException e) { // handle my exception catch (Throwable e) { // handle all other exception finally { // special cleanup }}
The 38.Java 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.
39.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 40.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:
staticfinalint255;staticfinalint8255;
41. Because of security concerns, there is a great difference between the preparation of a "program" and the preparation of 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.
42. 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.
43.Java provides built-in support for annotation documents, so source code files can contain their own documents, which are then stripped and reformatted by a program in HTML form. This is good news for document maintenance and use.
44.Java includes a standard library for solving 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.
45.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.
46. 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.
47. Overall, Java is more robust, with:
-object reference is initialized to null(a keyword)
-the reference is checked and throws an exception when it fails
-All array access will be checked for bounds
-Automatic garbage collection mechanism to prevent memory leaks
-Simple, relatively reliable exception handling
-Simple language support for multithreading
-byte-code check of network applets
Compare C + + and Java II