File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Array

 
Anil Konduru
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the String array object is getting created in the following code. For each object creation we need to instantiate that object with the new keyword but without creating the object the values are assigned.

public class CheckObjectArray
{
public static void main(String args[])
{
String[] stringobj = {"Red","Yellow","Green","Blue"};
System.out.println(stringobj[0]);
System.out.println(stringobj[1]);
System.out.println(stringobj[2]);

}

}
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

U must know that above code is just a array of string.it is not required to use new keyword to create array as shown below.
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a special syntax allowed by Java.

String [] array = { "A", "B", "C" };

is really the same as:

String [] array = new String[] { "A", "B", "C" };

If you think about it, most of the time, when we create an array, its instance type (here String[]) is the same as the reference type (also String[]). So this is a "shortened syntax".

So, although the syntax doesn't EXPLICITLY show it, there is a String array being instantiated and put on the heap. The String objects, because they are not created with the new operator, will be put in the String literal pool. But they also wil be instantiated (not on the heap, rather in the literal pool). The char[] arrays for these literal strings are also put on the heap.

For more on this topic, read:

About Strings
[ July 28, 2006: Message edited by: Douglas Chorpita ]
 
wise owen
Ranch Hand
Posts: 2023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays in the Java Tutorial.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic