java注释讲解

       注释简单的来说就是一种说明,不能被当成执行语句执行。做为一名程序员,但我们在写代码时是顺着思路写下去的。写代码好比就是在做题。当你在做的时候你脑海时的思路很清晰,会想到用一些特殊的方法来解决当前的问题。但是但你在解决一个大型问题时。思路有很多,而且有的比较复杂,现在你现写代码时,自己心里清楚自己为什么要这么写。但时隔几天或几个月,当你再次打开你所写的代码时。你会发现一大堆代码看下来整个人会晕头转向的。会忘记当时所写的部分代码的功能或实现过程。简单的说就是会忘记当时的思路。

       而这时注释的作用就显示十分重要了。当你在写代码时。写到关键部分你可以写下一些说明(注释),说明你的思路或此段代码的功能和作用。当有了这些说明,你时隔几个月再回来看自己曾经写的代码时就能快速找回当时的思路,快速理解代码。值得提的时。以后在工作上,往往是团队合作。今天你写了几行代码。明天可能你请假,另一个人来接手你的工作。接着你的代码写下去。但如果你写代码时用到了一些别人不知道的思路时。如果没有注释,别人根本就很难理解你的代码。也就难以接手你的工作了。

1、单行注释,以“ // ”开头后面接所要加的说明的内容,或者将光标置于要注释掉的代码行中,然后按快捷键 Ctrl + / 组合键,可以快速将该行注释,注释效果如下:

 1 package guide;
 2 
 3 public class GuideTest {
 4 
 5     public static void main(String[] args) {
 6         int[] arr1 = {0,12,34,99,53,23,66,89};
 7         
 8 //        Guide gu = new Guide();
 9         gu.printArray(arr1);
10         
11         int max = Guide.getMax(arr1);
12         System.out.println("max:" + max);
13         
14         int result = Guide.getIndex(arr1, 66);
15         System.out.println("index:"+ result);
16 
17     }
18 
19 }

第8行为注释掉了(在编辑器IDE中会变色),如果要编辑该代码时,第8行注释掉的代码会跳过编译的。

2、多行注释或称代码块注释,以“/*”开头,以“*/”结尾,效果如下:

 1 package guide;
 2 
 3 public class GuideTest {
 4 
 5     public static void main(String[] args) {
 6         int[] arr1 = {0,12,34,99,53,23,66,89};
 7         /*
 8         Guide gu = new Guide();
 9         gu.printArray(arr1);
10         
11         int max = Guide.getMax(arr1);
12         System.out.println("max:" + max);
13         
14         int result = Guide.getIndex(arr1, 66);
15         System.out.println("index:"+ result);
16         */
17 
18     }
19 
20 }

第7行以 /* 开头,到第16行以 */ 结尾,这样就会将第8行至第15行所有代码注释掉(全部变色了),编译时就会跳过这块代码。当然了也可以按下面这样进行多个单行注释,但看起来不美观,不太建议,效果如下:

 1 package guide;
 2 
 3 public class GuideTest {
 4 
 5     public static void main(String[] args) {
 6         int[] arr1 = {0,12,34,99,53,23,66,89};
 7         
 8 //        Guide gu = new Guide();
 9 //        gu.printArray(arr1);
10 //        
11 //        int max = Guide.getMax(arr1);
12 //        System.out.println("max:" + max);
13 //        
14 //        int result = Guide.getIndex(arr1, 66);
15 //        System.out.println("index:"+ result);
16 
17     }
18 
19 }
原文地址:https://www.cnblogs.com/aziji/p/10075805.html