• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.format()

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How and where is String.format() used?
 
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As stated from class String
156035507990450318.png
[Thumbnail for 156035507990450318.png]
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:As stated from here

Returns a formatted string using the specified format string and arguments.



Thanks for your reply.
What if System.out.print() is used?
Is there any difference between them?
 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:
What if System.out.print() is used?


This question is very basic. You can easily get the answer/tutorial from google.
 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any difference between them?


Yes, they are totally different.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:

Is there any difference between them?


Yes, they are totally different.



Excuse me I have just started learning programming a month ago.
I know string datatype is an array of characters.
But i am no sure about methods of string and their usage,Can you please refer me to a link where I can learn about methods of string and their usage?
 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link is for String.format. You can have a look.
https://www.javatpoint.com/java-string-format

For System.out.print(), it is used to print message to console or file.

Example
You will see the message "Hello World" in console as the output.
 
Greenhorn
Posts: 22
1
Netbeans IDE Oracle Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:

Randy Tong wrote:

Is there any difference between them?


Yes, they are totally different.



Excuse me I have just started learning programming a month ago.
I know string datatype is an array of characters.
But i am no sure about methods of string and their usage,Can you please refer me to a link where I can learn about methods of string and their usage?


I'm still new to programming and I've been helped by Sir Randy Tong each time I encounter problems. I look forward in learning with you in this ranch!
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jasper Abapo wrote:

Hari Nagarjuna wrote:

Randy Tong wrote:

Is there any difference between them?


Yes, they are totally different.



Excuse me I have just started learning programming a month ago.
I know string datatype is an array of characters.
But i am no sure about methods of string and their usage,Can you please refer me to a link where I can learn about methods of string and their usage?


I'm still new to programming and I've been helped by Sir Randy Tong each time I encounter problems. I look forward in learning with you in this ranch!




This is my first Java community and I didn't think I would get answer/help this fast.
We have Sir Randy Tong to thank for that.
Looking forward to learn with you Jasper.
 
Randy Tong
Rancher
Posts: 662
10
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope my post helped
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wondering if you meant
whereas print() does not.

The difference between printf() and String.format() is where the results of the formatting go. With printf() the results go to the output stream you specify, and with format() the result is a String which can be used any way you want.

A place where you might want to use format() is when you override the toString() method in one of your classes. Example:
 
Marshal
Posts: 79153
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . . I know string datatype is an array of characters. . . .

Afraid that is incorrect; it even says so in the Java® Language Specification (=JLS). I hope nobody has told you the two are the same.

. . . and welcome to the Ranch
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Hari Nagarjuna wrote:. . . I know string datatype is an array of characters. . . .

Afraid that is incorrect; it even says so in the Java® Language Specification (=JLS). I hope nobody has told you the two are the same.

. . . and welcome to the Ranch



ok thanks.
but I have seen that string is basically an object that represents sequence of char values in
https://www.javatpoint.com/java-string

Is that wrong?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:but I have seen that string is basically an object that represents sequence of char values...



Sure, that's (almost*) true. But that's a long way from actually being an array of characters.

* I say "almost" true because a String actually represents a sequence of Unicode characters. Java was changed in (I believe) version 1.5, which was released in 2004, so that String objects could represent Unicode characters which aren't in the Unicode BMP. Previously they could only represent Unicode characters in the range up to U+FFFF, which correspond to Java chars.
 
Hari Nagarjuna
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Hari Nagarjuna wrote:but I have seen that string is basically an object that represents sequence of char values...



Sure, that's (almost*) true. But that's a long way from actually being an array of characters.

* I say "almost" true because a String actually represents a sequence of Unicode characters. Java was changed in (I believe) version 1.5, which was released in 2004, so that String objects could represent Unicode characters which aren't in the Unicode BMP. Previously they could only represent Unicode characters in the range up to U+FFFF, which correspond to Java chars.




This is what I understood about difference between strings and array of characters so far and unicode(U+0000 to U+FFFF).
1.Unicode is combination of 0's and 1's and gives 65,536(2^16 or 16^4) characters.
2.character array can only contain this 65,536 unicode characters.
3.String can have things other than unicode characters in BMP(65,536 characters).
please correct me if I am wrong.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari Nagarjuna wrote:. . . https://www.javatpoint.com/java-string . . .

Those of us who have been in this trade a long time know there are lots of bad tutorials. That tutorial gets off to a good start saying,

In Java, string is basically an object that represents sequence of char values.

which you have quoted and, except for writing string, that is “basically” correct (so you were correct about the same point ). It even says that String implements CharSequence, which I recommend you read about if you have difficulty getting to sleep

The CharSequence documentation wrote:A CharSequence is a readable sequence of char values.

Unfortunately, that tutorial then goes completely wrong in its second sentence. The following is totally wrong:-

That tutorial wrote:An array of characters works same as Java string. For example:

   char[] ch={'j','a','v','a','t','p','o','i','n','t'};  
   String s=new String(ch);  

is same as:

   String s="javatpoint";

Your information about Unicode is, I am afraid, very much out of date.

I am afraid the javatpoint link about String#format() doesn't help you much. I can see a few problems with it:-
  • 1: It talks about locales, but doesn't explain anything about what they mean.
  • 2: Its description of %b is incomplete and misleading.
  • The link only shows you a few options for String#format. If you go through the documentation for Formatter, you will find them all. There are so many combinations that nobody remembers them all.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic