Im aware of the different data types in java but say if one wanted to have two different types of data in a single array; integer and string, would this be possible?

Im aware that java can have an array of each type but I stumbled upon using this and considering it doesn't return an error I thought it could be used:

Object [] myArray = new Object[11];

Just wondering...:P

Recommended Answers

All 9 Replies

Yes you could do something like that, but then you would have to run a validation on object retrieved from array to find if it is instance of obejct A or object B

if(myArray[n] instanceOf Book){
//code here
}
else(myArray[n] instanceOf Movie){
//code here
}

Yes you could do something like that, but then you would have to run a validation on object retrieved from array to find if it is instance of obejct A or object B

if(myArray[n] instanceOf Book){
//code here
}
else(myArray[n] instanceOf Movie){
//code here
}

So are you talking in terms of classes or more general objects in java?

Not sure if I understand your last question, so correct me if I'm wrong. Object is not use frequently (in direct), and you would used it only at the point of generalization. For example whole Jira project up till version 4 was infamous for it extensive use of Object collections and then manual checking what you are actually receiving. Which was a mad house. After that they started to deprecate this stuff and start dealing in specific types

Not sure if I understand your last question, so correct me if I'm wrong. Object is not use frequently (in direct), and you would used it only at the point of generalization. For example whole Jira project up till version 4 was infamous for it extensive use of Object collections and then manual checking what you are actually receiving. Which was a mad house. After that they started to deprecate this stuff and start dealing in specific types

Well I'll give you a quick example from my code:

static final Object minus = '-';
static final Object three = '3';
//......
public static void outputFive()
	{
		Object [] myArray = new Object[11];
		
		for( int i = 0; i < myArray.length; i++ )
		{
			if( i % 2 == 1 )
			{
				// System.out.print( i );
				// Prints all odd numbers
				myArray[i] = minus;
			} 
			else if( i % 2 == 0 ) 
			{
				// All even numbers
				myArray[i] = three;
			}
			
		}
		
		printArray( myArray );
	}

Here I have declared an array of Objects in which I will place different values depending on whether or not the number passed in the for loop is a prime number. I have created the constants above the method as data types Objects instead of say integers or characters. The method doesn't actually do much right now but it will when it all comes together at some point.

Yes that is fine and you can use. Your only "problem" would be checking each element to find out if it is instance of number or something else as I showed in my first reply.

You want to look into OO design a little more. If you're putting these things into an array, there must be a reason they're ending up there - that reason should give you a supertype, an abstract class that you can extend to the classes you need. If you find you're doing a lot of

for (Object o: ObjectArray){
  if (o instanceof Book) { do book stuff}
  if (o instanceof Movie) { do movie stuff}
}

then you really want to use inheritance. That should be like this:

for (LiteraryWork lw: LiteraryWorks)
{
  lw.doWhatIDo();
}

where doWhatIDo() is simply the right code for that type of thing - possibly a no-op, if no action is appropriate.

Your code above, though, doesn't mix types - you're creating a pair of Characters and putting them into an array of Objects. You could do that with ints - for example, you could use an array of booleans, true or false for odd or even, or you could use ints, -1 and 1, to get the same effect. Or, if you want, you could make and array of char.

To add to jon's reply, look into

Auto boxing and unboxing (example: Integer i = 5; will result in auto boxing) and especially look into inheritance. Don't sweat specific examples, such as your array example, until you have a thorough understanding of inheritance and the is-a relationship. Since Character 'is-a(n)' Object, that is why you can put a Character (or any other java class, since they all inherit directly or indirectly from Object) into an Object array.

On a related note, something which you should always keep in mind when writing code is to follow the principle of least intrusion when it comes to defining contracts; be it between methods of the same class or when exposing an external API.

Let's take a trivial example of printing the contents of a String array. You can of course write a method which would loop over all the strings and print them, but what happens when you need to print out "Foo" objects? Taking a step back, we realize that the "print" method really need not know about the real type of the object it would be printing. Given that every Object inherits the 'toString' method from the Object superclass, you can simply loop over any type of list and just output its string representation using the toString() method.

So basically, never ever define external contracts in terms of implementation/specific classes; they would make your code needlessly inflexible. Be as generic as possible i.e. make sure the method being called doesn't know "too" much about the caller and vice-versa.

Oh and BTW, apologies for the near-offtopic rant. :-)

Cheers all for your help, there's a-lot of information there to take it and research but I'll get it.
Thanks guys :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.