string in java
A Java string is nothing more than a string like you’ve seen it countless times.
In the most famous program in the world, you have e.g. the string “Hello World” is displayed on the screen.
Why is that so important?
Well, even if we live in a multimedia world with sound, videos and 3D cinema, texts are still one of the most important means of communication.
What is a java string
Just think about it when you want to order a book from Amazon for the first time. The first thing you have to do is fill out a text form in which you have to enter your name, address and the information for the payment processing.
And how do we process texts?
Exactly! With strings! And strings are called strings in programming.
Speaking of payment processing.
Have you ever been annoyed about the long IBAN (International Bank Account Number) code, which no one can remember, but which you must always enter on your bank transfers?
Me, yes! However, I have to admit that it makes perfect sense. Did you know that all kinds of useful information is hidden in the IBAN number?
Here is a small example:
US 53 8706 3410 3547 7960 34
The country code is in places 1 to 2
The check digit is in places 3 to 4
The bank routing number is in positions 5 to 12
The account number is in positions 13 to 22
The IBAN therefore offers us ideal opportunities to let off steam with the Java strings.
How are strings processed?
As the name suggests, a string is a string of individual characters.
The Java string. An object or a standard data type?
To save a single character, there is the data type char. It is therefore an obvious idea to consider a character string as a java array with the data type char.
It is definitely possible! But is it fun? No!! Therefore we will not pursue this option any further.
We’ll use the String data type right away.
Let’s practice that! And declare two string variables, to which we both assign the value “Adam Noah”.
String person1 = “Adam Noah”;
String person2 = ” Adam Noah “;
It looks like we are assigning a value to a common standard data type like integer, double or char.
BEWARE!
The string data type is NOT a standard data type, but an object.
But we should have created the string object with new!
Correct! As a matter of fact! We would have too:
String person1 = new string (” Adam Noah “);
String person2 = new string (” Adam Noah “);
Here JAVA supports us with so-called string literals, so that we can initialize a string as if it were a standard data type.
But even when comparing a string, the object-oriented nature of a string is striking.
Compare Java strings
Guess what the following program snippet provides for an output.
if (person1 == person2) {
System.out.println (“The people are the same!”);
} else {
System.out.println (“The people are different!”);
}
The variables person1 and person2 both have the same value Adam Noah. Nevertheless, the program output is:
The people are different!
Why is that? Is the if query broken?
No! Fortunately not. But since a string is an object, the variables person1 and person2 only contain references to the locations where the instances are stored.
However, because we created two objects and thus reserved two independent memory locations, these references are different.
The fact that these objects happen to have the same content is irrelevant for the comparison operator ==, because it only compares the references stored in the variables and they are different, which is why the condition in the if query is not met.
To check the contents of the string objects for equality, there is the object method equals. This method expects the string to be compared with as a parameter.
if (str1.equals(str2)){
System.out.println(“equal”);
}
else
System.out.println(“not equal”);