简体   繁体   中英

Data type primitive object not initializing boolean correctly

I have to create a wrapper class to hold any primitive data type. I created the class, and it works as intended for everything except the 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 my copy method, it makes the boolean value equal to true, regardless of whether i is 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 the cases, I have discovered that the copy constructor does not work for the data types short, boolean, and char. 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 going to attempt an ENUM, but I have forgotten how to use it. That, and I think that an integer would be easier to manipulate than an ENUM.

You're forgetting to set the j value of the new Primitive. Add the following after your switch:

this.j = i.j

Without this, the second time you try to make a constructor copy, it falls back to case 0. I suspect that it's working for the other values only because you haven't tested this case yet, but it's hard to say without seeing that code.

As mentioned above, the key error is failing to initialize j . Recognizing that j has to be set every time in every constructor, you might mark j "final".

Additionally the variables you have are designed to confuse. This is especially the case for j . first off, burying a control variable j as a 1 letter variable name with your domain data c is very confusing. No need to cram them so tight (the compiler does that). ONE LETTER VARIABLE NAMES ARE CONFUSING.

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

(and as another responder noted; this must be homework, since the built-in primitive wrapper classes (Integer for int, Boolean for boolean, ...) are designed for this and baked into the language.)

If you decide to keep the code mostly as is, at least rename j and separate it from c .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM