×

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String.

Syntax:

public String concat(String str)

Parameter:

Str: String to be concatenated at the end of current string.

Returns:

It returns Combined String.

Example 1: Concatenating Strings one by one

public class ConcatEx1{
public static void main(String args[]){
String s1,s2,s3="null";
s1="Tutorial";
System.out.println(s1);
s2=s1.concat("And");
System.out.println(s2);
s3=s2.concat("Example");
System.out.println(s3);
}
}

Output:

Tutorial
TutorialAnd
TutorialAndExample

Example 2: Concatenating multiple Strings

public class ConcatEx2 { 
    public static void main(String[] args) {     
        String str1 = "Welcome"; 
        String str2 = " to the"; 
        String str3 = " Tutorial And Example"; 
        // Concatenating multiple strings 
        String str5 = str1.concat(str2).concat(str3); 
        System.out.println(str5); 
    } 
}

Output:

Welcome to the Tutorial And Example

Example 3: concatenating spaces and special characters to the string object

public class ConcatEx3 { 
    public static void main(String[] args) {     
        String str1 = "welc".concat("ome");  
        System.out.println(str1);
        //adding the spaces
        String str3 = str1.concat(" ")+"to".concat(" ").concat("the").concat(" ").concat("tutorialandexample") ;
        System.out.println(str3);
        //adding the special character
        String str4 = str1.concat("!@#$%^&*()_+~{}:><?/*-+");
        System.out.println(str4);
    } 
}

Output:

welcome
welcome to the tutorialandexample
welcome!@#$%^&*()_+~{}:><?/*-+

Related Topics

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Class definition in Java

The class definition in Java Java is an object-oriented programming language. We essentially know that programming languages based on object-oriented paradigms have classes and objects in their concepts as main, which...

6 minutes read.

MOOD Factors to Assess a Java Program

In this tutorial, we will comprehendthe meaning of mood factors in Java. For the development of any software system,the quality of anapplication is important. It is more important to maintain large-scale...

4 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Tutorial

What is Java? Java is an object-oriented, robust, secured and platform-independent programming language. With the help of Java Programming, we can develop console, window, web, enterprise and mobile applications. Java language was...

22 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

Java Rename File

Renaming a file is the process of changing its name. Using the renameTo() function of the Java File class, renaming operations are possible. A file can be renamed using Java's renameTo()...

3 minutes read.

Two Decimal Places Java

When a double data type is used in Java before a variable, this indicates 15 digits after decimal point. However, there are situations when we only require 2 decimal places...

4 minutes read.