File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Array" Watch "String Array" New topic

String Array

Anil Konduru

Joined: Feb 18, 2006
Posts: 9
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"};



Anil Konduru.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

U must know that above code is just a array of is not required to use new keyword to create array as shown below.
Douglas Chorpita
Ranch Hand

Joined: May 09, 2006
Posts: 97
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 ]

SCJP 1.4 - 95%
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Arrays in the Java Tutorial.
I agree. Here's the link:
subject: String Array
jQuery in Action, 3rd edition