Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Does Number has doubleValue method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading 809 certification book from Ganesh  and Co. I came across TestNumber class on page 13.
here looks the code.

The code compiles successfully. But I have  two problems.
1. Does  Number types implement doubleValue.
2.  why the api has IntegerValue, floatValue.. etc, why it didnot have value method.
 
Marshal
Posts: 66980
255
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kiros haregewoine wrote:. . . 1. Does  Number types implement doubleValue.

Have you looked in the documentation comments for Number? Have you looked at the specification for unboxing conversion? What do you think is happening when you execute that line with += in?

2.  why the api has IntegerValue, floatValue.. etc, why it didnot have value method.

Have you looked in the documentation comments? Did you find an IntegerValue() method? Have you found the part with, “class Number,” just below the first horizontal line? What does it say there? Doesn't that answer your question?
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kiros haregewoine wrote:. . . The code compiles successfully. . . .

No, it doesn't. You haven't copied it correctly. You would be able to see the error for yourself if you indented it correctly.
 
kiros haregewoine
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

kiros haregewoine wrote:. . . The code compiles successfully. . . .

No, it doesn't. You haven't copied it correctly. You would be able to see the error for yourself if you indented it correctly.


yes, you are right. I just wrote from my mind. Here is the way the code goes.

My question:
1. why the above code compiles successfully?  
2. could the following code work


Additional comment: in the forum, it would have been better if save draft post exists along with submit.
Sorry, I am using terminal ide on my mobile and miss features.
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kiros haregewoine wrote:. .. . Here is the way the code goes. . . . 1. why the above code compiles successfully?  

It doesn't compile.

2. could the following code work
. . .

Have you looked up unboxing? I gave you the links to help you leanr; you will learn nothing if I spoon‑feed you.

Sorry, I am using terminal ide on my mobile and miss features.

I have never seen a save draft option on forum websites. Have you seen the preview button? I don't know anything about the mobile view, sorry.
 
kiros haregewoine
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

kiros haregewoine wrote:. . . . . .
sum+=num; //works with Integer type unboxing and widening, as you commented.
// would not it be better if Number has Value method which subclasses delegate  to their respective xxxValues. . . .


Thank you for your patience.
 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't quote the whole of an old post; nobody needs to read the same twxt twice. And it obscures your new question. I have removed some of the dupliation.

kiros haregewoine wrote:. . . . . .

I shall challenge you to tell us why that code now compiles. It is you who need to know that; I know that already.

sum+=num; //works with Integer type unboxing and widening, as you commented.

Does it? Please show us some code that will actually compile with sum += num; in.
I shall also let you work out how the compiler knows which “sum” is the variable name and which the method name.

// would not it be better if Number has Value method which subclasses delegate  to their respective xxxValues. . . .

Please show us an example of what you are thinking of. Maybe it would be better, but that isn't how the number classes were designed.

Thank you for your patience.

Think nothing of it.
 
kiros haregewoine
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the code now compiled?
Because static  method cannot access instance methods and fields.
Some code of sum+=num;

The Value method: I thought about it but the Value method cannot have different types of return. Thus, it      
may not work. I shoul revise the covariant return types.  

I will study also the link about names.


 
Campbell Ritchie
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That loosk good But you need to indent your code, otherwise you can confuse yourself about the struture of your programs.

kiros haregewoine wrote:. . . Because static  method cannot access instance methods and fields.

Beacause sum is now a local variable rather than a field

. . . //I find  it odd that  return variable name and method name having the same name. Hint: Java® Language Specification (=JLS) link.

Did you read that link? What did it say about variables and methods with the same name?

. . . sum+= num; . . .

That now works because you can apply unboxing to an Integer object and use it as an int.


The Value method: I thought about it but the Value method cannot have different types of return. Thus, it may not work. I shoul revise the covariant return types.   . . .

Those methods in the Number class date from the days before covariant return, so they could not be written like that. Also, Long and INteger and BigDecimal are subtypes of Number, but int and long are not subtypes of Number, so you an't give such method covariant return type. What would you return from a BigDecimal's value() method? There isn't a primitive orresponding to BigDecimal. The range of a BigDecimaal is (in theory) unlimited, so it is possible to go beyond the range of a double, so double wouldn't be a good return type for a hypothetical BigDeimal#value() method.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!