FAQ: Learn Java: Variables - doubles

This community-built FAQ covers the “doubles” exercise from the lesson “Learn Java: Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise doubles

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Is the ‘double’ datatype in java similar to the ‘float’ datatype in python?

Why do I need int? Can I just store everything in double?

2 Likes

I’m not an expert in Java but I’m learning. However, I think as with most things in Java, there are multiple ways to get the same answer, it just seems cleaner to write int a = 1; rather double a = 1;

However, I’m sure there is a specific reason why you would have to use an int over a double at some point. I believe double cannot be negative either.

Hope that helps.

2 Likes

I’ve just read that using “double” takes up more memory than using “int”. Memory isn’t important me at this early stage but maybe later on it becomes relevant.

2 Likes

I’m totally lost. What do the instructions asking me to do? this is what I wrote but I know it’s wrong. Please help this is so frustrating to not be able to learn such a simple language that everyone else knows

public class MarketShare {
public static void main(String args) {
// doubles can have decimal places:
androidShare = 81.7;
//doubles can have values bigger than what an int could hold:

double gdp = 1237700000;

System.out.println("androidShare");
}

}

I don’t know why all that looks weird above. I just looked a the solution and it doesn’t even make sense. what are the directions even asking me to do?

public class MarketShare {
public static void main(String args) {
// doubles can have decimal places:
androidShare = 81.7;
//doubles can have values bigger than what an int could hold:

double gdp = 1237700000;

System.out.println("androidShare");
}

}

Greetings! Take the quotations from around ‘androidShare’ in the println and see if that works :slight_smile:

1 Like

It’s because you did not add double to "androidShare = 81.7;
I hope this helps others.

In addition to not declaring the variable type, note that @byte5723385929 is printing out "androidShare". Because androidShare is surrounded by double quotation marks, Java interprets it as a string and will print the string literal androidShare. If you remove the quotation marks, Java will interpret that as a variable and will print out the corresponding value of that variable, in this case 81.7.

1 Like

Yes that is true. Thanks, I didn’t notice that

1 Like

Idk what I am doing Wrong

1 Like

I got it and i made Stupid mistake

You may want to specify what type of print command to use. I was trying to just use print and it didn’t work until I used println();

Or is there some reason I should know to use println instead?


What did I do wrong?

Welcome to the forums!

Each might be useful depending on what you are trying to implement. print() will print something out, and the cursor will remain at the end of what you’ve printed. println() will print something out, and the cursor will move to a new line.

Example

System.out.print("Hello");
System.out.println("World");

System.out.println("Hello");
System.out.println("World");

/* The above prints the following:
HelloWorld
Hello
World

*/

Welcome back to the forums!

I believe that the exercise is looking for you to use println() rather than print(). You can see my above reply for more info on the difference between them.

Maybe it’s the space between println and the parenthesis. I saw something like that in another exercise.

Welcome to the forums!

While the space between the method name and opening bracket is not the convention, it doesn’t actually cause the error. Rather, the error comes from system not being capitalized since it’s unable to recognize system as a valid package. Remember that Java is case-sensitive and therefore we need to use System.

1 Like

Hello, can you please let me know what I need to fix for this to work? I cannot understand what I did wrong.

Bro you should capitalize first letter of System.out.println not system