Here you can find the source of concat(Object... chunks)
Parameter | Description |
---|---|
chunks | the chunks to be concatenated |
public static String concat(Object... chunks)
//package com.java2s; public class Main { /**//from w w w .j a v a 2s. c om * Efficiently concatenates the string representations of the * specified objects. * * @param chunks the chunks to be concatenated * @return the result of the concatenation */ public static String concat(Object... chunks) { return join(null, chunks); } /** * Efficiently combines the string representations of the specified * objects, delimiting each chunk with the specified delimiter. * * @param delimiter the specified delimiter * @param chunks the chunks to be combined * @return the result of the concatenation */ public static String join(String delimiter, Object... chunks) { //It's worth doing two passes here to avoid additional allocations, by //being more accurate in size estimation int nChunks = chunks.length; int delimLength = delimiter == null ? 0 : delimiter.length(); int estimate = delimLength * (nChunks - 1); for (Object chunk : chunks) { if (chunk != null) { estimate += chunk instanceof CharSequence ? ((CharSequence) chunk) .length() : 10; /* why not? */ } } StringBuilder sb = new StringBuilder(estimate); for (int i = 0; i < nChunks; i++) { Object chunk = chunks[i]; if (chunk != null) { String string = chunk.toString(); if (string != null && string.length() > 0) { sb.append(string); if ((i + 1) < nChunks && delimiter != null) { sb.append(delimiter); } } } } return sb.toString(); } }