-
Notifications
You must be signed in to change notification settings - Fork 0
Strings
#What is a String?
A string is a basically a sequence of characters, words or phrases. In Java a string is a built in class that can be used to store those things. 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"
Syntax:
System.out.println("Hello World: " + "Welcome to Java");
Result:
Hello World: Welcome to Java
For the code above I have two different strings. One says "Hello World: " and "Welcome to Java" to concatinate this strings all I did was I place the plus sign between the two strings and the output became, "Hello World: Welcome to Java".
Note: Notice how I put the space at the end on Hello World this is becuase both of the strings above are what are called literal strings. This means that they are stored in memory EXACTLY how you input them. If I removed the space and then combined the strings that the output would be, "Hello World:Welcome to Java". #Using Strings to store data
Not only can strings be used to display text and strings they can also be used to store data. To do this you would declare and initializing the string. This basically means that you are creating the string and assigning a value to it. You do not have to initialize a string, or any other data type for that fact, but it is always a good idea to initialize a string or variable at the same time that you declare it. The reason you declare and initialize this way to avoid program crashes due to the variable not having a value.
To declare and initialize a string you would first call the string class. This tells the IDE that whatever follows this statement is the name of the string. Next you would give the string a name. This name can be whatever you want it to be. The final step is to initialize the string by setting it equal to a value using the assignment operator. All the assignment operator does is say the this "is equal to" that. Again you can set this value to be whatever you want it to be. Now if you've done everything correctly then congrats because you've just created your first string variable.
Example:
String greeting = "Hello";
#Screenshots