• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

.get() Method and String formatting

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I'm trying to modify a program per the Lesson requirements and I've determined that I just have to change the operator but I'm trying to gain a deeper understanding.

I've searched and searched the Internet (event tried the Oracle site).  I found some information regarding "%s" but no information that details what %s actually does?

Also, the following line has me in the dark: " newCounter = counter.get() * counter.incrementAndGet();"  How does .get() work; what does it specifically do?




Thanks in advance for any assistance!



 
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this Android? What is 'template'? What type is 'counter'?
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found an example link that shows the %s although I can't tell exactly what it does:

https://compiler.javatpoint.com/opr/test.jsp?filename=FormatExample
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the entire file:




Sorry, no, it isn't Android and I'm using STS...
Staff note (Ron McLeod) :

STS = Spring Tool Suite

 
Carey Brown
Saloon Keeper
Posts: 8234
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html Oracle's javadoc on the syntax of a formatter string.

A formatter String is a String that usually contains one or more substitution macros that begin with '%' and end with a letter. For instance, '%s' would be replaced by a String from the argument list that follows the format String.

The two most common methods that know how to parse and process a format String and the following arguments are:
String.format(...)
System.out.printf(...)

Example:
System.out.printf("Hi there, %s",name);
The '%s' is replaced by the contents of the variable 'name'.

Format macros can have a variety of modifiers between the '%' and the closing letter 's'.
Example:
System.out.printf("Hi there, %-10s (good to see you again)",name);
The '%-10s' is replaced by the contents of the variable 'name'. The '10' tells the formatter to allocate a minimum of 10 characters for the name and if there are less to pad them with spaces. The '-' tells the formatter to left justify the name within the 10 characters.
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carey -

Thank you so much for your post, examples, and the link!
 
Sheriff
Posts: 16277
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Is this Android? What is 'template'? What type is 'counter'?



since newCounter is declared as a long, I would guess counter is an AtomicLong object.

That would also explain the counter.get() and counter.incrementAndGet() method calls.

The incrementAndGet() method is the equivalent of the ++n (pre-increment operation)

The getAndIncrement() method is the equivalent of the n++ (post-increment operation)

Search for examples of AtomicLong.incrementAndGet()
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Junilu -

Thanks for taking the time to respond.

I'm just attempting to understand what .get() does?

I understand what incrementAndGet does but I'm not understanding what 'value' is residing in counter.get()?



Hope this clarifies.
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
counter.get() just returns the current value of counter.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/atomic/AtomicLong.html#get()
 
Royal Summers
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Knute -

Thank you, thank you, thank you!
 
reply
    Bookmark Topic Watch Topic
  • New Topic