File size: 3,667 Bytes
33852e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1
00:00:00,000 --> 00:00:01,000
Hello team!
2
00:00:01,000 --> 00:00:04,000
In this lesson we will learn with you escape sequences in Java.
3
00:00:05,000 --> 00:00:08,000
Let me show you what is escape sequence and how to escape characters.
4
00:00:09,000 --> 00:00:14,000
I believe you already understood that all string literals need to be enclosed in quotation marks.
5
00:00:15,000 --> 00:00:19,000
But what to do in case you need to have string which will contain quotes in itself?
6
00:00:20,000 --> 00:00:23,000
In this example I want to put book name in quotes.
7
00:00:23,000 --> 00:00:29,000
To do this, I need to use character escaping by using a special symbol backslash.
8
00:00:29,000 --> 00:00:31,000
I can escape quotation marks.
9
00:00:32,000 --> 00:00:38,000
In Java, a backslash combined with a character to be escaped is called an escape sequence.
10
00:00:38,000 --> 00:00:43,000
In case I don't use backslash in this example, there will be compilation error.
11
00:00:43,000 --> 00:00:50,000
Java compiler is very smart program, but it will be hard for it to compile string in such format.
12
00:00:50,000 --> 00:00:56,000
Java compiler expects that each string is started with quote and ended with quote.
13
00:00:56,000 --> 00:01:02,000
That's why without escape sequence for Java compiler, it will be impossible to understand that you
14
00:01:02,000 --> 00:01:05,000
want to have this string as part of the whole string.
15
00:01:06,000 --> 00:01:08,000
Let's consider another example.
16
00:01:08,000 --> 00:01:14,000
You want to have string which will contain directory path on your computer and you need to use backslash.
17
00:01:14,000 --> 00:01:21,000
But Java compiler knows that in source code will use backslash only in case we want to create escape
18
00:01:21,000 --> 00:01:28,000
sequence, and in this case compiler simply cannot find escape sequence because you don't need to escape
19
00:01:28,000 --> 00:01:29,000
J character.
20
00:01:29,000 --> 00:01:32,000
That's why you need to escape your backslash character.
21
00:01:33,000 --> 00:01:38,000
And we have here double backslash in the source code but single backslash in the output.
22
00:01:39,000 --> 00:01:42,000
In Java there are limited amount of escape sequences.
23
00:01:42,000 --> 00:01:50,000
Here you can find all lists of escape sequences for example backslash and t inserts, tab character
24
00:01:50,000 --> 00:01:54,000
backslash and n inserts newline character.
25
00:01:54,000 --> 00:01:57,000
Here is an example with two lines in one string object.
26
00:01:58,000 --> 00:02:01,000
You see backslash and n here.
27
00:02:01,000 --> 00:02:03,000
And here is the output.
28
00:02:03,000 --> 00:02:06,000
Here is one line and the second line.
29
00:02:07,000 --> 00:02:11,000
I encourage you to try these escape sequences in your source code.
30
00:02:12,000 --> 00:02:19,000
Another important thing to know about escaping characters in Java Unicode table includes the symbols
31
00:02:19,000 --> 00:02:22,000
of almost every written language in the world.
32
00:02:22,000 --> 00:02:28,000
In case you need to use Unicode character in your string, you can just escape Unicode code.
33
00:02:28,000 --> 00:02:34,000
All Unicode characters codes have the form U plus hexadecimal number.
34
00:02:34,000 --> 00:02:41,000
So if you need to use character from Unicode table, just escape this value and you will see copyright
35
00:02:41,000 --> 00:02:42,000
character in the output.
36
00:02:43,000 --> 00:02:44,000
That's all.
37
00:02:44,000 --> 00:02:47,000
Thank you for your attention and see you in the next lesson.
|