Jump to content
  • Advertisement
Sign in to follow this  
TheBluMage

[java] I never knew this would happen

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Was just writing a simple little quadratic equasion solver for my friend and stumbled onto something I was curious about. I returned the results from my solver method in a double[] containing 2 elements (one for each possible value of x). I wanted to test the method before writing up the GUI just to make sure I hadn't made any dumb mistakes and just out of curiosity and laziness, I tested the method with this code:
public static void main(String[] args) {

	double[] answers = solveEquasion(1, 2, 1); //a, b, c

	//Just wanted to see if I could print the array contents without a loop
	System.out.println(answers);

}

Having done most of my programming in .NET I half expected to just see a print out stating the type of the object being printed, but I was surprised to see this instead: [D@10b62c9 Since I never had really experimented with the console this way I decided to print a line using a new Object() instead and got this result: java.lang.Object@10b62c9 After looking at the first number for a second I made the connection that the [D must represent an array of doubles. Now, to the question. What does the number after the '@' signify? (My guess was that it's the object's location in memory, but I could be wrong.) I've been programming for years, but this just never came up. Guess you learn something new every day.[wink]

Share this post


Link to post
Share on other sites
Advertisement
actually it's a virtual address that the pointer is pointing to :p(when it's null you get a null pointer exception).

Share this post


Link to post
Share on other sites
I believe that's the hash key used to identify it internally in the VM. By the way, it's "equation".

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
By the way, it's "equation".
Blast this silly keyboard for misspelling that word![lol] Thanks for the info!

Share this post


Link to post
Share on other sites
"[D" is the Java internal type code for "an array of doubles"

You can't print out structured objects just by supplying them as parameters to system.out.println().

What happens if the answer is non-real?

In which case it should be a Complex [] that you're returning from your quadratic solver.

Or do you only care about real solutions? In which case there won't be any.

Mark

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
-1 is the answer to his example, do you mean when there are complex answers?

Share this post


Link to post
Share on other sites
Some values of a,b and c result in no real solutions to a quadratic equation, any young maths student will tell you this.

There is an answer however, these solutions lie on the complex plane, that's to say, they consist of an imaginary and real part.

I believe Java has a class "Complex" which represents these complex numbers.

In this case however, there will be real roots.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!