📄 3.doc.html
字号:
<a name="231016"></a>'%'
<a name="230975"></a>'\t'
<a name="231018"></a>'\\'
<a name="231020"></a>'\''
<a name="231024"></a>'\u03a9'
<a name="231026"></a>'\uFFFF'
<a name="230984"></a>'\177'
<a name="231030"></a>'<img src="chars/capomega.gif">'
<a name="231032"></a>'<img src="chars/circmult.gif">'
</pre><a name="230985"></a>
Because Unicode escapes are processed very early, it is not correct to write <code>'\u000a'</code> for a character literal whose value is linefeed (LF); the Unicode escape <code>\u000a</code> is transformed into an actual linefeed in translation step 1 <a href="3.doc.html#100850">(§3.3)</a> and the linefeed becomes a <i>LineTerminator</i> in step 2 <a href="3.doc.html#231571">(§3.4)</a>, and so the character literal is not valid in step 3. Instead, one should use the escape sequence <code>'\n'</code> <a href="3.doc.html#101089">(§3.10.6)</a>. Similarly, it is not correct to write <code>'\u000d'</code> for a character literal whose value is carriage return (CR). Instead, use <code>'\r'</code>.<p>
<a name="229744"></a>
In C and C++, a character literal may contain representations of more than one character, but the value of such a character literal is implementation-defined. In Java, a character literal always represents exactly one character.<p>
<a name="101083"></a>
<h3>3.10.5 String Literals</h3>
<a name="101084"></a>
A <i>string literal</i> consists of zero or more characters enclosed in double quotes.
Each character may be represented by an escape sequence.
<p><a name="229653"></a>
A string literal is always of type <code>String</code> (<a href="4.doc.html#26992">§4.3.3</a>, <a href="javalang.doc11.html#14460">§20.12</a>). A string literal always refers to the same instance <a href="4.doc.html#12028">(§4.3.1)</a> of class <code>String</code>.<p>
<ul><pre>
<i>StringLiteral:<br>
</i> <code>" </code><i>StringCharacters</i><sub><i>opt</i></sub><code> "
</code>
<i>StringCharacters:<br>
</i> <i>StringCharacter<br>
</i> <i>StringCharacters</i><code> </code><i>StringCharacter
</i>
<i>StringCharacter:<br>
</i><code> </code><i>InputCharacter</i> but not <code>"</code> or <code>\<br>
</code><i>EscapeSequence
</i></pre></ul><a name="22983"></a>
The escape sequences are described in <a href="3.doc.html#101089">§3.10.6</a>.
<p><a name="19350"></a>
As specified in <a href="3.doc.html#231571">§3.4</a>, neither of the characters CR and LF is ever considered to be an <i>InputCharacter</i>; each is recognized as constituting a <i>LineTerminator</i>.<p>
<a name="21405"></a>
It is a compile-time error for a line terminator to appear after the opening <code>"</code> and before the closing matching <code>"</code>. A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator <code>+</code> <a href="15.doc.html#39990">(§15.17.1)</a>.<p>
<a name="229156"></a>
The following are examples of string literals:<p>
<pre><a name="229157"></a>
"" // the empty string
<a name="229158"></a>"\"" // a string containing " alone
<a name="229159"></a>"This is a string" // a string containing 16 characters
<br><a name="229160"></a>"This is a " + // actually a string-valued constant expression,
<a name="229161"></a> "two-line string" // formed from two string literals
</pre><a name="25884"></a>
Because Unicode escapes are processed very early, it is not correct to write <code>"\u000a"</code> for a string literal containing a single linefeed (LF); the Unicode escape <code>\u000a</code> is transformed into an actual linefeed in translation step 1 <a href="3.doc.html#100850">(§3.3)</a> and the linefeed becomes a <i>LineTerminator</i> in step 2 <a href="3.doc.html#231571">(§3.4)</a>, and so the string literal is not valid in step 3. Instead, one should write <code>"\n"</code> <a href="3.doc.html#101089">(§3.10.6)</a>. Similarly, it is not correct to write <code>"\u000d"</code> for a string literal containing a single carriage return (CR). Instead use <code>"\r"</code>.<p>
<a name="19369"></a>
Each string literal is a reference <a href="4.doc.html#9317">(§4.3)</a> to an instance (<a href="4.doc.html#12028">§4.3.1</a>, <a href="12.doc.html#44670">§12.5</a>) of class <code>String</code> (<a href="4.doc.html#26992">§4.3.3</a>, <a href="javalang.doc11.html#14460">§20.12</a>). <code>String</code> objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions <a href="15.doc.html#5313">(§15.27)</a>-are "interned" so as to share unique instances, using the method <code>String.intern</code> <a href="javalang.doc11.html#14026">(§20.12.47)</a>.<p>
<a name="231331"></a>
Thus, the test program consisting of the compilation unit <a href="7.doc.html#40031">(§7.3)</a>:<p>
<pre><br><a name="23253"></a>package testPackage;
<br></pre><pre><a name="229782"></a>
class Test {
<a name="23254"></a> public static void main(String[] args) {
<a name="23255"></a> String hello = "Hello", lo = "lo";
<a name="229801"></a> System.out.print((hello == "Hello") + " ");
<a name="229787"></a> System.out.print((Other.hello == hello) + " ");
<a name="229789"></a> System.out.print((other.Other.hello == hello) + " ");
<a name="23837"></a> System.out.print((hello == ("Hel"+"lo")) + " ");
<a name="23845"></a> System.out.print((hello == ("Hel"+lo)) + " ");
<a name="23275"></a> System.out.println(hello == ("Hel"+lo).intern());
<a name="23276"></a> }
<a name="23260"></a>}
<br><a name="229785"></a>class Other { static String hello = "Hello"; }
</pre><a name="229778"></a>
and the compilation unit:
<p><pre><br><a name="229779"></a>package other;
<br><a name="229773"></a>public class Other { static String hello = "Hello"; }
<br></pre><a name="23261"></a>
produces the output:
<p><pre><a name="50713"></a>true true true true false true
</pre><a name="50715"></a>
This example illustrates six points:
<p><ul><a name="229805"></a>
<li>Literal strings within the same class <a href="8.doc.html#3857">(§8)</a> in the same package <a href="7.doc.html#34412">(§7)</a> represent references to the same <code>String</code> object <a href="4.doc.html#12028">(§4.3.1)</a>.
<a name="229807"></a>
<li>Literal strings within different classes in the same package represent references to the same <code>String</code> object.
<a name="229797"></a>
<li>Literal strings within different classes in different packages likewise represent references to the same <code>String</code> object.
<a name="23858"></a>
<li>Strings computed by constant expressions <a href="15.doc.html#5313">(§15.27)</a> are computed at compile time and then treated as if they were literals.
<a name="23859"></a>
<li>Strings computed at run time are newly created and therefore distinct.
<a name="23865"></a>
<li>The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
</ul><a name="101089"></a>
<h3>3.10.6 Escape Sequences for Character and String Literals</h3>
<a name="230359"></a>
The character and string <i>escape sequences</i> allow for the representation of some
nongraphic characters as well as the single quote, double quote, and backslash
characters in character literals <a href="3.doc.html#100960">(§3.10.4)</a> and string literals <a href="3.doc.html#101083">(§3.10.5)</a>.
<p><ul><pre>
<i>EscapeSequence:<br>
</i><code> \ b /* \u0008: </code>backspace<code> </code>BS<code> </code> <code>*/<br>
\ t /* \u0009: </code>horizontal tab<code> </code>HT <code> */<br>
\ n /* \u000a: </code>linefeed<code> </code>LF <code> */<br>
\ f /* \u000c: </code>form feed<code> </code>FF <code> */<br>
\ r /* \u000d: </code>carriage return<code> </code>CR <code> */<br>
\ " /* \u0022: </code>double quote <code>" */<br>
\ ' /* \u0027: </code>single quote <code>' */<br>
\ \ /* \u005c: </code>backslash <code>\ */<br>
</code><i>OctalEscape</i><code> /* \u0000</code> to <code>\u00ff: </code>from octal value<code> */
</code>
<i>OctalEscape:<br>
</i> <code>\ </code><i>OctalDigit<br>
</i><code> \ </code><i>OctalDigit</i><code> </code><i>OctalDigit<br>
</i><code> \ </code><i>ZeroToThree</i><code> </code><i>OctalDigit</i><code> </code><i>OctalDigit
</i>
<i>OctalDigit:</i> <i>one</i> <i>of<br>
</i> <code>0 1 2 3 4 5 6 7
</code>
<i>ZeroToThree:</i> <i>one</i> <i>of<br>
</i> <code>0 1 2 3
</code></pre></ul><a name="230722"></a>
It is a compile-time error if the character following a backslash in an escape is not an ASCII <code>b</code>, <code>t</code>, <code>n</code>, <code>f</code>, <code>r</code>, <code>"</code>, <code>'</code>, <code>\</code>, <code>0</code>, <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>, <code>6</code>, or <code>7</code>. The Unicode escape <code>\u</code> is processed earlier <a href="3.doc.html#100850">(§3.3)</a>. (Octal escapes are provided for compatibility with C, but can express only Unicode values <code>\u0000</code> through <code>\u00FF</code>, so Unicode escapes are usually preferred.)<p>
<a name="230717"></a>
<h3>3.10.7 The Null Literal</h3>
<a name="24276"></a>
The null type has one value, the null reference, represented by the literal <code>null</code>,
which is formed from ASCII characters. A <i>null literal</i> is always of the null type.
<p><ul><pre>
<i>NullLiteral:<br>
</i><code> null
</code></pre></ul><a name="230752"></a>
<h2>3.11 Separators</h2>
<a name="230757"></a>
The following nine ASCII characters are the Java <i>separators </i>(punctuators):
<p><ul><pre>
<i>Separator:</i> <i>one</i> <i>of<br>
</i> <code>( ) { } [ ] ; , .
</code></pre></ul><a name="230663"></a>
<h2>3.12 Operators</h2>
<a name="230669"></a>
The following 37 tokens are the Java <i>operators</i>,<i> </i>formed from ASCII characters:
<p><ul><pre>
<i>Operator:</i> <i>one</i> <i>of<br>
</i><code> = > < ! ~ ? :<br>
== <= >= != && || ++ --<br>
+ - * / & | ^ % << >> >>><br>
+= -= *= /= &= |= ^= %= <<= >>= >>>=
</code></pre></ul><a name="29585"></a>
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="2.doc.html">Prev</a> | <a href="4.doc.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -