/**
* Returns a BigInteger whose value is (this mod m). This method
* differs from the remainder method in that it always returns a
* non-negative BigInteger.
*
* @param m the modulus, which must be positive
* @return this mod m
* @throws ArithmeticException if m is less than or equal to 0
*/publicBigIntegermod(BigInteger m){if(m.signum()<=0)thrownewArithmeticException("Modulus <= 0: "+ m);...// Do the computation}
publicfinalclassPeriod{privatefinalDate start;privatefinalDate end;/**
* @param start the beginning of the period
* @param end the end of the period; must not precede start
* @throws IllegalArgumentException if start is after end
* @throws NullPointerException if start or end is null
*/publicPeriod(Date start,Date end){if(start.compareTo(end)>0)thrownewIllegalArgumentException(start +" after "+ end);this.start = start;this.end = end;}publicDatestart(){return start;}publicDateend(){return end;}...// Remainder omitted}
publicclassSetList{publicstaticvoidmain(String[] args){Set<Integer> set =newTreeSet<>();List<Integer> list =newArrayList<>();for(int i =-3; i <3; i++){
set.add(i);
list.add(i);}for(int i =0; i <3; i++){
set.remove(i);
list.remove(i);}System.out.println(set +" "+ list);}}
staticintmin(int... args){if(args.length ==0)thrownewIllegalArgumentException("Too few arguments");int min = args[0];for(int i =1; i < args.length; i++)if(args[i]< min)
min = args[i];return min;}
privatefinalList<Cheese> cheesesInStock =...;/**
* @return a list containing all of the cheeses in the shop,
* or null if no cheeses are available for purchase.
*/publicList<Cheese>getCheeses(){return cheesesInStock.isEmpty()?null:newArrayList<>(cheesesInStock);}
publicstatic<EextendsComparable<E>>Emax(Collection<E> c){if(c.isEmpty())thrownewIllegalArgumentException("Empty collection");E result =null;for(E e : c)if(result ==null|| e.compareTo(result)>0)
result =Objects.requireNonNull(e);return result;}
publicstatic<EextendsComparable<E>>Optional<E>max(Collection<E> c){if(c.isEmpty())returnOptional.empty();E result =null;for(E e : c)if(result ==null|| e.compareTo(result)>0)
result =Objects.requireNonNull(e);returnOptional.of(result);}
/**
* Returns the element at the specified position in this list.
*
* <p>This method is <i>not</i> guaranteed to run in constant
* time. In some implementations it may run in time proportional
* to the element position.
*
* @param index index of element to return; must be
* non-negative and less than the size of this list
* @return the element at the specified position in this list
* @throws IndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= this.size()})
*/Eget(int index);
/**
* Returns true if this collection is empty.
*
* @implSpec
* This implementation returns {@code this.size() == 0}.
*
* @return true if this collection is empty
*/publicbooleanisEmpty(){...}
包含HTML元字符的文档,例如小于号(<),大于号(>)和 and 符号(&),是用{@literal}标签将它们包围起来:
注意概要描述是否包含句点,例如以「A college degree, such as B.S., M.S. or Ph.D.」会导致概要描述为「A college degree, such as B.S., M.S」。缩写「M.S.」中的第二个句号后面跟着一个空格。最好的解决方案是用{@literal}标签
/**
* A college degree, such as B.S., {@literal M.S.} or Ph.D.
*/publicclassDegree{...}
概要描述应该是一个动词短语,描述了该方法执行的操作。例如:
ArrayList(int initialCapacity) ——构造具有指定初始容量的空列表。
Collection.size() ——返回此集合中的元素个数。
对于类,接口和属性,概要描述应该是描述由类或接口的实例或属性本身表示的事物的名词短语。
Instant ——时间线上的瞬时点。
Math.PI ——更加接近pi的double类型数值,即圆的周⻓与其直径之比。
为泛型类型或方法写文档时,请务必记录所有类型参数:
/**
* An object that maps keys to values. A map cannot contain
* duplicate keys; each key can map to at most one value.
*
* (Remainder omitted)
*
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*/publicinterfaceMap<K,V>{...}
在记录枚举类型时,一定要记录常量:
/**
* An instrument section of a symphony orchestra.
*/publicenumOrchestraSection{/** Woodwinds, such as flute, clarinet, and oboe. */
WOODWIND,/** Brass instruments, such as french horn and trumpet. */
BRASS,/** Percussion instruments, such as timpani and cymbals. */
PERCUSSION,/** Stringed instruments, such as violin and cello. */
STRING;}
在为注解类型记录文档时,一定要记录任何成员:
/**
* Indicates that the annotated method is a test method that
* must throw the designated exception to pass.
*/@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)public@interfaceExceptionTest{/**
* The exception that the annotated test method must throw
* in order to pass. (The test is permitted to throw any
* subtype of the type described by this class object.)
*/Class<?extendsThrowable>value();}