Java code format specification personal recommendation (with examples)

Source: Internet
Author: User

I personally organize the Java encoding format specifications, which is also the specification I have observed during development. The code format specification is only intended to reflect a standard and aesthetic. You can comply with your own specifications according to your own situation and company requirements.

 
 
  1. /**
  2. * For Java coding format recommendations, see JDK source code and Hyperic HQ source code. The famous open-source software under spring is now vmware ).
  3. * @ Author lihzh (coder)
  4. * Address: http://mushiqianmeng.blog.51cto.com/3970029/737120
  5. */
  6. Public class CodeRule {
  7. // Declare the variable with spaces on both sides of the equal sign.
  8. Private static int I = 1;
  9. // Method declaration. There are spaces between the right and left braces.
  10. Public static void main (String [] args ){
  11. // If statement, the comparison connector (>) has spaces, and the parentheses and braces have spaces in the middle.
  12. // If and left parentheses have spaces in the middle
  13. If (I> 0 ){
  14. System. out. println (I );
  15. }
  16. // Connect the two conditions (&) with spaces on the left and right.
  17. If (I> 0 & I <2 ){
  18. System. out. println (I );
  19. }
  20. // If. else statement format
  21. // 1. Refer to JDK for personal usage. else and braces have spaces before and after.
  22. If (I> 0 & I <2 ){
  23. System. out. println (I );
  24. } Else if (I> 2 ){
  25. System. out. println (I + 1 );
  26. } Else {
  27. System. out. println (I );
  28. }
  29. // 2. Refer to the Hyperic HQ source code. else starts a new line and still has Spaces
  30. If (I = 1 ){
  31. System. out. println (I );
  32. }
  33. Else {
  34. System. out. println (I );
  35. }
  36. // While Statement, which is of the same type as if statement. while and parentheses have spaces. The format inside the brackets is the same as if.
  37. While (I> 0 & I <2 ){
  38. System. out. println (I );
  39. I ++;
  40. }
  41. // For statement, two formats
  42. // 1. For more information, see Hyperic HQ. The semicolon is followed by a space. In each substatement, the connector is left or right with a space.
  43. // For and parentheses have spaces in the middle and the size Brackets have spaces in the middle.
  44. For (int j = 0; j <10; j ++ ){
  45. System. out. println (I );
  46. }
  47. // 2. See JDK. The difference is that there is no space between the connector and left in the substatement.
  48. For (int j = 0; j <10; j ++ ){
  49. System. out. println (I );
  50. }
  51. // +-*/, Format. There are spaces before and after the four Arithmetic Operators.
  52. // In some JDK Code, there is no space before or after the four arithmetic operators in the parameter passing of the method call or the existence of the four arithmetic operations in the judgment statement.
  53. // In order not to cause confusions or confusions, spaces are reserved for all individuals.
  54. Int a = 1 + 2;
  55. Int B = 1-2;
  56. Int c = 1*2;
  57. Int d = 1/2;
  58. // Ternary expression format, with spaces in the middle of each symbol
  59. Int j = I> 2? 1:-1;
  60. // Method description and call. The parameters are separated by commas (,) and there are spaces after commas.
  61. Sum (a, B );
  62. Sum (c + d, j );
  63. }
  64. // Method declaration, multiple parameters, with spaces after comma
  65. Private static int sum (int I, int j ){
  66. Return I + j;
  67. }
  68.  
  69. }

Note: Most of the format details can be set in the formater format of Eclipse. In the future, you only need to use ctrl + shift + F for formatting. For example, the if else format, the number of characters in each line is full, and so on.

 

This article from the "difficult coder" blog, please be sure to keep this source http://mushiqianmeng.blog.51cto.com/3970029/737120

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.