-2

how to use long in string methods like charAt in java??

String s="google";
long i=0;
System.out.println(s.charAt(i));
ernest_k
  • 42,928
  • 5
  • 50
  • 93
nitin
  • 1
  • 1

2 Answers2

1

The maximum Integer value in Java is: 2,147,483,647. Which means that for s.charAt(i), i can have a max value of 2,147,483,647 (2 billion)

Will you really have a string having more than 2 billion bytes of length? int is fairly enough.

Alan Deep
  • 1,937
  • 1
  • 12
  • 20
1

Well, Integer.MaX_VALUE is 2,147,483,647 (2 mld). Even if you'd use ANSI encoding, string with this many characters would have almost 2 gigabytes! With UTF-8 encoding that'd be 8 gigabytes!

So an integer is perfectly fine for representing character's position within a string, and If you want to get that position by long, just cast it.

string.charAt((int) i);
Danon
  • 2,190
  • 20
  • 34