标签:
标签 : Java基础
五月份得知入职阿里云OS, 才开始学Java, 断断续续学习/使用半年, 越来越喜欢这个语言/工具. 后来被拥抱变化之后, 拿到的大部分offer是Java服务端研发; 一路走来, 踩了很多坑, 也有了一点小小的心得, 而且博客已经停更几个月, 今天就以博客形式把他记录下来吧.
2015下半年第一篇博客, 从最基础的Java注释开始:
程序员圈有一个笑话
最讨厌在写代码的时候写注释, 最讨厌别人的代码里面不写注释.
我自己亲身经历:
这段时间在微店实习, 第一个接手的项目是将原先北京团队的代码迁移到杭州, 由于底层技术架构的更换, 大部分代码需要重写, 前提是要理解原先的业务逻辑, 但当我在SVN上把代码拉下来, 看到意大利面似的一大坨代码里只有寥寥几行注释时, 整个人都不好了…
另一个遇到场景, 有时自己的代码有Bug, 或者需要重构, 此时就需要Review代码, 可是突然发现自己已经很难理解原先逻辑了(很可能这段代码只是你前几天刚刚写的), 因为我们已经很难回到当时状态.
还有一个重要的原因就是文档, 往往一个系统被开发出来, 文档要么不全, 要么更新落后, 如果后人要接手这一套系统, 就必须直接阅读源码, 如果此时在代码的关键逻辑之处能够有一两行注释提示, 新人就没有必要绞尽脑汁去猜测当时的设计方案了.
后来自己写代码时就尽量写注释提示, 虽然不一定完全按照下面的注释规范, 但会尽量用 最简单的语言把问题阐述清楚, 在逻辑转折之处添加几行说明, 无论是自己还是未来的接手人, 都会对现在的你感激不尽.
Java提供三种注释方式: 单行注释、多行注释、文档注释.
command+/
: 以//
快速注释一行或多行 : // Integer[] array = new Integer[10];
// for (int i = 0; i < array.length; ++i){
// array[i] = new Integer(i);
// }
command+option+/
: 以/**/
快速注释一行或多行
/*
Integer[] array = new Integer[10];
for (int i = 0; i < array.length; ++i){
array[i] = new Integer(i);
}
*/
/** */
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
# javadoc 注释标签语法
标签 | 作用域 | 说明 |
---|---|---|
@author | 类 | 标明开发该类模块作者 |
@version | 类 | 标明该类模块的版本 |
@see | 类, 属性, 方法 | 参考转向(相关主题) |
@param | 方法 | 对方法中某参数的说明 |
@return | 方法 | 对方法返回值的说明 |
@exception | 方法 | 抛出的异常类型 |
@throws | 方法 | 与@exception相同 |
@deprecated | 方法 | 不建议使用该方法 |
下面是我自己看到和用过的注释原则:
/**
* The <code>String</code> class represents character strings. All
* string literals in Java programs, such as <code>"abc"</code>, are
* implemented as instances of this class.
* (其他描述)
* @author Lee Boynton
* @author Arthur van Hoff
* @author Martin Buchholz
* @author Ulf Zibis
* @see java.lang.Object#toString()
* @see java.lang.StringBuffer
* @see java.lang.StringBuilder
* @see java.nio.charset.Charset
* @since JDK1.0
*/
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
...
}
/**
* Initializes a newly created {@code String} object so that it represents
* the same sequence of characters as the argument; in other words, the
* newly created string is a copy of the argument string. Unless an
* explicit copy of {@code original} is needed, use of this constructor is
* unnecessary since Strings are immutable.
*
* @param original
* A {@code String}
*/
public String(String original) {
this.value = original.value;
this.hash = original.hash;
}
/**
* Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
*
* @return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
* <tt>false</tt>
*
* @since 1.6
*/
public boolean isEmpty() {
return value.length == 0;
}
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written initially into an ObjectOutputStream in the
* following format:
* <pre>
* <code>TC_STRING</code> (utf String)
* </pre>
* The String is written by method <code>DataOutput.writeUTF</code>.
* A new handle is generated to refer to all future references to the
* string instance within the stream.
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = toffset;
char pa[] = other.value;
int po = ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
if ((ooffset < 0) || (toffset < 0)
|| (toffset > (long)value.length - len)
|| (ooffset > (long)other.value.length - len)) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
// If characters don‘t match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
char[] val = value; /* avoid getfield opcode */
public boolean contentEquals(CharSequence cs) {
if (value.length != cs.length())
return false;
// Argument is a StringBuffer, StringBuilder
if (cs instanceof AbstractStringBuilder) {
char v1[] = value;
char v2[] = ((AbstractStringBuilder) cs).getValue();
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
// Argument is a String
if (cs.equals(this))
return true;
// Argument is a generic CharSequence
char v1[] = value;
int i = 0;
int n = value.length;
while (n-- != 0) {
if (v1[i] != cs.charAt(i))
return false;
i++;
}
return true;
}
在此只推荐了我所知道和常用一些小工具, 欢迎同学补充.
标签:
原文地址:http://blog.csdn.net/zjf280441589/article/details/49915379