• 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

Array declaration - what is diff ?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am declaring an array as follows,
int []i = {1};
int []i = new int[]{1};
what is the difference?
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they are both essentially the same.
second defn is also called as anonymous array.
however, the application of second definition comes into play in method arguments:
void method1(new int[]{1}){};
also, this will felicitate an easier task for the garbage collector since anonymous array object will be avilable for GC immediately after exec of method.
hth.
chetan
 
Rajasekar Loganathan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chetan nain:
they are both essentially the same.
second defn is also called as anonymous array.
however, the application of second definition comes into play in method arguments:
void method1(new int[]{1}){};
also, this will felicitate an easier task for the garbage collector since anonymous array object will be avilable for GC immediately after exec of method.

I agree with your anonymous array explanation but how do you
compare it with second definition since it is having a name
as "i".
I suspect there is a difference in using a new keyword
and with out it as in while memory allocation for string pools.
eg,
String s1 = new String("abc");
String s2 = "abc";
String s3 = "abc";
s2 & s3 refers same memory location but s1 not.
bye,
Sekar.
hth.
chetan


 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rajasekar,
Yes there is diff, because i had the same question and herbert helped me.
Go through following link.
http://www.javaranch.com/ubb/Forum24/HTML/001729.html
camoe back if u have any doubts.
vivek
 
Rajasekar Loganathan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vivek Shrivastava:
Hi rajasekar,
Yes there is diff, because i had the same question and herbert helped me.
Go through following link.
http://www.javaranch.com/ubb/Forum24/HTML/001729.html
camoe back if u have any doubts.
vivek


Thanks vivek it is very nice explanation i found. Can you
clarify me the following,
1) Why it is not possible to have Instance variables
declarion and followed by definition is not possible?
ie
Object ob;
ob = new Object(); // illegal if "ob" is instance variable
the same above code if it is inside a method it becomes a valid
statement how?
What is the reason and what does the compiler actually do
here?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object ob;
ob = new Object(); // illegal if "ob" is instance variable
it is just the syntax:
Object ob;
{ob = new Object();} // need to use instance initializer
just what the designer decided to allow.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic