Java problem collection (transferred from the Internet)

zhaozj2021-02-16  73

1. Q: How do I check the binary information of the Java class file? A: Use the UE to open the class file in binary form .2. Q: How does Java pass the parameters? Is it by value or by reference? A: All Parameters (Values ​​of Primitive Types, And Values ​​That Are References To Objects) Are Passed by Value [JLS SECT 8.4.1]. According to the above Java specification file, all parameters should be all valued. But in fact, actual experience tells us that all Java objects are in the up reference. So we can explain this: Basic types (such as int, float, char, etc.) are by value, and so-called "objects" is BY Value of Reference to Object. 3. Q: Why is String this type of class Class definitions into Final? A: It is mainly for "efficiency" and "security". If String is allowed to be inherited, since its height is usage, the performance of the program may be reduced, so String is defined as final. 4. What is the difference between Finalizers and C DESTRUCTORS? A: The mechanism of "memory use recycling" in Java, although it also provides a similar (C ) deStructors Finalize (), each object can use this method Method, but must take the risk of destroying the original recycling mechanism. So it is recommended to avoid using Finalize (), you can consider using NULL and DISPOSE () to release resources. 5. Q: Inherited a class called Frotz, the compiler gives me "No constuctor frotz () in the class" error prompt, why? A: Remember whenever you define a CLASS CONSTRUctor unless you go to Call this constructor at first, Java will automatically introduce SuperClass's original constructor, if superclass's constructor is parameter, then the problem The error prompt appeared. The method of solving is simple, find an API file, plus the correct parameters. 6. Q: How to make the char type to convert into int type? A: CHAR C = 'a'; int i = c; // In turn, as long as the mandatory type conversion is C = (char) i; 7. Q: My applet is originally good, one put it in Web Server There is a problem, why? A: Generally, try to determine the format of the class file. All file names are consistent with the Class name, and there is no error in the case; if you use the package in the program, you should be careful. For example, you declared a package called com.foo.util in the class, then there is no COM / FOO / UTIL under the Applet codebase directory of Web Server.

(Note that the directory name also has a large number of cases); the file files on the web server should be set in advance. (Especially Search, Read and Execute Permissions) 8. Q: How do I use a graphic full background with a graphic in the applet? A: Establish Panel or Canvas on the background, then fill it with graphics. 9. Q: How do I build my own menu in the applet? A: First, Frame Instance is created in the applet's init () method, and then you can add menus, menubar. (SETMENUBAR () is a method under Frame) or, finds Frame on the APPLET to work again. Container Parent = getParent (); while (! (!) Parent = Parent.getParent (); frame theframe = (frame) parent; Note: The second method is not feasible on the MAC or some Browsers. If you are using JDK 1.1, you can also consider using popup menu, so you don't have to pay attention to Frame. 10. How to compare two types of strings? A: Use "==" between the two objects, and the result of "two objects have the same reference" will be back. That is to say, this is equivalent to "whether the two objects have the same address", or "whether the two object objects are the same object." If you mean it is to determine if the contents of the two strings are the same, then the following methods should be used to: if (S1.Equals (S2)) or if (S1.EqualsignoreCase (S2)) or if (S1.StartSwith S2)) OR IF (S1.RegionMatches (S1_OffSet, S2, S2_Offset, Length) OR (S1.Compareto (S2) <0) 11. How to floating the number (FLOAT) Add? FLOAT does not seem to be? A: I guess your program is about to write this: float one; float two; float hard = one two; Please note that float and float are different, the former is one of the basic types of Java, while the latter is Class. You should change the source code to: float one; float tw; float hard = one two; or float one = new float (1.0); float two = new float (2.0); float hard = new float (one.floatvalue ) Two.floatValue ()); 12. How to convert a string string into an integer int? A: There are two ways: 1) int I = integer.parseint ([string]); or i = integer.parseint ([string], [int RADIX]); 2) INT i = integer.Valueof (my_str). INTVALUE (); Note: The string is transformed into Double, float, and long.

