Junior — Middle
What methods for working with numerical values do you know in classes for numbers?
sobes.tech AI
Answer from AI
In Java, wrapper classes for numbers (such as Integer, Double, Long, etc.) provide several useful methods for working with numeric values:
- parseXXX(String s) — converts a string to a primitive type (e.g.,
Integer.parseInt("123")returnsint123). - valueOf(String s) — returns a wrapper object from a string (e.g.,
Integer.valueOf("123")returns an Integer object). - xxxValue() — methods to obtain the primitive value from an object (e.g.,
intValue(),doubleValue()). - compareTo() — compares two numeric objects.
- toString() — converts a number to a string.
- MAX_VALUE and MIN_VALUE — constants with the maximum and minimum values of the type.
Example:
Integer num = Integer.valueOf("42");
int primitive = num.intValue();
int parsed = Integer.parseInt("123");
int comparison = num.compareTo(100); // >0 if num > 100