• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number to String ?

 
sameer khazi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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.

Thank You





:roll: :roll:
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class implements toString() because it is defined by Object.

toString() allows you to get something, meaningful representation of a given object.

All of the wrapper classes provide an overloaded toString() (static) method that takes a primitive value of the correct type and returns a String.

 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome and it is good to see we are doing a good job. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic