This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Double" and "double" - what's the difference?

 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The simplest program of all times! And yet I get some random error that I'm unable to fixed even after half an hour. The complier give me an error on the line x = Double.parseDouble(br.readLine());

incompatible types
found : double
required: java.lang.Double

What the difference? Double or double? What should I do to make this work
 
Reddy Prasad Kurapati Venkata
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double is a class.
double is a key word used to store integer or floating point number.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double is primitive and Double is wrapper class.

I looked at the signature of parseDouble() method:



It returns double, not Double. That should solve your problem.

You should have got a compile time error because of this.

However, if you are using JDK 1.5 or above then you would not get this problem.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ankur rathi:


You should have got a compile time error because of this.


Opps... You got compile time error only. The error confused me.

It should be:

found: java.lang.Double
expected or required: double

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ankur rathi:


Opps... You got compile time error only. The error confused me.

It should be:

found: java.lang.Double
expected or required: double




The compiler parsed the left hand side of the statement first and found a Double, therefore it expects the right hand side of the statement to also be a Double, but it's not - it's a double. Therefore the error is correct, it was expecting a Double but found a double.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:



The compiler parsed the left hand side of the statement first and found a Double, therefore it expects the right hand side of the statement to also be a Double, but it's not - it's a double. Therefore the error is correct, it was expecting a Double but found a double.


Hmmmm. Thanks Joanne.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when I'm expecting Double, I can get an double, right?
An the inverse is correct: if I have an pimitive variable double, I can get an object Double, without casting.

So, I don't understand why the topic's creator go wrong.



Take a loot at this code down here:



So, what's the difference?
Here it worked just fine. I'm using Java 6.
Hugs.
[ January 31, 2008: Message edited by: Andre Brito ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:
The compiler parsed the left hand side of the statement first and found a Double, therefore it expects the right hand side of the statement to also be a Double, but it's not - it's a double. Therefore the error is correct, it was expecting a Double but found a double.


It is wrong! The assignment always happens from Right to Left and NOT the other way around. In that case, the compiler expects the "double" primitive variable on the left hand side as the static method "parseDouble(String)" of class Double returns the primitive double.

As such, as ankur pointed out, had the code been compiled into JDK 1.5 compiler it would not have thrown an error for the assignment. I believe the code was compiled by using 1.4 compiler. That's when the compiler should have given an error saying that "required is double; but found is Double" (as the variable "x" is of type Wrapper class Double).

However, it should have thrown an error for not handling the IOException in a try-catch block or the throws statement, which are likely to be thrown by BufferedReader/InputStreamReaders.
[ January 31, 2008: Message edited by: Raghavan Muthu ]
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:
It is wrong! The assignment always happens from Right to Left


That's right, but I never mentioned assignment. I was talking about compiler parsing and because the compiler parsed the left hand side of the statement first and found a Double, it then expected to find a Double on the right hand side. It's the right hand side of the statement that the compiler is complaining about.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andre Brito:
[QB]But when I'm expecting Double, I can get an double, right?
An the inverse is correct: if I have an pimitive variable double, I can get an object Double, without casting.

So, I don't understand why the topic's creator go wrong.



that's because of Autoboxing and AutoUnboxing features introduced from Java SE 5.0

The same would not compile in the prior versions!
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joanne Neal:


That's right, but I never mentioned assignment. I was talking about compiler parsing and because the compiler parsed the left hand side of the statement first and found a Double, it then expected to find a Double on the right hand side. It's the right hand side of the statement that the compiler is complaining about.


Though you have not mentioned, but it is the fact! The compiler and the runtime environment works in the same order (Right-to-Left) for the assignment operator since the expression here deals with one.

And as such, the assignment works in the same way no matter what and since it gives an error because of checking at the RHS first and it finds out that the return value from the Double.parseDouble() method has to be assigned to a primitive double. But since the actual variable "x" available is of type wrapper Double, it complains
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOOOHOOO! Guys slow down a little. I've never had so many relies before. Ok so I understand the difference between a double and a Double but say I just want to input a number with decimal points (gods knows whether that's a double or a Double) using a BufferedReader, how do I do that?

I've tried doing this but I get a data type error:


