Solved

Java programming declaring and assigning a value to a float variable

Posted on 2014-01-25
1
392 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 27

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

697 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