简体   繁体   English

撇号在Java中是什么意思

[英]What do apostrophes mean in java

in the book i'm learning from i came across this code snippit: 在我从中学习的书中,我遇到了以下代码片段:

while (i < len) { 
char c = s.charAt(i); 
if (c == ’(’) { 
count = count + 1; 
} else if (c == ’)’) { 
count = count - 1; 
} 
i = i + 1; 
} 

what do the apostrophes mean in (c == '(') ? also isn't there a syntax error here? it looks like (c == '(') needs another ) at the end of it. 撇号在(c == '(')意味着什么?这里还没有语法错误吗?它的末尾看起来(c == '(')需要另一个)。

what about here : else if (c == ')') ? 那这里呢: else if (c == ')')吗?

They surround a char in the same way that " surround a string like String s = "a string" . 它们以与"包围String s = "a string"类的String s = "a string"一样的方式包围char

In the code, it is testing if c is a ( character. 在代码中,它正在测试c是否为(字符。

(BTW, you have ' characters in your code, and I think these should be ' characters.) (顺便说一句,您的代码中有'字符,我认为这些应该是'字符。)

Single quotes indicate a character as opposed to a string which is wrapped in double quotes. 单引号表示字符,而不是用双引号引起来的字符串。 So: char c = 'a'; 因此:char c ='a'; string s = "a string"; string s =“一个字符串”;

Apostrophe here is used to surround one char value. 这里的撇号用于包围一个char值。 With String you use "", with char you use '' 对于字符串,您使用“”,对于字符,您使用“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM