This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

.get() Method and String formatting

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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!



 
Royal Summers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the entire file:




Sorry, no, it isn't Android and I'm using STS...
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey -

Thank you so much for your post, examples, and the link!
 
Marshal
Posts: 14388
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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: 18
  • Mark post as helpful
  • send pies
  • 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: 6378
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute -

Thank you, thank you, thank you!
 
This looks like a job for .... legal tender! It says so right in this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!