What happens when you assigned a 'double' value to
a 'String'?

Answers were Sorted based on User's Feedback



What happens when you assigned a 'double' value to a 'String'?..

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

What happens when you assigned a 'double' value to a 'String'?..

Answer / pankaj

use toSting() method for printing double value as String or
assigning to string variable

Is This Answer Correct ?    1 Yes 0 No

What happens when you assigned a 'double' value to a 'String'?..

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

What happens when you assigned a 'double' value to a 'String'?..

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

What happens when you assigned a 'double' value to a 'String'?..

Answer / sriragv

results string i hope

Is This Answer Correct ?    1 Yes 2 No

What happens when you assigned a 'double' value to a 'String'?..

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

What happens when you assigned a 'double' value to a 'String'?..

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 happens when you assigned a 'double' value to a 'String'?..

Answer / niranjanravi

result is a string

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More Core Java Interview Questions

What do you mean by buffering?

0 Answers  


how we can make a read-only class in java?

0 Answers  


hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea

0 Answers  


Hi Friends, I am new to java. Can u explain about thread concept.How i know one thread is locked, and how can i force or acquire another thread for lock . Finally how to know thread is released from lock. Explain types of lock(like method level and block level) in thread.

1 Answers  


Can classes declared using the abstract keyword cab be instantiated?

0 Answers   MCN Solutions,






What is the difference between abstraction and encapsulation?

0 Answers  


What is equals method in java?

0 Answers  


7) Suppose there is Student class like class student { int age; string name; } We want to store these objects in a HashMap. Do we need to override any methods in Student class? If any which ones and why? what if i just override equals or just hashcode? what will be the results in both the cases?

4 Answers   CSC, Nagarro, RBS,


Can you explain the difference b/n abtract and interface with a good example,?In what cases we have use abtract and what case interface?

4 Answers   Satyam,


Write a program to create a binary Tree ?

2 Answers  


What is return null in java?

0 Answers  


Why string is not a wrapper class?

0 Answers  


Categories