What happens when you assigned a 'double' value to
a 'String'?
Answers were Sorted based on User's Feedback
Answer / paras bist
compilation fails ,
even for
double d=10.5;
String f=(String)(10.5);
"Cannot cast from double to String"
| Is This Answer Correct ? | 6 Yes | 0 No |
Answer / pankaj
use toSting() method for printing double value as String or
assigning to string variable
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / cleon
@Pankaj:
What are you trying to do?, trying to call a function from
primitive type/reference, that that will never work. You can
call function on Object types.
Double d = 5.5D; //D is optional as by default it is treated
to be double value
String s = d.toString();
If one tries to do something like
String s = 5.5;
A type mismatch errors will be shown during compilation itself!!
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / pankaj
hi,
toString() method will also not work here.
double d=10.5;
String str= d.toString();
E:\Programs>javac conversionTest.java
conversionTest.java:6: double cannot be dereferenced
String str= d.toString();
^
1 error
The only way I found is as below and is working fine.
double d=10.5;
String str= ""+d+"";
System.out.println(str);
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / vinay kumar
Actually java doesn't have string type
there is only 'char' type.
Here String is a class (in java) ,
but i am saying one thing,
these comes 1 & if not 2 & if not 3
1) if in a line have float value that total conversion value
comes a float value.
other wise
2) if in a line have double value that total conversion
value comes a double value.
otherwise
3) if in a line have long value that total conversion value
comes a long value.
| Is This Answer Correct ? | 0 Yes | 1 No |
Answer / ravikiran(aptech mumbai)
compilation fails
double d=10.5;
String f=(String)(10.5);
will work
| Is This Answer Correct ? | 1 Yes | 4 No |
What is the difference between throw and throws in java?
Write a program to print fibonacci series up to count 10.
Suppose there is an array list [10,5,20,19,15,24].Print them in ascending & descending order in collection framework concept ???
Why do we need singleton class?
why to use transient variables when static variables can be used for Serialization
What is the USE of Null interfaces ??...if thers nothing inside these interfaces how are they used and WHy are they used ???? No 1 has given a proper description yet
What is one third plus one third as a fraction?
How would overload a function based on return type?
What are internal variables?
What is pre increment and post increment in java?
Differentiate between a constructor and a method? Can we mark constructors final?
What is module in oop?