Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Converting autoboxes to primitives[Solved]  (Read 917 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2013-08-15 03:10:39 »

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
 
1  
Object.getClass().isPrimitive()


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

1  
2  
3  
4  
      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?

Offline BurntPizza
« Reply #1 - Posted 2013-08-15 03:43:43 »

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:
obj.getClass().isPrimitive()
will return false on Integers, etc. Instead you want this:

1  
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:
types[j] = obj
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.
Offline jonjava
« Reply #2 - Posted 2013-08-15 04:49:30 »

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 <dataType>value() they've inherited from the java.lang.Number abstract class: http://docs.oracle.com/javase/6/docs/api/java/lang/Number.html

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #3 - Posted 2013-08-15 04:55:31 »

I fixed it the blunt force way... This code is just to save some time when I am creating objects. Don't ask, it is a big story...  Tongue

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
for (int j = 0; j < arguments.length; j++) {
         if (arguments[j] instanceof Number) {
            if (arguments[j] instanceof Float)
               types[j] = float.class;
            else if (arguments[j] instanceof Integer)
               types[j] = int.class;
            else if (arguments[j] instanceof Double)
               types[j] = double.class;
            else if (arguments[j] instanceof Byte)
               types[j] = byte.class;
            else if (arguments[j] instanceof Float)
               types[j] = float.class;
            else if (arguments[j] instanceof Long)
               types[j] = long.class;
            else if (arguments[j] instanceof Short)
               types[j] = short.class;
            else if (arguments[j] instanceof Character)
               types[j] = char.class;
            else if (arguments[j] instanceof Boolean)
               types[j] = boolean.class;
         } else
            types[j] = arguments[j].getClass();

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-08-15 05:01:35 »

That code makes me extremely uncomfortable...
What exactly do you need an array of all primitive types for?
Offline BurntPizza
« Reply #5 - Posted 2013-08-15 05:02:25 »

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

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #6 - Posted 2013-08-15 05:11:37 »

That code makes me extremely uncomfortable...
What exactly do you need an array of all primitive types for?
You are not alone in that, believe me...

So... I had an issue where I needed to store an object type / .class, and later make a new instance of it, as the default ground, or wall tile. So instead of going barbaric by making a class containing seperate String ids' for each new object, I decided to make a method that will return it. Then, I realized that I need to pass in variables. So I made the varargs 'arguments,' and then float and Float got mixed up and I was forced to do this brute force method.

I don't like it, but I am tired, frustrated, and don't see too much wrong with this other than it is not too flexible...

Offline Jeremy
« Reply #7 - Posted 2013-08-15 09:19:22 »

That code makes me extremely uncomfortable...
What exactly do you need an array of all primitive types for?
You are not alone in that, believe me...

So... I had an issue where I needed to store an object type / .class, and later make a new instance of it, as the default ground, or wall tile. So instead of going barbaric by making a class containing seperate String ids' for each new object, I decided to make a method that will return it. Then, I realized that I need to pass in variables. So I made the varargs 'arguments,' and then float and Float got mixed up and I was forced to do this brute force method.

I don't like it, but I am tired, frustrated, and don't see too much wrong with this other than it is not too flexible...

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

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline davedes
« Reply #8 - Posted 2013-08-15 12:25:32 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
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
https://github.com/marcoamorales/pong-libgdx-clone/tree/master/src/com/pong

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #9 - Posted 2013-08-15 19:29:06 »

I would recommend looking at some open-source games to get a better idea of how to program.
(T_T)
You have no idea how much this insults me...
I am not an idiot. I know how to program.

I already have an object class for my own entities. I just like the name 'object'. This method has nothing to do with my list of entities! The java.lang.Object is for adding in the arguments for constructors. I have a feeling that you did not read this right. I have my own reasons for needing to use reflection. I need to be able to say in one method,
1  
2  
// Make an object from a .class, with these arguments in the constructor :
public Object makeObject((A .class i.e. Grass.class), (All of my arguments))


P.S: Why did you point me to only libGDX tutorials? I am not even using libGDX!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #10 - Posted 2013-08-15 19:37:17 »

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

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
1  
CreateObject(whatever Object, random list of args);


you should use multiple functions like this:
1  
2  
3  
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!):
1  
MakeObject(Objecttype t, Object... params);

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #11 - Posted 2013-08-15 19:45:47 »

None of you understand! When I generate a tilemap, I need to pass in some variable that will point to a class that will be made by default on the wall, or ground. Instead of making every object have a
1  
String id = "Name of Class";

I will just use reflections so I can pass through a class, and then initialize it. I won't use this method much, just when I need to pass in a class type, and initialize it.

If you still don't know what I am trying to do... just don't bother answering.

Also,
You maybe know how to program, but you will never stop learning.
When you stop learning, you stop beinig a good programmer Smiley.
I like that  Smiley
But, I never said that I was done learning!  Wink I just know way more than basic game loops. Really, how do you make a game and not know what a game loop is?!

But I hate

you should use multiple functions like this:
1  
2  
3  
CreateTerrain(Terraintype t);
CreateEnemy(Enemytype t);
CreateWeapon(Weapontype t);

etc..
That is extremly prone to annoyance. Think about it. I have to make 2 classes for every tile, object, and Entity type. Why that, when I could use ultra flexible reflection?

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #12 - Posted 2013-08-15 20:05:08 »

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

If you still don't know what I am trying to do... just don't bother answering.
If you don't have patience with those trying to help you, don't bother asking.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #13 - Posted 2013-08-15 20:15:13 »

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.

Offline Longarmx
« Reply #14 - Posted 2013-08-15 20:32:28 »

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

Quote
DIRT DIRT GRASS GRASS DIRT
MUD DIRT GRASS GRASS DIRT
WATER MUD GRASS GRASS DIRT
etc...

You could use an enum and say:

1  
2  
3  
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.

Offline davedes
« Reply #15 - Posted 2013-08-15 20:44:29 »

(T_T)
You have no idea how much this insults me...
I am not an idiot. I know how to program.
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. Smiley

Quote
P.S: Why did you point me to only libGDX tutorials? I am not even using libGDX!
Who cares what framework it uses -- the point is, look at good code and learn from it.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #16 - Posted 2013-08-15 20:49:11 »

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

If you still don't know what I am trying to do... just don't bother answering.
If you don't have patience with those trying to help you, don't bother asking.


I need to be able to store a .class as a variable. I would use a base object, but I want to be able to init over one line, not make it, then set the x, and y. So I made this method. This way, I can create an object based of a type, which I pass into a class called GenTileMap, which is a Generated Tile Map. I pass in Grass.class as the default ground tile, and Stone.class as the default wall tile.

I want to know what people don't get about this! Sorry if it seems to make more sense in my head.

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

Quote
DIRT DIRT GRASS GRASS DIRT
MUD DIRT GRASS GRASS DIRT
WATER MUD GRASS GRASS DIRT
etc...

You could use an enum and say:

1  
2  
3  
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.

No, not load a map. But I did do something like what you made, then got sick of it... I used enums, but they just did not fit. The way I do it, I don't have to make one huge class that contains ID checking. It was a waste of time, as apposed to using reflection, where it is done in less lines, and less hassle for me.
no offense, but you don't really know much about programming given all the code you've shown so far. Smiley

'No offense,' but you basically just said no offense and then directly offended me. What do you mean? Should I show more code? What about it is so bad? I am just using reflection to save me some time and effort.

Edit:
Forgot to say @davedes, I did not say I know programming fully. I just am saying I know a lot more than what you said. I know about loops and basic entity frameworks. I have been using those for ages.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #17 - Posted 2013-08-15 21:00:17 »

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.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #18 - Posted 2013-08-15 21:08:28 »

A: I did say. Right here:
Quote
<snip>
I need to be able to store a .class as a variable. I would use a base object, but I want to be able to init over one line, not make it, then set the x, and y. So I made this method. This way, I can create an object based of a type, which I pass into a class called GenTileMap, which is a Generated Tile Map. I pass in Grass.class as the default ground tile, and Stone.class as the default wall tile.
<snip>

Sorry if I cannot explain it right, but this is the best I can think of right now.

B: Well, do you have any ideas? I agreed it was uncomfortable, and sloppy. But really, what else can I do? I looked it up for 2 hours to no avail. Maybe someone else can answer the actual question?

EDIT: Sorry, mixed up the quote brackets.

Offline Jeremy
« Reply #19 - Posted 2013-08-15 21:09:58 »

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?

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #20 - Posted 2013-08-15 21:11:15 »

Well, I want to convert Autoboxes to primitives. I have a blunt force way that I fixed it with if-else statements. But I want to do exactly what the title infers. Then we got WAAAAY sidetracked.

Offline Jeremy
« Reply #21 - Posted 2013-08-15 21:12:01 »

Well, I want to convert Autoboxes to primitives. I have a blunt force way that I fixed it with if-else statements. But I want to do exactly what the title infers. Then we got WAAAAY sidetracked.

Why though?

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

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #22 - Posted 2013-08-15 21:14:06 »

Close, but I am pretty sure no.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #23 - Posted 2013-08-15 21:16:10 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
public enum Tile {
GRASS(),
STONE();

private Tile(){
}

}

1  
2  
3  
4  
5  
6  
7  
8  
public void createTile(Tile tile){
//create the tile

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

}

Exactly what is wrong with this?
Offline davedes
« Reply #24 - Posted 2013-08-15 21:18:34 »

Quote
What about it is so bad? I am just using reflection to save me some time and effort.
Well, reflection itself for this purpose is bad. Smiley

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.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #25 - Posted 2013-08-15 21:25:42 »

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.
Agreed.

<snip>
Exactly what is wrong with this?
Takes more time than my current method. I was doing that, but it was not good. I had to add an entry to Id.java every time I made a new kind of object. Also, It just felt sloppy, but not near as sloppy as all the if-else's. Which brings me back to my first question:
"Converting autoboxes to primitives"
That's all I want.

Offline BurntPizza
« Reply #26 - Posted 2013-08-15 21:30:51 »

"Converting autoboxes to primitives"
That's all I want.

Hmm, well last time I checked, that happens automatically, by definition.
1  
2  
Integer i = 20; //assigns primitive value to an object
int d = i; //assigns value from object to a primitive
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #27 - Posted 2013-08-15 21:44:43 »

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.
Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #28 - Posted 2013-08-15 22:01:50 »

"Converting autoboxes to primitives"
That's all I want.

Hmm, well last time I checked, that happens automatically, by definition.
1  
2  
Integer i = 20; //assigns primitive value to an object
int d = i; //assigns value from object to a primitive


No, not converting values themselves... I want to convert it from a Float, to float. I am using classes here, not values.

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.
Okay, I'll see what other games do.

Offline wessles

JGO Wizard


Medals: 66
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #29 - Posted 2013-08-15 22:11:30 »

Hm... How does minecraft do it? I googled it, and nothing useful came up.

Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (44 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!