Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which type is str[0] in string "abc"

I have a String s="abc" in Java. What is s[0] type? Isn’t it a String type?

I try to print it in Java, but it shows "The type of the expression must be an array type but it resolved to String";

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The square brackets are used to access the elements in an Array type.

String is an object, and so you can’t access it’s characters via [].

To access individual characters, you could either:

  1. s.charAt(index)
  2. char[] charArr = s.toCharArray(); and access charArr[idx]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading