how to use long in string methods like charAt in java??
String s="google";
long i=0;
System.out.println(s.charAt(i));
how to use long in string methods like charAt in java??
String s="google";
long i=0;
System.out.println(s.charAt(i));
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.
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);