Solved

Java programming declaring and assigning a value to a float variable

Posted on 2014-01-25
1
386 Views
Last Modified: 2014-01-25
I'm using Eclipse and Java.

I am trying to declare and assign a value to a float variable.
Below are the 2 examples I have. On both examples its not liking how I'm assigning the value to the variable

How do I fix both of these examples?


Example 1:

public class TestClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		float x;
							
		x = 2.14239;

		
		System.out.println("The value is: " + x );


	}

}

Open in new window


Example 2:

public class TestClass {

      public static void main(String[] args) {
            // TODO Auto-generated method stub
            
            float y;
                                          
            y = 6.02E23;

            
            System.out.println("The value is: " + y );


      }

}
0
Comment
Question by:maqskywalker
1 Comment
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
Comment Utility
There are 2 floating point types you can use in Java: "float" and "double".

99.99999% of the time you should double (it's more accurate - float is just a historical type that has little use today).  Also by default a constant like "1.23" is type "double" instead of type "float" - which is the error you're getting here.

So if you switch to:

double x ;
x = 2.14239;

and

double y ;
y = 6.02E23;

All should be well.

Doug
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now