Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Tue Jun 17, 2014 5:05 am
by Nisim123
The 3rd option given to this question is:
Long.longValue(mStr);
The Exam editor's note on this option is:
longValue is a non-static method in Long class.
My question is what prevents us from using a non- static method
in this context?
:?

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Tue Jun 17, 2014 5:50 am
by admin
It is not clear what are you trying to do. Option 3 will not compile because of the reason given. Please post exact code that you think should work.

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jan 20, 2016 4:18 pm
by RalucaD
Maybe a more precise explanation to the 3rd option could be:
the signature of longValue() method does not take a String as a parameter

public long longValue()
Returns the value of this Long as a long value.
Specified by:
longValue in class Number
Returns:
the numeric value represented by this object after conversion to type long.

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Sun May 29, 2016 6:12 am
by JaredTse
Hi All,

I have a question in related to this question when it comes to equality. And I was looking into the static method valueOf of Integer. And while I was experimenting that this returns true.

Integer bl = Integer.valueOf(123);
Integer bl2 = Integer.valueOf(123);
System.out.println( bl == bl2 ); // => false

valueOf method

Code: Select all

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }


Isn't this supposed to return since both are returning an instance of Integer ?

Thanks

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Sun May 29, 2016 9:01 am
by admin
It does print true. Not sure how are you running it but always use command line.
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Sun May 29, 2016 11:58 am
by JaredTse
Hi Paul,

Yes, you are right it does return - true -, The comment is what I expected to get, but printed true. However, upon further investigation and experiment I realised that if the value of int is less than 127 the JVM behaves differently. Where it uses, some sort of Cached data.

For example the following code returns false because is more than 127:

Integer a = Integer.valueOf(129);
Integer b = Integer.valueOf(129);
System.out.println( a == b ); // => false

and this is true:

Integer a = Integer.valueOf(123);
Integer b = Integer.valueOf(123);
System.out.println( a == b );

- Thanks

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Sun May 29, 2016 12:20 pm
by admin
That is correct. The idea is to not create multiple Integer objects for values between -128 to 127.
See this: http://stackoverflow.com/questions/3131 ... ng-in-java

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jun 16, 2021 9:32 am
by mihanoidy
Could anybody clarify about 3rd option
Long.longValue(mStr);
why it's not correct in this question?
Given:
String mStr = "123";
long m = // 1
Which of the following options when put at //1 will assign 123 to m?
I've tried to compile that code and it's runned without any problem.

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jun 16, 2021 9:57 am
by admin
Are you sure you tried the correct code because it doesn't compile.
Use command line to test: https://enthuware.com/oca-ocp-java-cert ... cation-ide

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jun 16, 2021 10:35 am
by mihanoidy
Oh, sorry, I confused and wrote in my code "Long.valueOf(mStr)" instead of "Long.longValue(mStr)". Thanks for helping!

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jun 08, 2022 1:03 am
by herngyih
Didn't expect that we need to know about longValue() method as well. Is knowing valueOf() and parseLong() only adequate for the exam?

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Wed Jun 08, 2022 2:13 am
by admin
You need to know about all variants that create wrapper objects.

Re: About Question enthuware.ocajp.i.v7.2.1126 :

Posted: Tue Jan 23, 2024 1:20 am
by agupta108
Some explaination could be added to Option B: " Long.parseLong(mStr);".