• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);

}

}
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arrays in the Java Tutorial.
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic