About Question enthuware.ocajp.i.v7.2.1027 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1027 :
Isn't Integer mutable if the value lies between -128 and 127. And so the increment here will acutally affect the Integer variable?
-
- Site Admin
- Posts: 10325
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1027 :
No, none of the wrapper classes is mutable.
I think you are confusing interning with mutability. Integer objects for integers between -128 to 127 are interened. I.e. if you have the following two lines:
Integer i1 = 100;
Integer i2 = 100;
then i1 and i2 would be referring to the same Integer object.
HTH,
Paul.
I think you are confusing interning with mutability. Integer objects for integers between -128 to 127 are interened. I.e. if you have the following two lines:
Integer i1 = 100;
Integer i2 = 100;
then i1 and i2 would be referring to the same Integer object.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Mar 23, 2021 3:27 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1027 :
public class TestClass{
public static Integer wiggler(Integer x){
Integer y = x + 10;
x++;
System.out.println(x);
return y;
}
public static void main(String[] args){
Integer dataWrapper = new Integer(5);
Integer value = wiggler(dataWrapper);
System.out.println(dataWrapper+value);
}
}
here the o/p: 6,20 so q is how 6 is coming since wrappers are immutable.
public static Integer wiggler(Integer x){
Integer y = x + 10;
x++;
System.out.println(x);
return y;
}
public static void main(String[] args){
Integer dataWrapper = new Integer(5);
Integer value = wiggler(dataWrapper);
System.out.println(dataWrapper+value);
}
}
here the o/p: 6,20 so q is how 6 is coming since wrappers are immutable.
-
- Posts: 28
- Joined: Fri Feb 09, 2018 11:50 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1027 :
I don't believe the code will compile (at least the version I have, where Integer.valueof(5,16) is called).
There is no valueOf method that takes two ints, defined on Integer, in Java 21.
The closest is public static Integer valueOf(String s, int radix): https://docs.oracle.com/en/java/javase/ ... tring,int)
Even the explanation uses the (String, int) signature:
3. Note that the exam expects you to know the basics of hexadecimal numbers. Specifically, you should know that the hexadecimal system has 16 digits - 0 to 9 and a, b, c, d, e , f. Thus, Integer.valueof("a", 16), will return 10. The digit 5 in hexadecimal is the same as 5 in decimal though.
There is no valueOf method that takes two ints, defined on Integer, in Java 21.
The closest is public static Integer valueOf(String s, int radix): https://docs.oracle.com/en/java/javase/ ... tring,int)
Even the explanation uses the (String, int) signature:
3. Note that the exam expects you to know the basics of hexadecimal numbers. Specifically, you should know that the hexadecimal system has 16 digits - 0 to 9 and a, b, c, d, e , f. Thus, Integer.valueof("a", 16), will return 10. The digit 5 in hexadecimal is the same as 5 in decimal though.
-
- Site Admin
- Posts: 10325
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1027 :
Yes, it should be valueOf("5", 16). This was mistakenly updated from 5 to to 5, 16 in the most recent version. Integer.valueOf(5) is fine but Integer.valueOf(5, 16) is not.
Fixed.
thank you for your feedback!
Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 13 guests