Converting autoboxes to primitives[Solved]

So, I have an array of java.lang.Object. I want to point out which ones are wrappers (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, and Double (Not the primitives which have lower-case / shorter names!)). Correct me if I am wrong, but I think that I just use

Object.getClass().isPrimitive()

So now I need to use those classes, and convert them to primitives, i.e. Float-float, Integer-int.

      for(int j = 0; j < arguments.length; j++) {
			if(arguments[j].getClass().isPrimitive())
				types[j] = //Then I want to put the primitive-inated version here.
		}

How?

If you find yourself dealing with arrays of Object only so that you can sneak multiple types of things around your code, it is more than likely stemming from some kind of bad design.
I highly recommend figuring out a different way of doing whatever it is you are trying to do.

Since I don’t know what that is I can’t help you there, but I can give a tip if you continue with the current method:
[icode]obj.getClass().isPrimitive()[/icode] will return false on Integers, etc. Instead you want this:


if (obj instanceof Number) //it's an autoboxed primitive (i.e. Integer, Long, Double, etc.)

Character is not a subclass of Number however, so you would need a separate test for that.

Also: [icode]types[j] = obj[/icode] will automatically assign the primitive value to types[j] if obj is a Number. That is why it is called autoboxing. You will need a cast however.

I don’t know what you’re doing, but you can’t just stick in different types of primitives into an array like you’re doing with ‘types[j]’ unless it’s an array of objects (for example Number objects) in which case you can just stick any kind of Integer, Long or Float object into it.

You can use the classes longValue() or any of other value() they’ve inherited from the java.lang.Number abstract class: http://docs.oracle.com/javase/6/docs/api/java/lang/Number.html

That code makes me extremely uncomfortable…
What exactly do you need an array of all primitive types for?

I’ll say it again: Character is not a subclass of Number, so that test inside of the Number test block is irrelevant.

Are you looking to serialize an object?
http://www.tutorialspoint.com/java/java_serialization.htm

Generally speaking, whenever you have a chain of more than a couple if-else statements, you are doing something wrong.

Another general rule is that whenever you are using instanceof to check for many different types, you are also approaching it the wrong way.

Further; if you are using arrays of java.lang.Object, there is also a chance you’re approaching something in the wrong way.

Maybe classes is what you need?

class Entity {
    //we can store different objects/primitives in this class
    private float x;
    private character c;
    private String text; 
}

...

//a growable array
ArrayList<Entity> list = new ArrayList<Entity>();

//we can create a class like so:
Entity entity = new Entity();
Entity entity2 = new Entity();

//and we can add/remove things from our list like so:
list.add(entity);
list.add(entity2);

list.remove(entity2);

I would recommend looking at some open-source games to get a better idea of how to program.

https://github.com/libgdx/libgdx/tree/master/demos

You maybe know how to program, but you will never stop learning.
When you stop learning, you stop beinig a good programmer :).

You should not create an godfactory, instead spread around the functions.
I dont even want to know what your function looks like right now.

Instead of

CreateObject(whatever Object, random list of args);

you should use multiple functions like this:

CreateTerrain(Terraintype t);
CreateEnemy(Enemytype t);
CreateWeapon(Weapontype t);

etc…

Also if you really want to do this, do it like this (but really, DONT!):

MakeObject(Objecttype t, Object... params);

I don’t see the need for a .class as a parameter. What functionality does the .class provide over the base object?

If you don’t have patience with those trying to help you, don’t bother asking.

First of all, I’m pretty sure none of us understand what you actually want to do. Telling us the same thing over and over expecting us to figure it out won’t help. Also, there’s no need to insult someone who clearly was just trying to help.

On to your question, I believe I have a slight idea of what you’re trying to do, could you tell us exactly what isn’t working? Have you even tried to create the method? Shouldn’t you just use generics and pass in the tile class? I don’t understand what’s not working.

Do you want to load a map with strings holding the tile type?

You could use an enum and say:


if(id.equals(DIRT.name()){
   return new TileDirt();
}

But again, if you’re approaching it this way, then you’re probably doing it wrong. I apologize if this is not what you are trying to do.

Not trying to insult ya – just giving you some fair advice. I started programming at a young age, and now, many years later, I can safely say that my early code really sucked.

And, no offense, but you don’t really know much about programming given all the code you’ve shown so far. :slight_smile:

Who cares what framework it uses – the point is, look at good code and learn from it.

You still haven’t answered my question - why do you need to store a .class by a variable?
Also, to davedes’ point, your code just looks like beginner. 99% of programmers couldn’t bear to have a long chain of if statements, and the good programmers don’t need them.

Sorry I haven’t read this through very thoroughly.

Can you describe at very abstract level what you want to do? Like save a game? Save a map? Load a map?

Why though?

Anyway, have you looked into object serialization - I think this is what you’re looking for.

That’s what thought too, but apparently not - he’s passing in “types” of tiles, and since enums “didn’t fit” he’s using Class.class to identify type.


public enum Tile {
GRASS(),
STONE();

private Tile(){
}

}


public void createTile(Tile tile){
//create the tile

}
public void someOtherMethod(){
createTile(Tile.STONE);

}

Exactly what is wrong with this?

[quote]What about it is so bad? I am just using reflection to save me some time and effort.
[/quote]
Well, reflection itself for this purpose is bad. :slight_smile:

You shouldn’t take it so personally. It took me a few years before I started producing ok code, and even then, it wasn’t too pretty. Programming is a subject in which you’re constantly learning, and constantly improving.

Hmm, well last time I checked, that happens automatically, by definition.


Integer i = 20; //assigns primitive value to an object
int d = i; //assigns value from object to a primitive

What do you mean “takes more time”? I can guarantee that those if statements will cause you more problems than if you used correct code structure. “Not good” isn’t really descriptive enough to describe the problem. Also, why would your id class contain id’s of every enum type? Like davedes said, I would recommend reading through the source of some games.

Here is what I think you want to do:

You have a bunch of different tiles your game can use, you want to create a Tile by its name. I.e, you want to create com.wesley.laferriere.world.SandTile by its name, and com.wesley.laferriere.world.GrassTile from its name. This way, when you want you save your map, you can say: These tiles are SandTile, and these tiles are GrassTile, then when you load your map you can just load all the tile’s classes with their name.

If that sounds like what you want to do, just say and I have a few routes you can take.