Java Data Types
Primitive Data Types
- int: whole numbers (e.g., 42)
- double / float: decimal numbers
- boolean: true or false
- char: a single character
- long, short, byte: other integer sizes for specific memory needs
Reference (Non-Primitive) Data Types
Includes String, Arrays, and Objects — these store references to data rather than the raw value itself, and can be null.
Why Testers Should Know This
Understanding data types is essential when writing Rest Assured assertions, since a response field's actual data type (e.g., a number vs a string) must match what the assertion expects.
Sizes, Ranges and Default Values
Every primitive type in Java has a fixed size on every platform, which keeps behaviour predictable.
| Type | Size | Range or values | Default (as a field) |
|---|---|---|---|
| byte | 8 bits | -128 to 127 | 0 |
| short | 16 bits | -32,768 to 32,767 | 0 |
| int | 32 bits | about -2.1 billion to 2.1 billion | 0 |
| long | 64 bits | very large whole numbers | 0L |
| float | 32 bits | about 6 to 7 significant digits | 0.0f |
| double | 64 bits | about 15 to 16 significant digits | 0.0 |
| char | 16 bits | a single UTF-16 code unit | '\u0000' |
| boolean | JVM-dependent | true or false | false |
Defaults apply only to class fields. Local variables inside a method have no default and must be assigned before use, or the code will not compile.
Wrapper Classes and the == Trap
Collections and many libraries work with objects, so Java wraps primitives in classes like Integer, Double and Boolean. Conversion happens automatically, called autoboxing. The danger is comparing wrapper objects with ==, which checks whether two references point to the same object, not whether the values match.
Integer a = 100, b = 100;
Integer c = 1000, d = 1000;
System.out.println(a == b); // true (small values are cached)
System.out.println(c == d); // false (different objects)
System.out.println(c.equals(d)); // true (compares values)
String s1 = "pass";
String s2 = new String("pass");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
In test code, always use equals() or your assertion library for objects and strings. A check that passes for small numbers and fails for large ones is very confusing to debug.
Matching Types in Rest Assured Assertions
Suppose an API returns {"id": 101, "name": "Asha", "active": true, "balance": 2500.75}. Each matcher must use the type Rest Assured produces when it parses the JSON.
given()
.baseUri("https://api.example.com")
.when()
.get("/users/101")
.then()
.statusCode(200)
.body("id", equalTo(101)) // int, not "101"
.body("name", equalTo("Asha")) // String
.body("active", equalTo(true)) // boolean, not "true"
.body("balance", equalTo(2500.75f)); // float by default!
By default, Rest Assured's JsonPath reads decimal numbers as float, so equalTo(2500.75), which is a double, fails even though the numbers look the same. Either add the f suffix or configure JsonPath to return double or BigDecimal. Whole numbers too large for int come back as long, so a matcher like equalTo(9876543210L) is needed for big IDs.
Checklist for Testers
- Use
longfor timestamps in milliseconds and large IDs. - Avoid
floatanddoublefor money; preferBigDecimalfor exact comparisons. - Compare strings and wrappers with
equals(), never==. - Check that a null field is really null and not the string "null".
- When an assertion fails with matching-looking values, suspect a type mismatch first.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.