About Question enthuware.ocajp.i.v7.2.1259 :
Moderator: admin
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1259 :
Would explain the scenario where the wrapper class interned the values between -128 and 127? My apologies for being vague but I do not understand it enough to be more clear.
Apparently it does not applies in this case "new Integer(1);". I thought it would be interned for both Integer and Long since it is below the range described earlier.
Apparently it does not applies in this case "new Integer(1);". I thought it would be interned for both Integer and Long since it is below the range described earlier.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Well, you can use the same code and replace the lines
Integer i = new Integer(1) ;
Long m = new Long(1);
with
Integer i = 1 ; //instead of 1, you may try with other values as well.
Integer m = 1;
Interning does not happen when you do a new of wrapper classes.
HTH,
Paul.
Integer i = new Integer(1) ;
Long m = new Long(1);
with
Integer i = 1 ; //instead of 1, you may try with other values as well.
Integer m = 1;
Interning does not happen when you do a new of wrapper classes.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Jul 24, 2016 6:04 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
As the equals() method could be overriden, we cannot predict the behaviour in the IF statement.
In this case, Why the answer can not be "None of the above"??
In this case, Why the answer can not be "None of the above"??
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
All wrapper classes are final. So you can't override equals method.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Here is the official declaration for "equals" method in Integer class:
"Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object."
but it our example we have Integer object and Long object. So why compile time error is not thrown?
"Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object."
but it our example we have Integer object and Long object. So why compile time error is not thrown?
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Or is it ok to pass any Object, but the method will return true if and only if passed value is an Integer.class?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Yes, since equals method is defined in Object class and that equals method accepts Object as an argument, any class that overrides the equals method must accept any object as an argument. There will be NO compilation error for this reason. However, at run time, the overridden equals method may check the type of the object passed and its value and then return the result true or false accordingly.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Hi Paul,
can you explain why this is allowed
short s = 10;
Short S = 10; //why dont need cast?
compile error:
long l = 10;
Long L = 10; //has to be 10L or (long)10
By assigning a primitive to a wrapper class variable (autoboxing) the compiler converts it to:
Short S=Short.valueOf((short)10); //here you need explicit cast
when do you need to explicit cast it and when not?
can you explain why this is allowed
short s = 10;
Short S = 10; //why dont need cast?
compile error:
long l = 10;
Long L = 10; //has to be 10L or (long)10
By assigning a primitive to a wrapper class variable (autoboxing) the compiler converts it to:
Short S=Short.valueOf((short)10); //here you need explicit cast
when do you need to explicit cast it and when not?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
Compiler can and does consider compile time constant values to determine whether the assignment is valid or not. 10 is an int but it is also a compile time constant that fits into a short, so the compiler performs a narrowing primitive conversion from int to short without requiring a cast. That is why short s = 10; compiles fine.
Short S = 10; compiles fine because section 5.2 allows a narrowing primitive conversion followed by a boxing conversion if the value of the constant expression is representable in the type short and the target variable is of type Short.
long l = 10; compiles fine because int is a smaller primitive type than long. There is no complication here.
The reason why Long L = 10; does not compile is tricky. It does not compile because it is not one of the allowed conversions as given by JLS section 5.2. Assigning 10 to Long will require a primitive widening and then a boxing conversion. But the specification does not allow widening+boxing (unlike in the case of Short, where it allows narrowing+boxing).
All this is explained in detail here: https://docs.oracle.com/javase/specs/jl ... jls-5.html
Short S = 10; compiles fine because section 5.2 allows a narrowing primitive conversion followed by a boxing conversion if the value of the constant expression is representable in the type short and the target variable is of type Short.
long l = 10; compiles fine because int is a smaller primitive type than long. There is no complication here.
The reason why Long L = 10; does not compile is tricky. It does not compile because it is not one of the allowed conversions as given by JLS section 5.2. Assigning 10 to Long will require a primitive widening and then a boxing conversion. But the specification does not allow widening+boxing (unlike in the case of Short, where it allows narrowing+boxing).
All this is explained in detail here: https://docs.oracle.com/javase/specs/jl ... jls-5.html
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
I have a similar doubt as in the previous post, only concerning constructor in this question.... Long(long value) ....
Long l = new Long(1); works fine. But is it not that I have widening first (from int to long) and then boxing (long to Long), which is not allowed. Yet it works in this case, unless I understand something wrongly.
I'd appreciate if you could clarify it.
Long l = new Long(1); works fine. But is it not that I have widening first (from int to long) and then boxing (long to Long), which is not allowed. Yet it works in this case, unless I understand something wrongly.
I'd appreciate if you could clarify it.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1259 :
When you do new Long(1);, 1 is indeed widened to long. But why do you think it is further boxed into Long?? The Long(long ) constructor is used.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Who is online
Users browsing this forum: Google [Bot] and 63 guests