Primitive datatype type not initializing boolean correctly

I need to create a wrapper class to store any primitive data type. I created a class and it works as intended for everything but boolean. This is my code:

byte a; // the byte in primitive
short b; // the short
int c, j; // int c is is int, j is the counter for Primitive to tell what dataType it is
long d; // the long
float e; //the float
double f; // the double
boolean g; // the boolean, with which I am having problems
char h; // the char
...;
public Primitive(boolean i) {
    g = i; 
    j = 6;
}

      

The top method works as intended, however, when I try to use my copy method, it makes the boolean equal to true, no matter if I am true or false.

public Primitive(Primitive i){
    switch (i.j){
    case 0: a = i.a; break;
    case 1: b = i.b; break;
    case 2: c = i.c; break;
    case 3: d = i.d; break;
    case 4: e = i.e; break;
    case 5: f = i.f; break;
    case 6: g = i.g; break;
    case 7: h = i.h; break;
    }
}

      

Any help would be greatly appreciated.

EDIT **

After testing the copy constructor for all cases, I found that the copy constructor does not work for short, boolean and char data types. It works for everything else. This is my modified code:

public class Primitive {
    private byte bytes;
    private short shorts;
    private int integer;
    private final int DATATYPE;
    private long longs;
    private float floaters;
    private double doubles;
    private boolean bools;
    private char character;
    Compare compare = new Compare();
    /**************************************************|
    |*                  Constructors                  *|
    |**************************************************/
    public Primitive(byte i) {
        bytes = i;
        DATATYPE = 0;
    }
    public Primitive(short i) {
        shorts = i;
        DATATYPE = 1;
    }
    public Primitive(int i) {
        integer = i;
        DATATYPE = 2;
    }
    public Primitive(long i) {
        longs = i;
        DATATYPE = 3;
    }
    public Primitive(float i) {
        floaters = i;
        DATATYPE = 4;
    }
    public Primitive(double i) {
        doubles = i;
        DATATYPE = 5;
    }
    public Primitive(boolean i) {
        bools = i; 
        DATATYPE = 6;
    }
    public Primitive(char i) {
        character = i;
        DATATYPE = 7;
    }
    public Primitive(Primitive i){
        switch (i.DATATYPE){
        case 0: bytes = i.bytes; break;
        case 1: shorts = i.shorts; break;
        case 2: integer = i.integer; break;
        case 3: longs = i.longs; break;
        case 4: floaters = i.floaters; break;
        case 5: doubles = i.doubles; break;
        case 6: bools = i.bools; break;
        case 7: character = i.character; break;
        }
        DATATYPE = i.DATATYPE;
    }
    ...;
}

      

I was about to try ENUM, but I forgot how to use it. That is, and I think the integer will be easier to manipulate than the ENUM.

+3


source to share


2 answers


As mentioned above, the key error is not initialized j

. By recognizing that it j

must be set every time in every constructor, you can mark j

"final".

Also, the variables you have are meant to be confusing. This is especially important for j

. Firstly, burying the check variable j

as a 1 digit variable name with your domain data is c

very confusing. You don't have to hammer them so hard (the compiler does this). ONE LETTER VARIABLE CONVENTION NAMES.

Using int to know the type is really old school. Enum would be more appropriate.



(and as another responder pointed out, this should be homework as it uses the built-in primitive wrapper classes (Integer for int, Boolean for boolean, ...)

If you choose to keep the code mostly as is, at least rename j

and separate it from c

.

+2


source


You forget to set the value of the j

new primitive. Add the following after the next switch:

this.j = i.j

      



Without it, the second time you try to make a copy of the constructor, it falls back to case 0. I suspect it works for other values ​​just because you haven't tested this case yet, but it's hard to tell without looking at this code.

+2


source







All Articles