13. How to convert integer int converted into a string string? A: There are three ways: 1) string s = string.valueof (i); 2) string s = integer.tostring (i); 3) string s = " i; Note: double, float, long turn into words The method of string is similar. 14. How to add a record from the end of a file file? A: There are two ways: 1) randomaccessfile fd = new randomaccessfile (File, "RW"); fd.seek (fd.Length ()); then use FD's Method write 2) Use FileoutputStream (String Name, Boolean Append This constructor 15. How to set the environment variable of Java 2 (JDK1.2)? A: Java 2 is installed, you need to set the PATH and JAVA_HOME environment variables. Unlike JDK1.1: After setting up the Java_Home environment variable, the JVM will automatically search the system class library and the user's current path. Java 2 Environment Variable Settings As shown in the following example: Solaris platform: setENV JAVA_HOME JAVA2 installation path setenv path $ java_home / bin: $ {pat} Windows platform: set java_home = java2 installation path set path = $ java_home / bin;% PATH% 16. Which Java integrated development tools support Java 2? A: At present, the popular Java integrated development environment, such as the JBuilder; Symantec Visual Cafe, "Sybase's Powerj support Java 2. 17. If there is an error when running a Java Applet in the Netscape or IE browser, how do I determine an error range? A: When the Java Applet is running in the browser, use the browser itself's default JVM. Different browsers have different support for JDK. Therefore, running Java Applets in the Netscape or IE browser, it is recommended to use the tool AppletViewer or Sun's HotJava browser provided by JDK to test the applet to determine the error of the error is related to the browser. If the applet runs everything in AppletViewer or Hotjava, the error is caused by the browser is not fully compatible with JDK. At this point, the solution can be Java Plugin using the Hotjava browser or install Sun. If an error occurs in the HotJava browser or AppletViewer, you should check the Applet program based on the error prompt. 18. How to list all drive names in the PC file system in the Java language? A: In Java 2, the File class in the java.io package adds method Listroots () to implement this feature. 19. Why does Runtime.exec ("ls") does not have any output? A: Call the runtime.exec method will generate a local process and return an instance of a Process subclass that can be used to control the process or acquire the process.

Since the child process created by the runtime.exec method does not have its own terminal or console, the standard IO of the sub-process has passed process.getoutputStream (), process.getinputstream (), process. The geterrorStream () method is redirected to its parent process. The user needs to use these stream to enter data or get the output of the child process to the child process. Therefore, proper execution of the routine Runtime.exec ( "ls") as follows:. Try {process = Runtime.getRuntime () exec (command); InputStreamReader ir = newInputStreamReader (process.getInputStream ()); LineNumberReader input = new LineNumberReader (ir ); String line; while ((line = INPUT.READLINE ())! = Null) System.out.println (line);} catch (java.io.ioExcection) {system.err.println ("oException" E.GetMessage ()); A: When the default Serialize is implemented, an ObjectOutputStream constructor and the configuration of an ObjectInputStream must correspond one by one. The constructor of the ObjectOutputStream writes an identification head in the output stream, while ObjectInputStream will first read this identity head. Therefore, when writing Object in an additional manner multiple times, the file will contain multiple identification heads. So use ObjectInputStream to DeserialIze this ObjectOutputStream, StreamCorruptedException will be generated. One solution is to construct a subclass of ObjectOutputStream and override the WriteStreamHeader () method. The covered WriteStreamHeader () method should determine if the Object is written to the file for the first time? If so, you call super.writestreamhead (); if you write Object when you append, you should call the ObjectOutputStream.Reset () method. 21. The sequence of objects (SERIALIZATION) is a flow-oriented, how should you write objects into a random access file? A: At present, there is no direct method to write objects into a random access file. However, you can use the ByTearRay input / output stream as an intermedion to write or read bytes from the random access file, and can create object input / output flows for reading by using byte flow. Write objects. It should be noted that an error will occur when you want to include a complete object in the byte stream, otherwise you will have an error. For example, Java.io.byteArrayoutputStream can be used to get the byte stream of ObjectOutputStream, which can be obtained from the BYTE array and write it into a random access file. Instead, we can read the byte array from a random access file, and use it to construct ByTearRayinputStream, which is constructed to read objects.

转载请注明原文地址:https://www.9cbs.com/read-15799.html

New Post(0)