Java (SE) You don't know -- excellent reply Summary

Source: Internet
Author: User

Original post content: LZ (that is, I am a younger brother): java (SE) You don't know. Here, I will give two board bricks with a thick face: 1. how to pass parameters to the main method when running java programs? During command line execution, multiple parameters are separated by spaces after the class name and parameters. For example, java MyTest Tomcat is good in use. "Tomcat is good in use. "is divided into five strings and passed to the main method of MyTest (as args ). 2. We usually use j2se basic classes such as String and Integer, where is the java installation environment? In $ HOME \ jre \ lib \ rt. jar. (Rt, everywhere ..) 3. There is no problem with the jar package. The Main-Class is also set for MANIFEST. MF. Why cannot I execute the main method for the response ("Invalid or Invalid upt jarfile" is reported ")? Main-Class: Add a space after the colon and configure the Class with the package path, for example, Main-Class: com. test. in this way, you can run the jar package: java-jar XXX in the command line. jar (of course, if the default open mode of the jar file is configured as "Java (TM) platform SE binary", you can also double-click the jar file for execution) 4. all strings have the same hashCode and are not necessarily equals. How to construct two strings so that their hashCode is the same and the strings are different? First, to simplify the problem, we can limit that the same length is two strings. We know that String. hash policy of the hashCode method (refer to the source code): cyclically traverses the characters in the string (c Represents the character, ASCc represents the ASCII value of c), and calculates: hashcode = 31 * hashcode + ASCc because the string has only two digits, the initial value of hashcode is 0, and our two strings are assumed to be "XY" and "MN ", the hashcode is (31 * ASCX + ASCY) and (31 * ASCM + ASCN) respectively ). Then we can make up the number: any value (smaller as possible, within the ASCII code range) can be obtained for ASCX and ASCM. ASCY and ASCN should be based on the values of ASCX and ASCM, obtain the curve relationship (the value must also be in the ASCII code range of 0 ~ Within 127 ). For example, if the value of ASCX is 49 and ASCM is 50, the equation 31*49 + ASCY = 31*50 + ASCN is obtained, and the conversion is ASCY = 31 + ASCN. In this case, set ASCN to 66, and ASCY to 97. Next, you can use System. out. println (char) 49); Identify the characters corresponding to each ASCII code: 49, '1'; 50, '2'; 66, 'B'; 97, 'A '. As a result, two strings with the same hashCode and different strings are constructed: "1a", "2B" (I wiped it, not intentionally constructing this string ..). (For good looks, we have collected a regular character. You can also try other ASCII characters, 0 ~ In the range of 127, the switch expression in JDK 7 can be String. 2L (rumlee): When we print a string, we only know that there is a System. out. print and System. out. I recently learned about the two println functions. java also provides printf functions similar to C language. 3L (Iamhuanhuan): The biggest feature of java is platform independence, Because java runs on a Java Virtual Machine (jvm ), it is achieved by simulating various computer functions on the actual computer. It interacts with the operating system to shield the operating system environment for java to run, so that Java programs only need to generate bytecode that can run on the java Virtual Machine. 6L (chenchenliu): a large part of java's popularity has something to do with its cross-platform, but now I feel that cross-platform is no longer his advantage .. Net may also be cross-platform (7L (longtian1213): Use enum to implement the singleton class. Previously, the method 8L (yanxing2012) can be defined in enum: The String object is immutable, when adding a variable, convert it to StringBuffer first, call the append method, and then convert it to String. The original String then references it 12L (tlfu_12344): The loading sequence of the class member variables is: static block of the parent class (static variable) --> then static block of the Child class (static variable) --> parent class member variable --> parent class constructor block --> subclass member variable --> subclass constructor block 13L (fudongrifdr): the limitation expression of static method calls can be calculated, however, its value is ignored. Null limit 14L (kittaaron): Variable Parameter application (C and CPP all have this syntax, the most typical is the implementation of String format): public PrintWriter format (String format, object... args); 17L (huntor): StringBuilder is used now. + The operation will be converted by the compiler into the append 19L (huntor) of StringBuilder: String + operation, write the code segment. After compilation, use javap-c to view the operation. 29L (rumlee): When writing java code, we generally use English letters, underscores, numbers, and so on as class names and variable names, but in fact, java can fully support using Chinese as these names 30L (rumlee): c Performance is higher than java, it depends on what type of application is made 31, 32L (raistlic ): I checked the part of the method binding during JLS 7 Compilation, so I had the following code to guess whether it could be compiled. If yes, what 37L (w215230188) is output for all these methods ): post-auto-increment principle: first declare a temporary variable storage I non-auto-incrementing value int temp = I; that is, temp = 0 41L (huntor): when many people change their time, take a string of numbers by yourself. In fact, java. util. concurrent. timeUnit provides the conversion method: 42L (huntor): null instanceof Xxxx returns false, so aObject! The non-null judgment before = null & aObject instanceof Xxxx is redundant. 43L (valenon): java language and java Virtual Machine will develop independently in the future, because java Virtual Machine can run bytecode files, and bytecode files may not be generated only by java. In fact, there are already some languages that can generate bytecode and run on java virtual machines, such as JRuby and Groovy. We hope the jvm will become more universal in the future. 46L (qq467310440): java Rounding Problem 52L (huntor): Use switch (this) 56L (nj_dobetter) in the method that defines the enum class ): java programs without the main function can run 59L (lin9118) through java commands: Do not use intermediate variables to implement two number swaps 60L (lin9118 ): calculate the power of a number multiplied by 2 without having to use the for loop to implement 61L (ldh911): static code snippet 62, 63L (raistlic): ThreadLocal instructions and the original post address of the file path: in the http://bbs.csdn.net/topics/390538912 post many J2SE small knowledge point is very interesting, the paste is relatively scattered, It is summarized here to write a summary. If you are interested, you can go to the post to see the detailed description of the detailed floor.

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.