C + + programmer Java (i)

Source: Internet
Author: User

Construction and destruction

L can define an initialization function, but all variables inside the class are initialized before the initialization function.

L can directly initialize variables when they are directly defined in the class.

The order in which variables are defined in the L class is the order of initialization

The L class constructs all of the predefined variables first, and then calls the constructor

The garbage collection mechanism not only reclaims memory, but also moves the data structure to a compact area. In the case of a small amount of garbage, the labeling method is used without memory movement.

When you use this to invoke other constructors within a constructor, you must repeat the first sentence and call it only once. Other functions are not available with this call constructor.

Grammar

L Break and continue can be followed by a label

L for can have foreach syntax

L Java no sizeof

L/** Start writing Javadoc, use @ and other semantics

The static statement can be combined into a static block and executed only once.

l Enum has ToString, ordinal, and values methods

L try...finally Ensure that the code in the try block exits anyway, and the code in the finally is bound to execute

L Final allows the underlying types and references to remain the same, but does not leave the object itself unchanged. Java does not provide this functionality. Must be initialized before final use.

L final is used when the method is not covered by the quilt, so the private method is all implicit final method

L final is used for a class to indicate that it cannot be inherited, so all of its methods are implicitly final.

The foreach syntax of L for can be used in containers

L All exceptions have two constructors, one does not accept any arguments, one accepts a string

L use throws for exception description after function arguments

L Java does not allow overloading of any operators

L Static Imports can import the package into the current domain, and then use the no-packet path

The enum defines a class that inherits from Java.lang.Enum, which is a common class that has some limitations and can define methods. Because Java does not support multiple inheritance, enums cannot inherit other classes, but they can implement interfaces

Each instance of an enum can define different implementations of the same method, as long as the method is defined as abstract in the enum definition

l Use the volatile keyword to obtain read-write atomicity for this resource

L synchronized keyword can get atomic domain and code snippet (critical section)

Dynamic type detection

L Each class has a class object

L instanceof can be rtti to determine the type of object and can only be compared to a named type and not to a class object. Class.isinstance can also play the same function as the instanceof keyword

An important difference between instanceof and class objects is that instanceof considers subclasses, whereas class objects consider subclasses and base classes to be different types

L class GetMethods () and GetConstructors () methods return an array of method objects and constructor objects, respectively

L Class.forName () generated results are not known at compile time, and all method signature signatures are dynamically extracted at execution time. The function of this method is to find and load the specified class and obtain a list of its methods to support reflection.

L There is no way to hide a method against reflection, even if it is private. When you get the method object by class, setting the method's Setaccessible (true) causes any methods to become available. Even after the compiled code, the name of the private method can be displayed through Javap–private, which can be called.

L

Arrays and mutable argument lists

L do not allow the size of the specified array when defining an array (because an array reference is defined).

L array references can be directly equal to each other, but point to the same number of groups

The l array has a fixed field: length, which indicates the size of the array

L Java can implement variable parameter list in two ways, array and variable argument list syntax

The L function can return an array reference directly without worrying about memory problems. The JVM will reclaim its memory when it is not needed by the user

The copy array should use System.arraycopy (), which optimizes overloads for various types

The comparison of L arrays is performed using Arrays.equals (), sort using the Arrays.sort () function, the Arrays.binarysearch () function is only for sorted arrays, and the results of unsorted lookups are unknown

L arrays.aslist () can convert an array to a list

Package and access rights

The L code is assembled as a package, and each compilation unit can have a class with the same name as the file, and only that class is visible to the public and is decorated with public, with a maximum of one, no, and no file name at random.

The access rights of the L class are only the package access and public access rights.

A set of compilation units can be defined in a package, with the Package keyword added to the beginning of the compilation unit file.

L packet path is usually the reverse of the domain name plus file path

The access rights for the L class include public, private, protected, and package access (friendly), which is the default, which means that other classes in the same package can be accessed and not available outside the package.

L users of the class cannot access the package access rights

L in the same directory, no package is specified, the default package content for that directory.

L

Interfaces and internal classes

The L interface can contain fields, but the domain is implicitly static and final

L class with implements keyword implementation interface

L interface keyword-defined interface is also decorated with public, not decorated for the package access rights

L All methods in interface are public by default and can only be public

L subclasses can inherit multiple interfaces, but can inherit only one base class

L can inherit parent interface with sub-interface

The name of each interface method cannot be the same when the combination inherits multiple interfaces

The domain defined in the interface must be initialized and can be initialized with a very large expression.

The L interface can be nested into interfaces or classes, and can be defined as private when nested into a class

L Inner class has access to all elements of the perimeter class

The way to get references to external classes in an inner class is: the perimeter class. This

The creation of non-static inner classes must first create a perimeter class, and static inner classes (nested classes) do not require

L cannot access the non-static domain of a perimeter class from a nested class

Outer class to be used when creating an inner class: The outer class. New syntax

L Inner class function if you want to use an externally defined object, the function argument must be a final decoration

L Anonymous inner class must inherit a parent class or implement an interface

L can implement inner classes in interface, at which time the inner class is automatically a nested class of public and static

L Multi-layer inner class can have full access to any upper class

The inner class is inherited and must be called manually in the grandson class, because the parent class (inner Class) has a reference to the Grandfather class by default. : Fulei.super ()

L can define a local inner class that can access all the fields before the method and the domain of the perimeter class

The L interface is not completely hidden from the class information, and can still invoke specific methods of the specific class if the user is using Rtti. The corresponding policy should be completely hidden from the user, only visible to the interface (for example, package access rights)

Mechanism

The compilation unit file ends in. Java and is compiled into machine code with a. Class ending

L Java does not have conditional compilation function

All class files are loaded only when they are used, and the static domain is initialized only at load time.

Java in addition to static and final (private) functions, other functions are late-bound (dynamic binding)

L All types of conversions are checked at run time, and ClassCastException exceptions are thrown without checking

Java generics are implemented by wiping out,list<string>, list<integer> at runtime is the same type, are List

If you want to continue to hold a reference to an object, but you want the garbage collection mechanism to reclaim the object when it is out of memory, you need to use the Reference object. SoftReference, WeakReference, phantomreference from strong to weak

The thread generated by the background thread is also a background thread, and if the foreground thread ends all, the background thread exits, but the background thread exits without affecting the foreground thread.

L incremental operation of Java is not atomic operation

Inherited

l Inherit using extend keyword

L Subclass access to parent class domain with super keyword

A function that overloads a parent class in a subclass does not overwrite a function of the parent class (C + +)

The private method subclass of the L base class can define a function with the same name, but it is not an overlay, but a completely new function

The L subclass can define a variable with the same name as the base class, but it is a completely new variable that must be qualified when accessed

L should not invoke dynamic binding functions (also known as normal functions) in constructors

L subclasses can have covariant return types

The class that has an abstract method is an abstract class that must also be limited by abstract

Container

L containers are divided into collection and map two kinds

L The new program should not use outdated vectors, HashTable, stacks

L set is a kind of interface, its concrete realization has hashset, TreeSet, Linkedhashset and so on

L Queue has linkedlist, priorityqueue

L Map Interface has hashmap (search), TreeMap (sort), linkedhashmap (iteration), Weakhashmap (Allow recycling), concurrenthashmap (concurrency), identityhashmap implementation

L throws a Concurrentmodificationexception exception if another thread modifies this container while using the container

Other common library operations

The System.out and System.err in the standard input and output can be used directly, while the system.in is a inputstream that has not been packaged and processed.

BufferedReader stdin = new BufferedReader (Newinputstreamreader (system.in))

L SetIn (InputStream), SetOut (PrintStream), Seterr (PrintStream) are used to redirect standard input and output

L use Osexcute.command () to execute system commands

L Externalizable Interface is not serialized, if not the interface class can be decorated with transient to do not want to be serialized domain, or to implement their own writeobject () and ReadObject () function.

L Enumset can finally put the enumeration decent into the set. Enummap can create more meaningful map key values

Defining a task requires inheriting the Runable interface and redefining the run () function in the class, which is the function body. But to generate a thread you must attach a task to a thread t = new Thread (new task) and then call T.start () to start the thread, so the run function in the task is called.

Another way to invoke a task is to use executor, a mechanism that enables a executorservice to manage all tasks. In addition, executor can also invoke the call method of the callable interface to generate threads. You must use the Executorservice.submit () call

The L Thread.yield () function forces the scheduler to work. Sleep yields CPU resources to make the scheduler work

L use Thread.CurrentThread (). SetPriority () to set the priority of the current thread.

L use Thread.setdaemon (true) to set the current thread as a background thread

L Use the Thread.Join () function to allow one thread to wait for another thread to finish before continuing execution

L can use synchronized keyword or java.util.concurrent library to lock resources

L provide atomic classes in Java: Atomicinteger, Atomiclong, atomicreference, etc.

Tricks

L Declare all constructors of a class as private, no one can create the class except the static method of this class

L Use a combination when using class functionality, and you should use inheritance when using class interfaces

L final static named variable all uppercase, and the underscore is split

L

Insufficient

Java array access checking, garbage collection, and the need for new objects are inefficient.

L Pre-Initialize all class variables superfluous

L Full late binding strategy that's ridiculous.

L No encapsulation can prevent the reflection mechanism from getting private functions

L can easily be disassembled.

L generics are too poor to work even with arrays

Advantages

L interface, implement, extend key words are very good

L The idea of taking full advantage of the package path consisting of file name and path is good.

L for foreach is good (c++11 has borrowed)

l have all the requirements of the class is very good

L Good frame generation and use (current C + + can do it, but no one does it)

L Extensive, integrated IDE-based, unified library support is good, currently C + + various library organization is too scattered

The implementation of the Java library is clearly organized and disciplined, taking full account of practical applications. For example, a widely available ToString

Java implements class objects for each class, so the dynamic type becomes easy and imaginative. But it also brings about efficiency issues. If the defined class has only a few objects, the cost is too high, but if the object is batch, and the RTTI can bring great advantages, it can be used. It is best to allow programming when specifying whether to use. The emphasis on large-scale projects and libraries requires a lot of this functionality. And this feature enables reflection, and reflection is the key to a new world of dynamic programming.

L integration of serialization into the language interior

The enumeration type is powerful and can even define methods. So the state machine itself can perform the conversion

• Implement many bulk operations in Java, especially for higher language integration of string and print information

L JavaBeans can be easily used to generate programs with components, C + + lacks such a provision to work closely with the IDE

C + + programmer Java (i)

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.