This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concept of new!  RSS feed

 
Deepakk Verma
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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
 
pankaj vijay
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Janeice DelVecchio
Bartender
Posts: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, you will get an error:


the for line should read:
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • 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: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 55751
163
  • Mark post as helpful
  • send pies
  • 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: 1812
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 55751
163
  • Mark post as helpful
  • send pies
  • 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().
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which should of course be called getLength() and getSize().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!