Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic