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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

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

Post 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?
:?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

RalucaD
Posts: 3
Joined: Mon Jan 18, 2016 3:17 pm
Contact:

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

Post 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.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

It does print true. Not sure how are you running it but always use command line.
-Paul.
If you like our products and services, please help us by posting your review here.

JaredTse
Posts: 20
Joined: Sat Apr 23, 2016 2:52 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

mihanoidy
Posts: 2
Joined: Wed Jun 16, 2021 9:09 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

mihanoidy
Posts: 2
Joined: Wed Jun 16, 2021 9:09 am
Contact:

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

Post by mihanoidy »

Oh, sorry, I confused and wrote in my code "Long.valueOf(mStr)" instead of "Long.longValue(mStr)". Thanks for helping!

herngyih
Posts: 6
Joined: Fri Jun 03, 2022 9:14 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You need to know about all variants that create wrapper objects.
If you like our products and services, please help us by posting your review here.

agupta108
Posts: 4
Joined: Sat Jan 20, 2024 9:26 am
Contact:

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

Post by agupta108 »

Some explaination could be added to Option B: " Long.parseLong(mStr);".

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 36 guests