Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Concatenate Strings in a Java Program

In computer programming, a letter or digit is called a character and a collection of letters or digits that make a phrase, sentence, or paragraph, is called a string. Java uses the concatenate operator (+) to paste two or more strings together. Shown below is the Java code to concatenate the strings "first text " and "second text".

import javax.swing.*;

public class concatenateText 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Concatenate Text");

      JTextArea myText = new JTextArea("first text " + "second text");
      myText.setLineWrap(true);
      frame.add(myText);

      frame.setVisible(true);
   }
}

Concatenate Strings in Java

I generally hate it when people add a bunch of extra code to an example. It confuses things. But in this case I've included the javax.swing library and added code to create a JFrame and a JTextArea. If I didn't add this extra code, you would need to use the System.out.print command to output text, and I don't think that's an interesting or realistic way to learn Java programming. The important part of this code is the line:

JTextArea myText = new JTextArea("first text " + "second text");

Which is the line that is responsible for the actual display of the text. Note the use of the concatenate operator in the argument passed to the JTextArea.

If you wanted to experiment further, you could leave the JTextArea initializer empty and instead declare two String variables, then use the concatenate operator to paste the two strings together and initialize a third variable with the new string. Then use the JTextArea.append method to display the text in the program window, as shown below.

import javax.swing.*;

public class concatenateText2 
{

   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Concatenate Text");

      String firstPart = "first part ";
      String secondPart = "second part";
      String fullText = firstPart + secondPart;

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);
      myText.append(fullText);
      frame.add(myText);

      frame.setVisible(true);
   }
}

Note that, although in Java, the names of primitive variable types, like int and char begin with lower-case letters, the String variable type is capitalized. That's because strings are actually objects, which makes them very powerful because they contain methods like .length() and getChars.

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2018 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268