Thursday, January 2, 2020
Java Syntax The String Literal
AString literal is a sequence of characters used by Java programmers to populate String objects or display text to a user. The characters could be letters, numbers or symbols and are enclosed within two quotation marks. For example, I live at 22b Baker Street! is aString literal. Although in your Java code you will be writing the text within the quotes, the Java compiler will interpret the characters as Unicode code points. Unicode is a standard that assigns all letters, numbers, and symbols a unique numerical code. This means that every computer will display the same character for each numerical code. This means that if you know the number values you can actually writeString literals using Unicode values: \u0049\u0020\u006C\u0069\u0076\u0065\u0020\u0061\u0074\u0020\u0032\u0032\u0042\u0020\u0042\u0061\u006B\u0065\u0072\u0020\u0053\u0074\u0072\u0065\u0065\u0074\u0021 represents the sameString value as I live at 22b Baker Street! but obviously its not as nice to write! Unicode and normal text characters can also be mixed. This is useful for characters you might not know how to type. For example, a character with an umlaut (e.g., Ãâ, Ãâ") as in Thomas Mà ¼ller plays for Germany. would be: Thomas M\u00FCller plays for Germany. To assign aString object a value just use a String literal: String text So does Dr Watson; Escape Sequences There are certain characters that you might want to include into aString literal which need to be identified to the compiler. Otherwise, it might get confused and not know what the String value is supposed to be. For example, imagine you want to put a quotation mark within a String literal: So my friend said, Its how big? This would confuse the compiler because it expects allString literals to begin and end with a quotation mark. To get around this we can use what is known as an escape sequence - these are characters which are preceded by a backslash (in fact youve already seen several if you look back at the Unicode character codes). For example, a quotation mark has the escape sequence: \ So theString literal above would be written: So my friend said, \Its how big?\ Now the compiler will come to the backslash and know the quotation mark is part of theString literal instead of its end point. If youre thinking ahead youre probably wondering but what if I want to have a backslash in my String literal? Well, thats easy - its escape sequence follows the same pattern - a backslash before the character: \\ Some of the escape sequences available dont actually print a character to the screen. There are times when you might want toà display some text split by a newline. For example: The first line. The second line. This can be done by using the escape sequence for the newline character: The first line.\nThe second line. Its a useful way to put a little bit of formatting into oneSting literal. There are several useful escape sequences worth knowing: \t is for inserting tabs into the literal\b inserts a backspace\n inserts a newline\r inserts a carriage return\ inserts a single quotation mark\ inserts a double quotation mark\\ inserts a backslash Example Java code can be found in the Fun With Strings Example Code.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.