-3

I've encoutered an error while writing a line like this:

String string;
if (Character.isLetter(string[i])) {...}

Eclipse tells me that The type of the expression must be an array type but it resolved to String.

It does not raise the error if I use string.charAt(i) though.

My question is:
What is the difference between string[i] and string.charAt(i) in Java?
Are there any delicate aspects of using one or another?

I've tried to find a solution by myself, but I've encoutered javascript version of this problem only. (Example: Difference between str[0] and str.charAt(0))

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
  • if string is array then only you can do string[i]. – Prashant May 01 '15 at 18:43
  • 1
    Your question is unclear. What is `string`? If it is instance of `String` then Java doesn't support `[i]`. Why do you think it should work? In Java String *contains* array of characters (`char[]`), but it *is not* such array itself and `[index]` can be used only with array. – Pshemo May 01 '15 at 18:44

6 Answers6

3

In Java a String type is not the same as an array of characters as it is in some languages. It is possible to get an array of characters from a String, but they are not the same thing.

Evan Williams
  • 406
  • 2
  • 8
1

In Java, string[i] is not the ith index of the string. Instead, if you had defined string as an array (e.g., String[] string = new String[10]), then string[i] would refer to the ith element in the array.

David Koelle
  • 20,726
  • 23
  • 93
  • 130
1

String[i] is a the way to access the ith element in an array of Strings, please note they are zero-indexed in java like in javascript.

While string.charAt(i) is the ith character in one particular String. Here again i is zero-indexed.

It's tempting to think that Strings in java are array of characters like many other languages but in fact they are built on top of a char array which is a private member of class String as explained here.

https://stackoverflow.com/a/20248431/2669716

Community
  • 1
  • 1
Anirudh
  • 2,286
  • 4
  • 38
  • 64
1

Accessing elements using the [] operator only works for arrays. Strings are not arrays. That means using the [] operator on a String does not compile.

If you want to convert a String to a char[] array, use the toCharArray method.

String s = // ...
char[] chars = s.toCharArray();
char firstLetter = chars[0];
fabian
  • 80,457
  • 12
  • 86
  • 114
1

[] is used to reference an index of an Array. While a String is usually considered to be an Array of char (char[]), you aren't accessing the character array... that method string.charAt(i) will access the Array to get you the physical character.

Learn more about the String class here: http://docs.oracle.com/javase/8/docs/api/java/lang/String.html

If you had code like:

String[] stringArray = new String[] { "String 1", "String 2", "String 3", "..." };

You could do:

for (int i=0; i < stringArray.length; i++) {
    System.out.println("String " + String.valueOf(i) + ": " + String.valueOf(stringArray[i]) + ".");
}

Hope that helps!

anonymous
  • 657
  • 1
  • 8
  • 21
1

This might clear things up for you:

public static void main(String[] args) {
  String str = "my name is Muneeb";
  String[] strarray = str.split(" "); //split on white space.

  for(int i=0; i<str.length(); i++){
     System.out.println(str.charAt(i));
  }

  for(int i=0; i<strarray.length; i++){
     System.out.println(strarray[i]);
  }
}

The first output will be:

m
y

n
a
m
e

i
s

M
u
n
e
e
b

The second output will be:

my
name
is
Muneeb
Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Muneeb Nasir
  • 2,414
  • 4
  • 31
  • 54