Class wrapper class in Java

I come from the world of PHP and I'm so confused about how to think when you declare objects in java.

so when you traditionally do this:

Rectangle rect = new Rectangle(); 

reason rect is the Rectangle data type.

According to the java tutorial page, the number wrapper class is a subclass of Number. So this is a class, but when you create it, the tutorial did it like this:

  Integer x; x = 12; 

Why this does not look like the traditional way:

  Integer x = new Integer(12); or Integer x = new Integer(); 

And here is another example:

  String s = new Integer(i).toString(); 

So here s is a String object. what i get. But you got a new Integer (i). Why new? What this means and what happens when it sends an "i" to the constructor. Where can I see what the constructor with parameters does in the java API?

Many questions, but could not find sources on the network explaining this.

+6
java
source share
5 answers

First, you can just use primitive types:

 int x = 12; double d = 3.3456; 

Secondly, you can optionally use object wrappers for these numbers:

 Integer x = new Integer(12); Double d = new Double(3.3456); 

Third, if you need a string representation, you can do this simply:

 String s = Integer.toString(12); 

or simply:

 int x; String s = "x = " + x; 

or even printf() as syntax:

 int x = 12; String s = String.format("x = %d", x); 

Finally, Java from version 5 supports automatic boxing and unpacking, which can confuse you if you do not expect this. For example:

 Integer i = 1234; // autoboxes int to Integer 

and

 int i = new Integer(1234); // autounboxes Integer to an int 

Just use primitive types if you don't need Number wrappers. The most common reason for using them is to place them in collections ( List s, etc.).

+10
source share

What you see in the first example is called autoboxing / autounboxing. Java (starting with version 5) will automatically convert between 5 and Integer.valueOf(5) , which builds a shell of type Integer from the primitive int. But the latter ( Integer x = new Integer(12) ) is absolutely correct.

However, in the second case, this does not work if you want to write something like 5.toString() . Auto-boxing occurs only when a primitive is assigned to a wrapper type or when a primitive is transferred where the shell type is expected, etc. Primitive types are not objects and, therefore, do not have properties that can be referenced.

Re: "why new", because all reference (non-primitive) types in Java are allocated dynamically, on the heap. So (autoboxing aside) the only way to get Integer (or another reference type) is to explicitly allocate space for one.

+7
source share

Some things have not received other answers:

 Integer x = new Integer(); 

This will not work, because the Integer class does not have a no-args constructor (what will the final value of the int object be?).

 String s = new Integer(i).toString(); 

So here s is a String object. what i get. But you got a new Integer (i). Why new? What this means and what happens when it sends an 'i' to the constructor. Where can I see what the constructor does with parameters in the java API?

This means the same as above: an Integer object is created, the contents of the i variable (possibly the loop index) are passed as a constructor argument, and then the result toString() method is called, which displays a string representation of the number.

As for where you can look for such things, the Java API details all aspects of the standard API. If this is not enough, the Sun JDK comes with the full source code for the standard API. When you install the JDK, you have the opportunity to get this source code, and most IDEs will allow you to easily go to it.

+2
source share

In addition to objects, Java also includes primitive data types such as boolean, byte, int, long, char, float, double. All of them are mentioned in lower case and are used with literal values. They are always passed by value.

There are also matching Number Boolean, Byte, Integer, Long, etc. objects, which, like all objects, are passed by reference instead. Since these are objects, not primitives, there are various characteristics and performance characteristics that must be observed.

Modern Java has automatic boxing and automatic unpacking for silent conversion between primitives and objects, which allow the syntax, as you used Integer i = 5 , to autobox primitive 5 in Object Integer . Previously, you had to either use a primitive int i = 5 , or explicit syntax based on Integer i = new Integer(5) objects, and not mix types as you can now.

+1
source share

This is a weird behavior:

 Integer x; x = 12; 

It is the result of java autoboxing .

Some story:

Prior to Java 1.5, it was not allowed. You had primitives ( int, char, byte, boolean, float, double,long ), and the rest in the Java world were classes (including compatible wrappers: Integer, Character, Byte, Booelan, Float, Double, Long ).

The main Java data structures worked with objects, so if you needed to store numbers in a list, you had to "wrap" your value (wrappers are just ordinary classes that contain a primitive type)

For example, it could be my own int shell:

  public class Entero { // integer in spanish... :P private final int wrappedInt; public Entero( int i ) { this.wrappedInt = i; } public int getEntero() { return wrappedInt; } } 

Nothing special, in general terms, how the wrapper classes are implemented (of course, there are many useful methods)

So, if you want to use it in a list (which contains only objects), you need:

  List list = // get the list from somewhere.... list.add( new Integer( 1024 ) ); // wrap it .... // use the list and at some point iterate it: Iterator iterator = list.iterator(); while( iterator.hasNext() ) { Integer e = ( Integer ) iterator.next(); // unwrap it i = e.intValue(); } 

Call

  list.add( 1024 ) 

Immediately impossible, because 1024 is an int literal, not an object.

Tons of code were written as follows: over the years.

Since Java 1.5 added autoboxing, which is basically syntactic sugar, now the β€œnew Integer (i) /integer.intValue ()” is introduced under the hood by the compiler, and the code becomes:

 list.add( 1024 ); // wrapped in the compiled code in the the .class file that is. .... Iterator i = list.iterator(); while( i.hasNext() ) { int i = ( Integer ) i.next(); // unwrapped for you by the compiler under the hood } 

Removing the wrapping process from the source code.

In addition, with the help of generics, you also saved the cast:

  List<Integer> list = .... // <- you still have to say the list is of "Integers" not "int" .... Iterator<Integer> i = list.iterator(); // The iterator has to use the "<Integer>" generic mark while( i.hasNext() ){ int x = i.next(); // but you can get the value directly. } 

Mostly generics is a sign to say, β€œCheck what is being used, like this type and don’t bother me with casting anymore,” but generics are another topic.

+1
source share

All Articles