java get wrapper class for primitive

java get wrapper class for primitive

In Java, each primitive type has a corresponding wrapper class that provides an object representation of the primitive value.

To get the wrapper class for a primitive type, you can use the following methods:

  • Boolean.valueOf(boolean) for boolean
  • Byte.valueOf(byte) for byte
  • Short.valueOf(short) for short
  • Integer.valueOf(int) for int
  • Long.valueOf(long) for long
  • Float.valueOf(float) for float
  • Double.valueOf(double) for double
  • Character.valueOf(char) for char

For example, to get the wrapper class for an int value, you can use the following code:

int i = 123;
Integer wrapper = Integer.valueOf(i);
Source:‮tual.www‬turi.com

The valueOf method returns an Integer object that wraps the primitive int value.

You can also use the parseInt method of the Integer class to parse a string representation of an int value and return an Integer object:

String str = "123";
Integer wrapper = Integer.parseInt(str);

Note that the wrapper classes are immutable, which means that once you create an object, you cannot modify its value. If you need to modify the value, you can create a new object with the modified value.

Created Time:2017-11-03 22:21:11  Author:lautturi