• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Concept of new!

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is new used only to allocate memory for an object.......i mean if i do something like

Will this create an array of integer type or there is some predefined class named as int whose
object is it creating.....
please explain

if somebody else also has some useful information regarding new then please share
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new is not the name of class .. its a keyword which is used to allocate memory. It create object of class.
in your statement

int []f = new int[100];


here you are creating an array of length 100 whose type is integer.
int is not class but Array itself is a class. Array is subclass of Object.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pankaj, by saying "Array is a subclass of Object", you seem to be suggesting that there's a class "Array" which is involved here. There is not. There is a class that represents "array of int", and an instance of that class is created in the "new" expression we're talking about. That class (ultimately, like all classes) is, indeed, a subclass of Object.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incidentally, you will get an error:


the for line should read:
 
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:Incidentally, you will get an error:

Well done noticing, but "for (int i = 0; i < myArray.length; i++) . . ." is better. Less risk of copying the size incorrectly.

Deepakk Verma, have a look in the Java™ Tutorials where you find out about arrays. There is also a section about "for" loops there.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's always a potential error with you, isn't there, Campbell?

Alright... I just have to get used to it and just get better.

Janeice

P.S. I think you need a () after the ".length"
 
Campbell Ritchie
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That syntax will guarantee you always stay within the bounds of your array.
No, you miss out the (); it is length not length(). The explanation is in the Java™ Language Specification where you will find "length".

You will be able to understand that with no further explanation.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So array.length is a field... but String.length() is a method.... and so is ArrayList.length(). Difference in syntax for the same result.

Stupid (and my brain wants to reject it), but noted.
I SUPPOSE it ALMOST makes sense because you need to declare the length of the array when it's created... so no calculations need to be done.

You'd think they'd make programming languages easier for the PROGRAMMER to understand.

Campbell Ritchie - in the business of teaching ranchers about Java every day of his life.

Thanks again!
Janeice

 
Campbell Ritchie
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe lack of communication between those who wrote the array part of the JVM and those who wrote the classes. You get methods called length() and methods called size().
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which should of course be called getLength() and getSize().
 
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic