• Post Reply Bookmark Topic Watch Topic
  • New Topic

s.length understanding  RSS feed

 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone, could some one please explain why the following code prints out the value 21 and not 20. What am I missing ?

 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just count 21 characters in the string. Are you saying that you only counted 20?

Henry
 
Junilu Lacar
Sheriff
Posts: 11166
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, you missed one character. There are indeed 21 of them in that string.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because length() method of String returns the length of this string.
Here total char count = 17 + 1 special symbol '!' and 3 blank spaces so total becomes 21.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm only counting 20. My count starts at 0 is that right..
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:

I'm only counting 20. My count starts at 0 is that right..
No, length() method counting starts from 1 not 0.

Edit:
charLearningJavaisFun!
count123456789101112131415161718192021
 
Junilu Lacar
Sheriff
Posts: 11166
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:No, length() method counting starts from 1 not 0.

Actually, that's not quite right either. String.length() can return 0.

@OP: don't confuse indices and length. Yes, indices in Java are zero-based. Length, however, is not an index, it's an absolute measure. A string with no characters (an empty string) has a length of 0. A string with 1 character, a length of 1 and so forth. A string with 21 characters in it has a length of 21.
 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's my mistake, so is it only length() method counting that starts from 1 ...?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:

I'm only counting 20. My count starts at 0 is that right..


No. Indexing starts at zero. Counting starts at 1. Would you ever say that you have zero apples when there is one in your hand?
 
Junilu Lacar
Sheriff
Posts: 11166
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Dev wrote:that's my mistake, so is it only length() method counting that starts from 1 ...?

You may have just missed my last response but no, the minimum value that length() can return is still 0. That's when a string has 0 characters in it.


 
Will Dev
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so only indexing starts at 0. Got it. Thank you everyone for assisting me, i love this forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!