My question is related with converting a NUMBER TO STRING..
Say, i have a variable of type Double as shown:
double t=42.5;
now , i make use of a static method "toString" of class Double to convert this number to a string...
String mydouble=Double.toString(t); System.out.println(mydouble); //this statement outputs the value as : 42.5
My question is since the output is also a number, what is the meaning of 'converting a number to string' since i do have a impression of string representation...
Where as...System.out.println(t); this also outputs the same value i.e 42.5.
At the end of the day, my question is when both println statments gives the same value i.e 42.5, what is the actual role played by the method toString() method.
Your number comes out in 1s and 0s which the processor can work out mean 42.5. It is stored in a format which represents a number. There is a method something like "toBinaryString" in the Double class which lets you see the 1s and 0s, but it omits leading 0s; remember a double is 64 bits.
Your Double.toString(d) comes out as a "4" then a "2" then a "." then a "5". It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.
Remember the age-old question: How long is a String?
sameer khazi
Greenhorn
Joined: Sep 05, 2008
Posts: 28
posted
0
Originally posted by Campbell Ritchie: It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.
Remember the age-old question: How long is a String?[/QB]
sameer khazi
Greenhorn
Joined: Sep 05, 2008
Posts: 28
posted
0
Originally posted by sameer khazi: [QB][/QB]
Hi Mr Cambell..
Well you have made the concept clear as how the numbers are actually represented when output.
Can you please elaborate a bit on the following..." represents a writing". This may clear my remaining doubt.
Thank you
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
Anything which can be put in writing can be put into a String. This posting contains a String. You can't change to bold or italics in the middle of a String, but anything you can write on paper can be put into a String.
sameer khazi
Greenhorn
Joined: Sep 05, 2008
Posts: 28
posted
0
Thank you Mr Shekhar and Mr Campbell for your inputs.
When i find no one nearby to clear my doubts i simply log-in to JavaRanch. It seems i have found a good tutor in "it".
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
You're welcome and it is good to see we are doing a good job. Thank you.