-2

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.

enter image description here

Arya McCarthy
  • 8,554
  • 4
  • 34
  • 56

2 Answers2

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