Can we add flaot value to integer and store result in float, if yes,please explain by taking one code sample..?

Questions by shidramappa

Showing Answers 1 - 1 of 1 Answers

anup_java

  • Dec 8th, 2006
 

You dont need to write any extra code to add a float value to an integer value and store the result in float. Consider the following sample code :

CODE :

public class Test
{
 public static void main(String[] args)
 {
  int x = 10;
  float y = 10.0f;
  float z = x + y;
  System.out.println(z);
 }
}

OUTPUT:

20.0

EXPLANATION:

The integer x is automatically promoted to float when the following statement is executed.

float z = x + y;
This is known as automatic conversion .

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions