4

I have a huge set of strings, where I want to find the length of the each string and add them and display the total length of string.

To calculate the length, I use method as string.length(), but I noticed that datatype of length() is int and max size of int is 2,147,483,647. However my string object have characters more then 2,147,483,647. Any suggestion how to get the length when it goes beyond int limit?

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

5

However my string object have characters more then 2,147,483,647

I doubt it: a String uses a char[] to store the characters and arrays can't hold more than Integer.MAX_VALUE elements. See also this post.

If you simply need to add the lengths of several strings, you can use a long variable instead.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
2

You can't have a string longer than that.

Integer.MAX_VALUE is the upper limit for indexing the internal character arrays used to store your string objects, so there isn't a way for you to have strings longer than that.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
1

In java no string object can have length greater than Integer.MAX_VALUE so string.length will work for you, but if you want have overall length of set of string greater than Integer.MAX_VALUE than you can use long type variable to store the value of length of overall set of strings

zaffargachal
  • 802
  • 7
  • 21
  • That will not make a difference. Since `String.length` returns an `int`. So, before typecasting, the data would already have been truncated. – Rohit Jain Nov 05 '12 at 12:40
  • you have set of strings, and each element can return the length greater than int in java, so there will be no truncation but when you are adding the different element length store that in long type and that method should use long type as return type – zaffargachal Nov 05 '12 at 12:42
  • Can't understand your first statement. Why there will be no truncation in case your length is greater than `Integer.MAX_VALUE`? – Rohit Jain Nov 05 '12 at 12:44
  • You cant have any string that has length greater than Integer.MAX_VALUE in java – zaffargachal Nov 05 '12 at 12:46
  • Exactly. And in that case, your answer makes absolutely no sense. Please update it. – Rohit Jain Nov 05 '12 at 12:47
  • My answer is about how you should get overall length for all string – zaffargachal Nov 05 '12 at 12:49
  • Then also it doesn't answer the question. OP is concerned that his strings are greater in length than and int can afford. Please re-read the question. – Rohit Jain Nov 05 '12 at 12:52
  • @RohitJain It makes sense what zaffar says. Since Java Strings cannot hold more than Integer.MAX_VALUE characters but it seems that the OP likes to deal with these large strings, it makes sense to tell him that he can work witha bunch of strings instead and calculate overall length using the long type. Do not want to say to recommend something like that but it makes sense in this context here. – Fabian Barney Nov 05 '12 at 13:49
  • @FabianBarney. Well, the answer now makes sense, after he edited it. :) – Rohit Jain Nov 05 '12 at 15:10
  • @FabianBarney , Rohit is right :) it realy make sense after editing , Thanks Rohit :) – zaffargachal Nov 05 '12 at 17:51