Get startedGet started for free

Using Wrappers

Because wrappers and primitives often look very similar in code, it takes some practice to work with wrappers. Wrapper classes come with a number of important methods that make comparing, converting, and generally working with data easier. In this exercise, you will experiment with several wrapper methods and fields.

This exercise is part of the course

Data Types and Exceptions in Java

View Course

Exercise instructions

  • Compare the value of pi to eulers value using the .compareTo() method.
  • Print out the maximum allowed short primitive value.
  • Parse the string "true" to a boolean primitive using Boolean's .parseBoolean() method.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class ExamineWrappers {

	public static void main(String[] args) {
		Double pi = 3.14;
		Double eulers = 2.71828;
		// Compare pi's value to euler's
		int x = ____.____(____);
		System.out.println(x);

		// Use Short to print out the maximum short
		System.out.println(____.____);

		// Use Boolean to turn string "true" to a boolean
		boolean y = ____.____("____");
		System.out.println(y);
	}
}
Edit and Run Code