1

I have a doubt in Java wrapper class like Integer, Character. I know that when we declare a class Abc and can create an object to it in Java like

Abc a = new Abc(); 

It instantiates a with reference to Abc class and in that we have fields that contain values for variables. My doubt being, when we create Integer class like:

Integer i = 5;

How is it pointing to value 5? Should not it contain a field which contains its value and point to Integer object like:

static int value; // To hold value 5 for Integer class
4
  • we cannot declare class like Abc a = new Abc();, in Java. You are creating an object Commented Mar 13, 2018 at 10:11
  • 1
    This is called autoboxing, when you write Integer i = 5; your compiler at compile time change it to Integer i = Integer.valueOf(5); Commented Mar 13, 2018 at 10:11
  • 1
    Although this question requires just a brief research, downvoting is not a way to encourage new users. Why don't you leave a comment? Commented Mar 13, 2018 at 10:12
  • Sorry @Andrew I got my mistake. This can be a useful link stackoverflow.com/questions/9030817/… Commented Mar 13, 2018 at 10:58

3 Answers 3

1

How is it pointing to value 5?

It's pointing to an Integer instance which holds that primitive value.

Integer i = Integer.valueOf(5);

which is a bit optimised version of new Integer(5) (this one is deprecated since Java 9). The process is called autoboxing and is made by the compiler.

Should not it contain a field which contains its value?

It does contain a field. Though, it shouldn't be static and shared for the whole class.

private final int value;

Actually, Integer.valueOf(5) will be taken from the cache as well as any value in the range [-128, 127] unless a greater java.lang.Integer.IntegerCache.high value is specified.

Sign up to request clarification or add additional context in comments.

2 Comments

Sir, Are not final variables supposed to be initialized in constructors or non-static initializers only?
@Handmadegiftsco, yes, they are. But a static method could call a constructor or refer to another static member (have a look at static final Integer cache[] inside private static class IntegerCache)
1

To quote the Javadoc for Integer

An object of type Integer contains a single field whose type is int.

So yes, there is a field of type int in the object returned by the autoboxing. The Integer object returned by autoboxing might be

  • newly created, or
  • returned from a cache;

but it will be the same as the object returned by the static valueOf method of the Integer class.

The answers to this question may help you understand when valueOf (and therefore autoboxing) creates a new object, and when it returns an existing object.

Comments

0
Integer i = 5;

from this, the compiler will make Integer i = Integer.valueOf(5);(Autoboxing)

See also Oracle Docs in

Autoboxing and Unboxing

The Numbers Classes

Immutable Objects

2 Comments

Sir how can a reference variable for class like i in this case point to value?
You can't pass a wrapper class by reference. Because are immutable, for that, you will need return the value from your method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.