

{"id":123483,"date":"2024-10-05T18:00:34","date_gmt":"2024-10-05T12:30:34","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123483"},"modified":"2024-10-05T18:25:03","modified_gmt":"2024-10-05T12:55:03","slug":"java-string-concat-method","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/","title":{"rendered":"Java String concat() Method with Examples"},"content":{"rendered":"<p>The Java concat() method is handy for putting strings together. It helps us create dynamic text in our programs. In this article, we&#8217;ll take a close look at how the concat() method works, and we&#8217;ll show you some practical examples.<\/p>\n<p>The Java String concat() method joins two strings to make a new one. It&#8217;s great for when you want to add one string to another.<\/p>\n<p>For example, you can use it to combine a first name and a last name to make a full name or to mix text with data from a database. When you understand how to use concat(), you have more control over the text in your programs.<\/p>\n<h2>Method Description<\/h2>\n<p><strong>The Java\u00a0 concat() method description is defined as follows:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String concat(String str)<\/pre>\n<p>It takes in a single String parameter and returns a new String. The original String that concat() is called on remains unchanged or immutable. The new String returned combines the original String and the String parameter passed in.<\/p>\n<p>Under the hood, the concat() method creates a new StringBuilder, appends the string parameters, and converts the StringBuilder to a String. We&#8217;ll explore this further in the next section.<\/p>\n<h3>Internal Implementation<\/h3>\n<p><strong>Here is a snippet from the Java OpenJDK source code showing how concat() is implemented:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public String concat(String str) {\r\n    int otherLen = str.length();\r\n    if (otherLen == 0) {\r\n        return this;\r\n    }\r\n    int len = value.length;\r\n    char buf[] = Arrays.copyOf(value, len + otherLen);\r\n    str.getChars(buf, len);\r\n    return new String(buf, true);\r\n}<\/pre>\n<p>As we can see, a new character array is created with enough capacity to hold both strings. The contents of the original string are copied over first using System.arraycopy(). Then, the contents of the parameter string are copied into the array starting at the end of the original string. This new array with the combined contents is passed to the String constructor to create the final concatenated string.<\/p>\n<p>Following this process, strings remain immutable while allowing new String instances to be created efficiently under the hood.<\/p>\n<h3>Java String concat() Method Examples<\/h3>\n<p>Let&#8217;s explore some code examples to demonstrate how concat() can be applied.<\/p>\n<p><strong>Example 1:<\/strong> Immutable Nature<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringConcatenationExample {\r\n    public static void main(String[] args) {\r\n        String name = \"John\";\r\n        String fullName = name.concat(\" Doe\");\r\n\r\n        System.out.println(name); \/\/ Output: John\r\n        System.out.println(fullName); \/\/ Output: John Doe\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nJohn<br \/>\nJohn Doe<\/p>\n<p>This example shows the immutable nature of strings in Java. Calling concat() on the name string does not change the name. It returns a new String, leaving the name unchanged.<\/p>\n<p><strong>Example 2:<\/strong> Concatenating Multiple Strings<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringConcatenationExample {\r\n    public static void main(String[] args) {\r\n        String firstName = \"John \";\r\n        String lastName = \"Doe\";\r\n        \r\n        String fullName = firstName.concat(lastName);\r\n        \r\n        fullName = fullName.concat(\" - Java Developer\");\r\n        \r\n        System.out.println(fullName);\r\n    }\r\n}<\/pre>\n<p><strong>Output: <\/strong><br \/>\n<strong>John Doe &#8211;<\/strong> Java Developer<\/p>\n<p>Here, we combine multiple strings by calling concat() numerous times and chaining the method. No matter how many concatenations we perform, the original strings remain unchanged.<\/p>\n<p><strong>Example 3:<\/strong> Chaining with Spaces and Special Characters<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringConcatenationExample {\r\n    public static void main(String[] args) {\r\n        String start = \"Hello\";\r\n        String mid = \"world\";\r\n        String end = \"!\";\r\n        \r\n        String message = start.concat(\" \").concat(mid).concat(\". \").concat(end);\r\n        \r\n        System.out.println(message);  \r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello world. !<\/p>\n<p>This example shows we can combine concat() calls to insert strings with spaces, punctuation, or other special characters.<\/p>\n<p><strong>Example 4:<\/strong> Appending at the Beginning<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class StringConcatenationExample {\r\n    public static void main(String[] args) {\r\n        String name = \"John\";\r\n        String greeting = \"Hello \".concat(name);\r\n        \r\n        System.out.println(greeting);  \r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><br \/>\nHello John<br \/>\nBy putting the append string first, we can use concat() to efficiently prepend a string rather than just append.<\/p>\n<h3>Conclusion<\/h3>\n<p>The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances. As demonstrated through examples of appending, prepending, and chaining strings with spaces and punctuation, concat() provides an efficient way to build dynamic strings from immutable components. By understanding concat()&#8217;s signature, behavior, and use cases, developers gain more control over crafting precise textual output in their programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Java concat() method is handy for putting strings together. It helps us create dynamic text in our programs. In this article, we&#8217;ll take a close look at how the concat() method works, and&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":134222,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[31149,7345,31148,31120,31147,31078,8152,31118,31146],"class_list":["post-123483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-concat-method-in-java","tag-java","tag-java-concat-method","tag-java-string-concat-method","tag-java-string-concat-method-with-examples","tag-java-tutorials","tag-learn-java","tag-string-concat-method","tag-string-concat-method-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java String concat() Method with Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/\" \/>\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 - DataFlair\" \/>\n<meta property=\"og:description\" content=\"The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-05T12:30:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-05T12:55:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.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=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\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 - DataFlair","description":"The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances.","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:\/\/data-flair.training\/blogs\/java-string-concat-method\/","og_locale":"en_US","og_type":"article","og_title":"Java String concat() Method with Examples - DataFlair","og_description":"The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances.","og_url":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-10-05T12:30:34+00:00","article_modified_time":"2024-10-05T12:55:03+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Java String concat() Method with Examples","datePublished":"2024-10-05T12:30:34+00:00","dateModified":"2024-10-05T12:55:03+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/"},"wordCount":518,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.webp","keywords":["concat() method in java","Java","java concat() method","java string concat() method","java string concat() method with examples","java tutorials","Learn Java","string concat() method","string concat() method in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/","url":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/","name":"Java String concat() Method with Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.webp","datePublished":"2024-10-05T12:30:34+00:00","dateModified":"2024-10-05T12:55:03+00:00","description":"The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/java-string-concat-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/string-concat.webp","width":1200,"height":628,"caption":"string concat()"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/java-string-concat-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Java Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Java String concat() Method with Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123483"}],"version-history":[{"count":5,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123483\/revisions"}],"predecessor-version":[{"id":143517,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123483\/revisions\/143517"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/134222"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}