I am beginner in Java. As I was going through the book, I got stuck on this problem. My question is why there are single quotes instead of double quotes around this period. When we want to print a string, we use double quotes.
Asked
Active
Viewed 93 times
-2
-
3please add the code in the question. – davidxxx Jun 07 '17 at 19:45
-
single quotes represent a Character and double quotes represent a string. – Karan Sharma Jun 08 '17 at 06:14
2 Answers
1
'.' would indicate a type of char
"." would indicate a type of string
Both will work to print

Easton Bornemeier
- 1,918
- 8
- 22
1
In Java, we use ''
to delimit a char
and ""
to delimit a String
.
In this particular case, you need a char
because you are comparing '.'
with the result of a call to the charAt
method - which is always a char
. That is to say, sentence.charAt(lastCharPosition)
has to be a char
, so it can only be '.'
and never "."
.
If you wanted a String
, (which you don't in this case), you could write "."
.

Dawood ibn Kareem
- 77,785
- 15
- 98
- 110