{"id":89039,"date":"2025-02-17T18:00:48","date_gmt":"2025-02-17T12:30:48","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89039"},"modified":"2025-02-17T18:15:26","modified_gmt":"2025-02-17T12:45:26","slug":"java-string-concatenation","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/","title":{"rendered":"Java String concat() Method with Examples"},"content":{"rendered":"<p>The concatenation function in Java makes a new string composed of multiple strings. There are two ways of combining strings in Java: The +string operator is used. Use the concat() method.<\/p>\n<h3>String Concatenation by + (String concatenation) operator<\/h3>\n<p>The Java string concatenation function is used to insert strings.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">classTechvidvan{  \r\npublic static void main(String args[]){  \r\nString s=\"Sachin\"+\" Tendulkar\";  \r\nSystem.out.println(s);\r\n }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nSachin Tendulkar<\/p>\n<h3>Concatenation of strings using the concat() method<\/h3>\n<p>The string concat() method concatenates a given string to the end of an existing string.<\/p>\n<p><strong>Syntax:<\/strong> public String concatString other<\/p>\n<h3>How to concatenate strings in Java<\/h3>\n<ul>\n<li>Concatenation is combining two or more strings to form a new string by appending the next string to the end of the previous strings.<\/li>\n<li>In Java, two strings can be concatenated using the + or += operator or the concat() method, defined in the java.lang.string class.<\/li>\n<li>This shot will discuss how to perform string concatenation using both methods.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p>The program below demonstrates how to concatenate two strings using the + operator in Java.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Techvidvan{\r\npublic static void main( String args[] )\r\n{ \r\nString first = \"Hello\";\r\nString second = \"World\"; \r\nString third = first + second; \r\nSystem.out.println(third); \r\n\/\/ yet another way to concatenate strings \r\nfirst += second;\r\n System.out.println(first);\r\n }<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHelloWorld<br \/>\nHelloWorld<\/p>\n<h4>Parameter:<\/h4>\n<p>stringToConcat is the string to be concatenated. It will be appended at the end of the string object calling this method.<\/p>\n<h4>Return type:<\/h4>\n<p>The concat() method returns the concatenated string.<\/p>\n<h3>String concatenation using StringBuilder class<\/h3>\n<ul>\n<li>To perform concatenation operations, StringBuilder is a class that provides an append() method.<\/li>\n<li>Arguments of other types, such as objects, string builders, int, char, CharSequence, boolean, float, and double, are accepted by the append() method.<\/li>\n<li>The most popular and fastest way to concatenate strings in Java is StringBuilder.<\/li>\n<li>It is an immutable class, which means you can update or change the values \u200b\u200bin the StringBuilder object.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan\r\n{   \r\npublic static void main(String args[])  \r\n{  \r\nStringBuilder s1 = new StringBuilder(\"Hello\");    \r\nStringBuilder s2 = new StringBuilder(\" World\");   \r\nStringBuilder s = s1.append(s2);  \r\nSystem.out.println(s.toString());   \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello World<\/p>\n<p>s1, s2 and s are defined as StringBuilder objects by the code snippet above. Using an append method, S will store the result by concatenating s1 and s2.<\/p>\n<h3>Concatenation of strings using the format() method<\/h3>\n<p>The String.format() method can combine multiple strings with a format specification such as %s followed by string values or objects.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan\r\n{  \r\npublic static void main(String args[])  \r\n{  \r\nString s1 = new String(\"Hello\");      \r\nString s2 = new String(\" World\");      \r\nString s = String.format(\"%s%s\",s1,s2);\r\nSystem.out.println(s.toString());   \r\n}  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello World<\/p>\n<p>Here, the String s objects are assigned the concatenated result of the strings s1 and s2 using the String.format() method. format() accepts parameters as a format specifier followed by objects or String values.<\/p>\n<h3>Concatenate strings using the String.join() method (Java version 8+)<\/h3>\n<p>The String.join() method is available in Java version 8 and all versions above. The String.join() method accepts arguments, first a delimiter and an array of String objects.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan{ \r\n    public static void main(String args[])  \r\n    {  \r\n        String s1 = new String(\"Hello\");      \r\n        String s2 = new String(\" World\");   \r\n        String s = String.join(\"\",s1,s2);   \r\n        System.out.println(s.toString());   \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello World<\/p>\n<p>The result of the String.join &#8220;&#8221;,s1,s2&#8243; method will be stored by a string object s in this code snippet. The delimiter is enclosed in a quotation mark next to the string object or array of string objects.<\/p>\n<h3>String concatenation using StringJoiner class (Java Version 8+)<\/h3>\n<p>StringJoiner class has all the functionalities of String.join() method. In advance, its constructor can also accept optional arguments, prefixes and suffixes.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class Techvidvan{   \r\n    public static void main(String args[])  \r\n    {  \r\n        StringJoiner s = new StringJoiner(\", \");    \r\n        s.add(\"Hello\");     \r\n        s.add(\"World\");    \r\n        System.out.println(s.toString());  \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello, World<\/p>\n<p>A StringJoiner item s is declared in this code snippet, and the stringjoiner constructor takes a separator value. Inside the quotation marks, a separator is indicated. The add() method will append the Strings passed as arguments.<\/p>\n<h3>String concatenation using the Collectors.joining (JavaVersion 8+)<\/h3>\n<p>The join() function of the Collectors class in Java 8 provides input elements to be grouped into a similar order as they are.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.*;  \r\nimport java.util.stream.Collectors;  \r\npublic class Techvidvan \r\n{  \r\n     public static void main(String args[])  \r\n    {  \r\n        List&lt;String&gt; liststr = Arrays.asList(\"abc\", \"pqr\", \"xyz\");\r\n       String str = liststr.stream().collect(Collectors.joining(\", \"));                   System.out.println(str.toString());    \r\n    }  \r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>abc, pqr, xyz<\/p>\n<p>You have a list of string fields. The result of the Collectors.joining() method is held in a String object.<\/p>\n<h3>Conclusion<\/h3>\n<p>A brief description of Java string concatenation is provided in this article. We&#8217;ve also discussed using concat() and &#8220;+&#8221; operators for strings. We&#8217;ve also compared the concat() method and the &#8220;+&#8221; operator and how to choose one to use in different contexts.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The concatenation function in Java makes a new string composed of multiple strings. There are two ways of combining strings in Java: The +string operator is used. Use the concat() method. String Concatenation by&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447266,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[296,5408,5409,467,263,327,5537,5411,5412,469],"class_list":["post-89039","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java","tag-java-string-concat-method","tag-java-string-concat-method-with-examples","tag-java-string-concatenation","tag-java-tutorial-for-beginners","tag-java-tutorials","tag-string-concantenation-in-java","tag-string-concat-method","tag-string-concat-method-in-java","tag-string-concatenation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String concat() Method with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java String concat() Method with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-02-17T12:30:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-17T12:45:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java String concat() Method with Examples - TechVidvan","description":"Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/","og_locale":"en_US","og_type":"article","og_title":"Java String concat() Method with Examples - TechVidvan","og_description":"Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2025-02-17T12:30:48+00:00","article_modified_time":"2025-02-17T12:45:26+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Java String concat() Method with Examples","datePublished":"2025-02-17T12:30:48+00:00","dateModified":"2025-02-17T12:45:26+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/"},"wordCount":612,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp","keywords":["java","java string concat() method","java string concat() method with examples","java string concatenation","java tutorial for beginners","java tutorials","string concantenation in java","string concat() method","string concat() method in java","string concatenation"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/","url":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/","name":"Java String concat() Method with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp","datePublished":"2025-02-17T12:30:48+00:00","dateModified":"2025-02-17T12:45:26+00:00","description":"Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/12\/string-concantenation-in-java.webp","width":1200,"height":628,"caption":"string concantenation in java"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-string-concatenation\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java String concat() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=89039"}],"version-history":[{"count":3,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89039\/revisions"}],"predecessor-version":[{"id":447802,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89039\/revisions\/447802"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447266"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}