GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Double" and "double" - what Watch ""Double" and "double" - what New topic
Author

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

Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176



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


Olly
Reddy Prasad Kurapati Venkata
Greenhorn

Joined: Jan 16, 2008
Posts: 7
Double is a class.
double is a key word used to store integer or floating point number.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Aug 05, 2005
Posts: 3601
    
  15
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.


Joanne
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Dec 13, 2007
Posts: 95

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

Joined: Apr 20, 2006
Posts: 3344

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3601
    
  15
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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Nov 17, 2007
Posts: 176

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

Joined: Nov 17, 2007
Posts: 176

Oh sorry, wrong code. Forgot the "throws IOException" thing
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Nov 17, 2007
Posts: 176

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Nov 28, 2007
Posts: 215

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

Joined: Nov 17, 2007
Posts: 176

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Sep 17, 2006
Posts: 1282
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 ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: "Double" and "double" - what's the difference?