Explore the English language on a new scale using AI-powered English language navigator.
StringsStrings appeared in every previous lesson, when application was performing output and it's time to examine them in detail. String is a reference typeRecently we've been looking over primitive types only and String is the first reference type to consider. Example to start withLet's start with an example. public class Strings { public static void main(String[] args) { String str; str = "Hello. I am a string!"; System.out.println(str); } } Hello. I am a string! Program stores "Hello. I am a string" text in a string and then reuses it in printing statement. What can one do with strings?String lengthTo ask for string length one should use accessor method length(): public class StringLength { public static void main(String[] args) { String str; str = "Hello."; int strLength; strLength = str.length(); System.out.println("Length of \"" + str + "\" is " + strLength + "characters"); } } Length of "Hello." is 6 characters Here we deal with methods for the first time. We will examine object-oriented paradigm in detail later, but for now it's important to know, that:
Concatenating stringsTo concatenate two strings one can use + operator: public class ConcatenatingStrings { public static void main(String[] args) { String str1, str2, str3; str1 = "Hello "; str2 = "world!"; str3 = str1 + str2; System.out.println(str3); } } Hello world! Actually, we used concatenation many times before in printing statements. Concatenations mixtureWhat if one wants to mix a string and an integer? Fortunately, it's still possible to use + operator: public class ConcatenationMixture { public static void main(String[] args) { String strVar; int intVar; strVar = "Babylon "; intVar = 5; String finalString; finalString = strVar + intVar; System.out.println(finalString); } } Babylon 5 Actually you can concatenate strings with any objects and primitive types variables. Mechanisms to do such mixed concatenations are beyond this lesson, but we will certainly discuss them in further articles. Comparing stringsStrings comparison has some nuances, which are to be discussed now. We want to discuss two types of comparison: checking for equality and determining lexicographic order. Testing for equalityStrings can't be compared using double equals operator, one should use method equals(String anotherStr) instead: public class EqualityTest { public static void main(String[] args) { String a, b; a = "abc"; b = "ab"; b += "c"; System.out.println("a: \"" + a + "\""); System.out.println("b: \"" + b + "\""); System.out.println("a == b: " + (a == b)); System.out.println("a.equals(b): " + a.equals(b)); } } a: "abc" b: "abc" a.equals(b): true Example shows, that double equals operator doesn't produce proper result in some cases. Why? As we told before, strings are objects. But it's possible, that two different objects contain the same string-value. That is the reason why comparison yields false, while testing using equals gives proper result. Determining lexicographic orderIn lexicographic order "a" < "b" < "c" < "c0". To compare strings one should use compareTo(String anotherStr) method. a.compareTo(b) returns following values:
public class ComparingStrings { public static void main(String[] args) { String a, b; a = "a"; b = "b"; System.out.println("Comparing \"a\" and \"b\": " + a.compareTo(b)); System.out.println("Comparing \"b\" and \"a\": " + b.compareTo(a)); System.out.println("Comparing \"a\" and \"a\": " + a.compareTo(a)); } } Comparing "a" and "b": -1 Comparing "b" and "a": 1 Comparing "a" and "a": 0 Last thing to know: how to input stringsBefore we finish the lesson, let's see how to input strings. For that purpose we'll use Scanner tool, you can remember from "number guessing game" practice. import java.util.Scanner;
public class InputStrings { public static void main(String[] args) { Scanner in = new Scanner(System.in); String name; System.out.print("Enter you name: "); name = in.nextLine(); System.out.println("Your name is " + name); } } Enter you name: John Your name is John Draw your attention to the first line of the sample. Scanner tool is unavailable, until one doesn't import it using keyword import. You can do it manually or, if using IDE, it will add import for you. Partners Ads
|