• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String class

 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



i understood that we're passing an array object to one of the string constructor but i wonder how the appropriate character in that array returns to println method without calling a method in string class that returns the character.
explain what is happening.?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand it better if it were written like this way instead?
 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks for your response.
now i can make you understand my question clearly
we're just instantiating the string object through its constructor
how do we get the value,because constructor does not return any values?
explain how the control flows internally.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know the toString()-method?

System.out.println(string); is equivalent to System.out.println(string.toString());
 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Do you know the toString()-method?

yes.

you're right toString() is a method in String class.
shouldn't it be explicitly called?
how the compiler will understand that this particular method is called?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ssiva raman wrote:shouldn't it be explicitly called?

No (as shown in my previous example)

ssiva raman wrote:how the compiler will understand that this particular method is called?

The compiler doesn't need to know that! The compiler just makes sure that you don't violate any of the java language specifications (e.g. assigning a String to an int). And because the println()-method of System.out can be used with a lot of different parameters (e.g. boolean, double, int, Object,...), the compiler will compile your program without any problem. What actually happens, depends on the implementation of the println()-method.
 
ssiva raman
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont get it clealy,
if you dont bother can explain it quite briefly.

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

According to the javadoc of the String class, this line Allocates a new String so that it represents the sequence of characters currently contained in the character array argument.


This line just prints the sequence of characters to the standard output. No need to invoke toString() on the string variable (because that returns just another String object).
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
This line just prints the sequence of characters to the standard output. No need to invoke toString() on the string variable (because that returns just another String object).

Or to be more precise
If the argument is null then the string "null" is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

Quote is from the Javadoc for the PrintStream class
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic