The Integer And Double Classes

In this article, we will discuss integer and The main purpose of these rapper classes is to exchange ancient data and objects.

Let's look at an example using the array list class.

If you haven't seen the array list class before, you can think of it as a place to save a list of items.

In this example, we are storing numeric data in the entry list.

ArrayList<Integer> myArrayList = new ArrayList<>(); myArrayList.add(5); int num = myArrayList.get(0); // Asking for the first (technically "zero-th") number in myArrayList. num should now hold 5.

This example shows that any How to use the integer wrapper class.

Conversion in rapper classes and thus applies to all types of rapper classes: integer Double 32MWV code "2dMWV" "

This exchange also occurs when starting a rapper class object instance.

Double wrapper1 = 23.456; Integer wrapper2 = 3; Double wrapper3 = new Double(13.57); Integer wrapper4 = new Integer(7); Double wrapper5 = Double.valueOf(30.59); Integer wrapper6 = Integer.valueOf(15);

Each of these instances causes the compiler to autobox the initial value in an object of the correct wrapped class.

Here are some examples where we inbox the values:

// Autoboxing into Integers Integer wrapper = 3; Integer wrapper2 = Integer.valueOf(15); // Unboxing back to ints int primitiveInt = wrapper; int primitiveInt2 = wrapper2.intValue();

As you can see, the easiest way to set inbox or autobox values ​​is to easily assign a rapper object to an old value, or a rapper object to an old variable. Be assigned.

These previous examples also showed some of the methods that exist in rapper classes.

Leave a Comment

Your email address will not be published. Required fields are marked *