Skip to content

Primitive Data Types: Jbyte #34

@nsanitate

Description

@nsanitate

Implement the byte primitive data type that allow to overcome the differences between Java and JavaScript.

The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

Expected Behavior

  • Declaring a variable to refer to a byte
  • Creating, Initializing, and Accessing a byte
    • initialize with hexadecimal
    • initialize with binary
  • Equality and Relational Operators
    • equal to
    • not equal to
    • greater than
    • greater than or equal to
    • less than
    • less than or equal to
  • Arithmetic Operators
    • additive operator
    • subtraction operator
    • multiplication operator
    • division operator
    • remainder operator
  • Unary Operators
    • unary plus operator
    • unary minus operator
    • increment operator
    • decrement operator
  • Exception
    • incompatible types: possible lossy conversion with short, int, long, float o double
    • incompatible types: cannot be converted to byte

Acceptance test

Declaring a variable to refer to a byte

let aByte: Jbyte;

Creating, Initializing, and Accessing a byte

const aByte = jbyte('26');
console.log("Byte: " + aByte);

// initialize with hexadecimal
const anHex = jbyte('0x1a');
console.log("Hex: " + anHex);

// initialize with binary
const aBin = jbyte('0b11010');
console.log("Bin: " + aBin);

Output:

Byte: 26
Hex: 26
Bin: 26

Equality and Relational Operators

const value1 = jbyte('1');
const value2 = jbyte('2');

if(is(value1.eq(value2)))
   console.log("value1 == value2");

if(is(value1.ne(value2)))
   console.log("value1 != value2");

if(is(value1.gt(value2)))
   console.log("value1 > value2");

if(is(value1.ge(value2)))
   console.log("value1 > value2");

if(is(value1.lt(value2)))
   console.log("value1 < value2");

if(is(value1.le(value2)))
   console.log("value1 <= value2");

Output:

value1 != value2
value1 <  value2
value1 <= value2

Arithmetic Operators

const value8 = jbyte('8');
const value7 = jbyte('7');
let result: Jbyte;

result = value8.add(value7);
console.log("8 + 7 = " + result);

result = value8.sub(value7);
console.log("8 - 7 = " + result);

result = value8.mul(value7);
console.log("8 * 7 = " + result);

result = value8.div(value7);
console.log("8 / 7 = " + result);

result = value8.mod(value7);
console.log("8 % 7 = " + result);

Output:

8 + 7 = 15
8 - 7 = 1
8 * 7 = 56
8 / 7 = 1
8 % 7 = 1

Unary Operators

const value1 = jbyte('-1');
let result: Jbyte;

result = value1.plus();
console.log("+(-1) = " + result);

value1.inc();
console.log("++(-1) = " + value1);

value1.dec();
console.log("--(0) = " + value1);

result = value1.minus();
console.log("-(-1) = " + result);

Output:

+(-1) = -1
++(-1) = 0
--(0) = -1
-(-1) = 1

Exceptions

const aByte = jbyte('129');
const anotherByte = jbyte('ten');

Output:

ERROR: incompatible types: possible lossy conversion from int to byte
ERROR: incompatible types: String cannot be converted to byte

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions