Basic knowledge of Java: System Command calling, serialization, JDO, and anonymous internal class

Source: Internet
Author: User

1. Java calls system commands or executableProgram

 //  String cmd = "F: \ apache-tomcat-6.0.20.exe ";
// String cmd = "d :\\ Program Files \ Microsoft Office \ office11 \ winword. exe f :\\ test.doc ";
// String cmd = "cmd.exe/C start F: \ test.doc ";
String cmd = "Ping www.baidu.com ";

Runtime run = runtime. getruntime (); // Returns the runtime objects related to the current Java application.
Try {
PROCESS p = run.exe C (CMD ); // Start another process to execute the command
Bufferedinputstream in = New Bufferedinputstream (P. getinputstream ());
Bufferedreader inbr = New Bufferedreader ( New Inputstreamreader (in ));
String linestr;
While (Linestr = inbr. Readline ())! =Null )
// Obtain the output information on the console after the command is executed.
System. Out. println (linestr ); // Print Output Information
// Check whether the command fails to be executed.
If (P. waitfor ()! = 0 ){
If (P. exitvalue () = 1) // P. exitvalue () = 0 indicates normal end, 1: Abnormal End
System. Err. println ("command execution failed! ");
}
Inbr. Close ();
In. Close ();
} Catch (Exception e ){
E. printstacktrace ();
}

2. Java serialization (serialization)

It is a transmission mechanism established by Java. It can convert object information into a series of bytecode that can be transmitted to the network or stored in a file. Serialization is very simple to use, but it still has restrictions. It must immediately access the features of the object, and it is not suitable for accessing large volumes of data. If an error occurs when changing the properties of an object, it cannot be rolled back. Therefore, it is not suitable for the data integrity requirements of applications, in addition, data cannot be read and written asynchronously by multiple threads or programs. All these shortcomings make serialization unable to meet the requirements of most data storage.

1) serialize the elements that can be saved
Serializing can only save the non-static member traffic of an object, but cannot save any member methods and static member variables. serializing only saves the value of the variable, no modifiers for variables can be saved. A. Permanently Save the object and save the object's byte sequence to the local file; B. Pass the object through serialization in the network; C. Pass the object through serialization in the process.

2) serialization: Save the object state and restore the object as needed. The serialization interface is used as a tool for serializing objects. Only objects of serialization classes can be serialized.
3) transient keyword
For some types of objects, their statuses are instantaneous, and such objects cannot save their statuses. For example, a thread object or a fileinputstream object. For these fields, we must use the transient keyword to indicate them. Otherwise, the compiler will take measures.
In addition, serialization may involve storing objects on disks or developing data on the network, which may cause security problems. Because the data is outside the Java Runtime Environment and is not controlled by the Java security mechanism. These fields that need to be kept confidential should not be stored in permanent media or simply not stored without processing, in order to ensure security. The transient keyword should be added before these fields.

4) when a parent class is serialized, the Child class is automatically serialized without explicitly implementing the serializable interface;

5) A. parcelable is superior to serializable in memory usage, so parcelable class is recommended. B. serializable will generate a large number of temporary variables during serialization, resulting in frequent GC. C. parcelable cannot be used when data is stored on a disk, because parcelable cannot guarantee data continuity in the case of external changes. Although serializable is not recommended for low efficiency, we recommend that you use serializable in this case.

3. JDO

JDO is a new specification for Java object persistence. It is short for Java data object and is also a standardized API for accessing objects in a data warehouse. JDO provides transparent object storage. Therefore, developers do not need to store data objects.Code(For example, the use of JDBC APIs ). these tedious routine work has been transferred to the JDO product provider, freeing developers to concentrate their time and effort on business logic. in addition, JDO is flexible because it can run on any data underlying layer. JDBC is more common for Relational Database Service (RDBMS) JDO and provides underlying storage functions for any data, such as relational databases, files, XML, and object databases (odbms, makes applications more portable.

JDO: JDO integrates many of the features of the above persistence mechanism, which makes creating a persistence class in JDO as simple as creating a serialization class. JDO supports batch data storage, data consistency, concurrent processing, and JDBC query. Just like object-relational ing software and object database, it allows the use of advanced object-oriented features such as "inheritance ". It avoids the dependency on strict specifications defined by the vendor, just like the real bean in EJB. Like EJB, JDO does not specify any specific backend database. However, there is no "Wan lingdan" in the world ". Therefore, using JDO is not good for every application. Many applications can use other ideal storage mechanisms.

4. Internal and anonymous internal classes

1. Basic internal theory:

Java internal class: the definition of an internal class is defined within another class. The reason is:
1) An internal class object can access the implementation of the object created for it, including private data. That is, an internal class instance is privileged to the instance of the class that contains it.
2) for other classes in the same package, internal classes can be hidden. In other words, internal classes, regardless of method visibility, are afraid of being public. Apart from inclusive classes, other classes cannot use it.

2. Anonymous internal class

Case:

Public   Class Mymain {

Public Static Void Main (string ARGs []) {

New Out (){
Void Show (){
System. Out. println ("this is anonymous interclass showing .");
}
}. Show ();

New Myinter (){
Public Void Print (){
System. Out. println ("this is myinter showing .");
}
}. Print ();
}
}

Class Out {
Void Show (){
System. Out. println ("this is out showing .");
}
}

Interface Myinter {
Void Print ();
}

An anonymous internal class is an internal class without a name. Note:

1) The anonymous internal class cannot have Constructors
2) Anonymous internal classes cannot define any static members, methods, and classes.
3) Anonymous internal classes cannot be public, protected, private, or static
4) only one instance of the anonymous internal class can be created.
5) an anonymous internal class uses it to implicitly implement an interface or implement a class.
6) because the anonymous internal class is a local internal class, all restrictions on the local internal class apply to it.

Related Article

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.