-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpecialCharacter.java
More file actions
executable file
·62 lines (51 loc) · 2.84 KB
/
SpecialCharacter.java
File metadata and controls
executable file
·62 lines (51 loc) · 2.84 KB
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
package chapter_01.part_02;
// SPECIAL CHARACTER IN JAVA
/*
Because strings must or can be written within quotes, Java may misunderstand certain characters, leading to errors
To include special characters within strings, Java provides escape characters, which are preceded by a backslash (\):
Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash
\n Newline Inserts a newline
\t Tab Inserts a tab
\r Carriage Inserts a carriage return
The sequence \" inserts a double quote in a string:
*/
public class SpecialCharacter {
public static void main(String[] args){
String text = "We are the so-called \"Vikings\" from the north.";
System.out.println(text);
// The sequence \' inserts a single quote in a string:
String question = "It\'s okay";
System.out.println(question);
// The sequence \\ inserts a single backslash in a string:
String backslash = "The character \\ is called backslash.";
System.out.println(backslash);
// Newline and Tab examples:
String multiLine = "Line 1\nLine 2\nLine 3";
System.out.println("Multi-line text:\n" + multiLine);
String tabbedText = "Name:\tJohn\nAge:\t25";
System.out.println("Tabbed text:\n" + tabbedText);
// Table for visual representation of special characters:
System.out.println("\nSpecial Character Table:");
System.out.println("+------------------+-------------------------+");
System.out.println("| Escape character | Result |");
System.out.println("+------------------+-------------------------+");
System.out.println("| \\' | ' |");
System.out.println("| \\\" | \" |");
System.out.println("| \\\\ | \\ |");
System.out.println("| \\n | Newline |");
System.out.println("| \\t | Tab |");
System.out.println("| \\r | Carriage |");
System.out.println("+------------------+--------------------------+");
}
}
/*
Java uses a special notation to represent special characters, as
shown in the table above. This special notation, called an escape sequence, consists of a backslash
(\) followed by a character or a combination of digits. For example, \t is an escape sequence
for the Tab character and an escape sequence such as \u03b1 is used to represent a Unicode.
The symbols in an escape sequence are interpreted as a whole rather than individually. An
escape sequence is considered as a single character.
*/