Skip to content

Strings

trekitch edited this page Feb 1, 2017 · 22 revisions

#What is a String?

A string is a basically a sequence of characters. For example, "Hello my name is Tom" is a string. Strings are used all the time to do things from storing user input to just simply telling the user hello. The question is how do you write a string. There are multiple ways you can display a string one of the most common ways is to use a print line statement (abbrv. println), also known as a print statement. A println statement is simply method outputs what you tell is to. The syntax is as follows:

System.out.println("Hello World");

The code above simple outputs a statement that says Hello World. With a println statement whatever you put inside of the double quotes will be displayed in the command line. There are also different types of print statements. The two major statements are print and println. The difference between the two is that println, once it outputs the results, moves to the next line. Print on the other hand stays on the same line after it outputs. Print statements are used generally when you are taking user input or want the program to perform different actions on the same line.

#Concatenating Strings

Concatenation simply means to combine or put things together in a chain or series. To perform concatenation you would use a concatenation operator which is basically a symbol that tells the IDE to combine things. In Java the concatenation operator is the plus(+) symbol. Let's say that I wanted to combine two separate strings using the operator. All I would do is place the operator between the two strings.

Example:

Strings: "Hello World", "Welcome to Java"

System.out.println("Hello World " + "Welcome to Java");

Note: There will be more screenshot examples later in this section

Clone this wiki locally