Solved

Java programming declaring and assigning a value to a float variable

Posted on 2014-01-25
1
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 28

Accepted Solution

by:
dpearson earned 500 total points
ID: 39809570
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

690 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