Could someone just tell me what to write to input "x", it would be very much appreciated.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry, wrong code. Forgot the "throws IOException" thing
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found one issue in your code(may be it is a typo)


[ February 01, 2008: Message edited by: Balasubramanian Chandrasekaran ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Olivier Legat:
...I just want to input a number with decimal points (gods knows whether that's a double or a Double) using a BufferedReader, how do I do that?

I've tried doing this but I get a data type error:


Nothing to get confused here. "double" is a primtive datatype which allows you to deal with floating type numbers with precision. Whereas "Double" is a corresponding wrapper class to deal with the primitive. You may read about Wrapper Classes to get to know about more. Nothing much, the wrapper classes has the same primitive value but with some more functionalities.

The code what you have is perfectly working. What error you get? Ideally you should not be getting any errors!

If you enter an integer value like "2" you will get the output as "2.0" whereas the floating value like "2.3" will be outputted as it is.

Let us know if you need any more information.
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandrasekaran, I tried that, but somehow that only works for very basic programs. Here is an executable piece of my whole code. When I want to you a "try" and "catch" it keeps jumping to the error message.



Do I have to get rid of the "try" and put 'throws IOException' next to the method instead? That's my last opinion but it seems really unprofessional
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olivier,

Why are you getting so much confused? Please read my previous replies. The code which you have originally posted (of course with exception handling and the other mistakes corrected) works pretty fine.

Just try that out.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Olivier Legat:
..When I want to you a "try" and "catch" it keeps jumping to the error message.


Your this code also works perfectly fine in my machine. How are you getting an error? Can you tell the way you execute the program?


Do I have to get rid of the "try" and put 'throws IOException' next to the method instead? That's my last opinion but it seems really unprofessional


No need. Either way should be fine. Try with the code what you have posted first. It should work fine!
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavan Muthu:


No need. Either way should be fine. Try with the code what you have posted first. It should work fine!


Hi Raghavan,

i think oliver should have executed the piece of code which he posted here from some IDE. Because, the same set of code when i executed in the shell i got correct result but when i tried within JCreator LE i got this result



I am still wondering why this is so
 
Olivier Legat
Ranch Hand
Posts: 176
Chrome Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES!!! THERE WE GO! I'm using JCreator LE. When I compile and run it in Command Prompt it works. Thanks Chandrasekaran, that seems to have solved my problem.

Cheers guys
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balasubramanian Chandrasekaran:


.. i think oliver should have executed the piece of code which he posted here from some IDE.


that's where the problem might have come from. All what i did was running the applications through Command line! - the safest.

If at all you are using any IDE, you have to first read the documentation of how the IDE deals with the user inputs (in your case, as you get your input from System.in).
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Olivier Legat:
...(snip)...
Could someone just tell me what to write to input "x", it would be very much appreciated.
...(snip)...





I put several things in here you have not studied yet because you asked if someone could just show you. Well there it is. Ya'll hammer down on this.
[ February 03, 2008: Message edited by: Nicholas Jordan ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, seems like the question was already answered, so I'm not going to read Nick's code too carefully. (Note: please don't anyone learn variable naming from Nick. Use normal conventions.)

Comments on other things:

Aside from having horrible formatting, this has a serious problem: it doesn't tell us what the error is! "***Error in reading command***" tells us someting bad happened, but gives no further clues. It's usually a good idea to include a stack trace whenever you catch an exception:

In many cases calling exit() is a bad idea too. If you want to exit, just don't catch the exception in the first place. Declare your method to throw the exception instead. There's also a relevant discussion about whether it's a good idea to catch Exception at all.

[B][Nick]:
[/B]
Errr, that's a tiny bit better, maybe. Unfortunately it loses the stack trace, which can be very useful. And some exceptions (notably NullPointerException) do not have any message - you'll just print "null" which is very uninformative. Again, the standard thing to do with an exception, the first thing that any beginner should be taught to do, is to print the stack trace:

More advanced users may use a logger instead. But always, always include stack trace info unless you are sure, in advance, that you completely understand why a particular exception might have been thrown. In some cases you might not show the stack trace to a user, but you should certainly log it somewhere.
[ February 03, 2008: Message edited by: Jim Yingst ]
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You posters need to listen up, Jim is on top of this. See if you can implement his fixes to my post. That will get much done for the original poster's needs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic