• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Is Array an Object

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is array an object?


Arrayobject
It can only store one datatype variablesIt can store multiple datatype variables
It cannot execute methodsIt can execute methods


So how is array an object?
 
Saloon Keeper
Posts: 5773
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just how it is implemented in Java. Try this:


It can store multiple datatype variables


Only if you write it that way. Objects may contain only variables of one type.

It can execute methods


Only if you write it that way. Objects need not have publicly (or even privately) executable methods.
 
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . .

Arrayobject
It can only store one datatype variablesIt can only store multiple datatype variables
It cannot execute methodsIt can execute methods

Where did you get that from? The bit about one datatype variable sounds confused; please explain what you mean by it. If you mean that the fields of a non‑array object can have different types, then I shall say that they can only have the types declared when the code is written. Similarly an array has one type of element, which is defined when the code is written. The following code defines the type of element as Foo:-...but an array can be declared with any element type without <> in its name. An object with one field will have only one type of field.
Let's omit the wait(...), notify(), and notifyAll() methods, but we can call the other six methods inherited from Object like this:-Line 10 won't compile because you can't call methods on primitives.
 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:. . . . Objects need not have publicly (or even privately) executable methods.

An array is what Tim has described here: an object from a class which only overrides one method from Object and doesn't declare any new methods.
 
Marshal
Posts: 7083
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:

Arrayobject
It can only store one datatype variablesIt can only store multiple datatype variables
It cannot execute methodsIt can execute methods



If you had to choose from those 4 sentences and put them in the corresponding table columns/rows - it is fine, but in general I find it unnecessarily confusing as for descriptions.

When we say about object "It can only store multiple datatype variables", I end up being confused, so it cannot store singular datatype variables? It can't do anything else? Those questions don't require answer, but just shows, how I would be confused reading in a book about object presented in such way.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Hari Nagarjuna wrote:

Arrayobject
It can only store one datatype variablesIt can only store multiple datatype variables
It cannot execute methodsIt can execute methods



If you had to choose from those 4 sentences and put them in the corresponding table columns/rows - it is fine, but in general I find it unnecessarily confusing as for descriptions.

When we say about object "It can only store multiple datatype variables", I end up being confused, so it cannot store singular datatype variables? It can't do anything else? Those questions don't require answer, but just shows, how I would be confused reading in a book about object presented in such way.



Ok.
Now that I look at it my post was confusing.
What I was trying to ask was



In the line above  I am declaring and assigning an int array.
So can only contain one type array elements i.e int
methods cannot be used with this array.--->maybe I am wrong.But at least I have not seen it from the topics I have read yet.




Above I have declared one as an object and I could assign int,boolean values to same object
and I could call display() method using the object which is not possible using arrays.

Well this is assuming we have variables of  different datatypes and methods in a class which may not be the case every time.
At least we have that possibility.As class is a blueprint of object which contains state(instance variables) and behaviour(methods)
and objects should be able to use state and behaviour properties of class.

But considering array as an object it cannot even call methods?

The matter above topic has some of my assumption and may be TOTALLY wrong.
Please correct me.






 
Saloon Keeper
Posts: 3462
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Hari,

well, Tim showed you that an array is an object. If still in doubt, see if you can use an int[] as a type-argument in a generic class. For instance, is this legal: List<int[]> a = new ArrayList<>();? Or can you have a Comparator<int[]>? Try it out in your IDE.

And while in your IDE, if you do:

and then you type in the program part: a., does there pop up a list of available methods? If so, why?
 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . . methods cannot be used with this array.--->maybe I am wrong.But at least I have not seen it from the topics I have read yet.

You appear not to have read my post, then. What good is it asking a question and then not reading the replies?

. . . Above I have declared one as an object and I could assign int,boolean values to same object and I could call display() method using the object which is not possible using arrays. . . .

You are not assigning an int or a boolean to your object (and cannot). Only to its fields.
You can only call display() because you wrote a display() method. You cannot write new methods in an array.

But considering array as an object it cannot even call methods? . . .

