Java string concat() sample code examples.

Java 8Object Oriented ProgrammingProgramming

<p style="">You can concatenate two strings in Java either by using the <strong>concat()</strong> method or by using the &lsquo;+&rsquo; , the &ldquo;concatenation&rdquo; operator.</p><h2 style="">The concat() method</h2><p>The concat() method appends one String to the end of another. This method returns a String with the value of the String passed into the method, appended to the end of the String, used to invoke this method.</p><h3 style="">Example</h3><pre class="prettyprint notranslate">public class ConncatSample { &nbsp; &nbsp;public static void main(String []args) { &nbsp; &nbsp; &nbsp; String s1 = &quot;Hello&quot;; &nbsp; &nbsp; &nbsp; String s2 = &quot;world&quot;; &nbsp; &nbsp; &nbsp; String res = s1.concat(s2); &nbsp; &nbsp; &nbsp; System.out.print(&quot;Concatenation result:: &quot;); &nbsp; &nbsp; &nbsp; System.out.println(res); &nbsp; &nbsp;} }</pre><h3 style="">Output</h3><pre class="result notranslate">Concatenation result:: Helloworld</pre>
Updated on 26-Feb-2020 05:18:41