+ 1

public class Program { public static void main(String[] args) { String[ ] myNames = newstring[5]; mystring[]={ "A", "B", "C", "D"}; System.out.println(myNames[2]); } }

In the explanation it is said that we can initialize the capacity of array using keyword " new".when I applied the same logic for string it was showin error. Please help me out with this code. Tanks in advance.

15th Oct 2016, 12:17 AM
Pragathi M D
6 odpowiedzi
+ 2
String[ ] myNames = { "A", "B", "C", "D"}; System.out.println(myNames[2]); You don't need the rest, is that what you are asking?
15th Oct 2016, 1:51 AM
Flo
0
My Question is whether we can do declaration and assignment separately?
15th Oct 2016, 2:47 AM
Pragathi M D
0
I want to print only C not ol the chracters..
15th Oct 2016, 2:48 AM
Pragathi M D
0
yes u can do that separately,bt remember that size mst be defined at the time of declaration if u r doing so
15th Oct 2016, 11:08 AM
Ankur Sharma
Ankur Sharma - avatar
0
you have declared the array name as myNames , initialized using mystring and again printing using myNames. That is why the error message is showing. Do it correctly
19th Oct 2016, 8:16 PM
Jappreet Singh
Jappreet Singh - avatar
- 4
i don't think you can with Strings , since String has slightly different behavior than regular arrays
15th Oct 2016, 7:52 AM
Flo