No object calls methods. Only statements and expressions can call methods. As I showed you earlier, minus line 10 which won't compile, you can call methods on an array object.
 
Liutauras Vilda
Marshal
Posts: 7083
491
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

I think forget about your initial topic, at least in the way it was presented to you, because it causes no end confusion. I'd say analogs were chosen poorly, which in turn confused you.

When things get complicated, and difficult to explain, I usually try to find some silly examples which may or may not closer to real life examples.

Here's one:
1. Think of an array as an already built toolbox, which can hold either a set of hammers, or bunch of screws, or bunch of pliers or similar tools of one kind. That is how it was built and that is it.
2. Think of an object as a thing, which can be built to be a: either toolbox, or house, or phone, or car, or... anything what you could think of as a model from let's say real life.

So an array is already pre-built and could not be modified - so it serves a purpose of storing something. It is an object. Probably would be easier to think that everything is object, except few primitive types in java such as: booleans, ints, longs, chars, bytes...

Consider object as a material, which gets its form when is assembled based on needs. So if you need it to be able to store several different kind values in it, yes, you can configure it in such way so it could do that. If you need it so it couldn't store any value in it - you can make it so too.

Arrays in Java are being declared in such way as an example:

So such array would be able to hold 4 car objects in it. An array is denoted as [] in Java. Car on the other hand, is a type of object an array can hold.

Now, how such Car object definition may look like:

As you see, I defined how my Car object will look like, meaning, what kind of information it will hold. So it will have its make, it will store its current speed. Make and Speed as you see are of different type, obviously... So I made it in the way it makes sense to me, which is going to be used in my hypothetical program.

So such object gets instantiated in the following way:

If you remember, I've declared one method in it, which is increaseSpeed(), so I can call it:

With an array on the other hand, you can't define methods yourself, because it was built already and most importantly, NOT BY YOU, so you have locked ability to add anything else than what was added by the guys who implemented presumably Java language. And as you saw, an array has "[]" this thing, which you don't use when you create object, so that is also a specific array's thing, which I don't think you need to know anything more about it (same as I).

And so really that is it I think.

What confuses you from all that?
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sir Tim Moores,Sir Campbell Ritchie,Sir Liutauras Vilda,Sir Piet Souris for your replies.
I am using the idea of Sir Liutauras Vilda to represent what I understood about class,objects and arrays.
Please correct me if I am wrong.

1.Class can be considered as a blueprint.
  Example-Blueprint for a house which can be designed by you.

2.Objects are created using class as blueprint.
  Example-Building identical houses using same house blueprint.
 


3.Object name(Object reference variable) helps us to identify each object seperately
  Example-Now the two houses are built[new House(); and new House()] are identical.
When person1 and person2 wants to move into their own House they should first identify their own House .
So they created signboards seperately which point to their own Houses.[This is object reference variable or object name]

4.Dot operator is used with objects to assign field values to object fields.
Example-Paint the house in white color .
Which house ?(There are two identical houses).
Ans-Person1 House which ca be represented by
[here color is string variable of class House]

5.Methods can also be called using dot operator on objects which point to the action performed in a particular object by given method.
Example-Let's say the persons in two houses performs action of eating.
There is method of eating in the class House.

Here house,utensiland eating are String variables that need to be assigned for each House as
they may eat different things using different utensils.
People of Person1 House are eating maggi with fork.Here


so the method gives result of


People of Person2 House are eating oats with spoon.


so the method gives result of



In case of arrays

1.Blueprint(its class) is alredy predetermined.
2.It can store only one type of field value.[int or char or string,etc...]
Example-
3.Array name can be used in similar way as objects to assign values to array elements.

4.Array name can be used as reference used to call its methods with help of dot operator.
Example-

5.The methods of an array object are predetermined and cannot be edited by user.
 
Campbell Ritchie
Marshal
Posts: 65425
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I htink you have got it right about arrays, but your design of objects needs a lot of work. Start by making all their (non‑constant) fields private.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!