?
Solved

Java programming declaring and assigning a value to a float variable

Posted on 2014-01-25
1
Medium Priority
?
399 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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