David
David

Reputation: 14963

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.

what about here : else if (c == ’)’) ?

Upvotes: 2

Views: 3633

Answers (3)

mohdajami
mohdajami

Reputation: 9690

Apostrophe here is used to surround one char value. With String you use "", with char you use ''

Upvotes: 0

mcliedtk
mcliedtk

Reputation: 1031

Single quotes indicate a character as opposed to a string which is wrapped in double quotes. So: char c = 'a'; string s = "a string";

Upvotes: 1

David Johnstone
David Johnstone

Reputation: 24470

They surround a char in the same way that " surround a string like String s = "a string".

In the code, it is testing if c is a ( character.

(BTW, you have characters in your code, and I think these should be ' characters.)

Upvotes: 6

Related Questions