String byron = new String ("She walks in beauty, like the night, \nOf cloudless climes and starry skies\nAnd all that's best of dark and bright\nMeet in her aspect and her eyes.") Ĭonsole output: She walks in beauty, like the night, Thus, if the compiler encounters \n in the text, it understands that this is not just a symbol and a letter to display on the console, but rather a special command to "move to a new line!".įor example, this may be useful if we want to display part of a poem: \b - backspace (a step backward in the text or deletion of a single character).Java has quite a lot of control sequences. String workFiles= new String ("My work files are in D:\\Work Projects\\java") Ĭonsole output: My work files are in D:\Work Projects\javaSuper! The compiler immediately determines that the \ are ordinary characters that should be displayed along with the rest. What should we do? Exactly the same thing as before: we just add another \ to our \! After all, the compiler doesn't know \ as anything other than a control sequence! It expects the backslash to be followed by a certain character that it must somehow interpret in a special way (such as a quotation mark).īut, in this case, \ is followed by ordinary letters. Once again, the compiler doesn't understand what to do. String workFiles= new String ("My work files are in D:\Work Projects\java") Suppose we want to tell someone about our work: Quotation marks are by no means the only characters we may need to escape. Let's try running the main() method.Ĭonsole output: My favorite book is "Twilight" by Stephanie MeyerExcellent! The code worked exactly how we wanted it to! We've used \ to escape our two "internal" quotation marks. String myFavoriteBook = new String ("My favorite book is \"Twilight\" by Stephanie Meyer") Let's try changing our code with the book: Upon encountering this construct in your code, the compiler will understand that this is just a quotation mark that should be displayed on the screen. In Java, a backslash combined with a character to be "escaped" is called a control sequence.įor example, \" is a control sequence for displaying quotation marks on the screen. This symbol is normally called "backslash". This is accomplished using a special symbol: \. To do this, Java uses character escaping. We need to let the compiler know when a quotation mark is a command ("create a string!") and when it is simply a character ("display the word "Twilight" along with quotation marks!"). "Another quotation mark? Is this some kind of mistake? I'm already creating a string! Or should I create another one? Argh!.:/" Here's why: simply speaking, the compiler gets confused about what it is expected to do. But that's what the compiler is already doing! The quotation mark suggests that a string must be created. When the compiler reaches this piece of text, it simply doesn't understand what it is expected to do. In our case, the quotation marks around the word "Twilight" are inside other quotation marks. And every time the compiler sees ", it expects that the quotation mark will be followed by a second quotation mark, and that the content between them is the text of a string to be created by the compiler. it expects strings to be wrapped in them. The compiler interprets quotation marks in a very specific way, i.e. What do you think the problem could be? And what does it have to do with quotation marks? It seems the compiler is unhappy about something! String myFavoriteBook = new String ("My favorite book is "Twilight" by Stephanie Meyer") I'm 20!") īut what do we do if we need to create a string that itself must contain quotation marks? For example, suppose we want to tell the world about your favorite book: String alex = new String ("My name is Alex. The main thing when creating a string is that the entire sequence must be enclosed in quotation marks: These characters can be any letters, numerals, punctuation marks and so on. Hi! In previous lessons, we've already gotten acquainted with text strings, which are represented by the String class in Java.Īs you probably remember, a string is a sequence of characters.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |