File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Problem understanding API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem understanding API " Watch "Problem understanding API " New topic

Problem understanding API

Rob Freiberger

Joined: Mar 13, 2005
Posts: 8

I'm really new to Java, please excuse my basic question.

In my class we briefly discussed the API and I have downloaded it to my computer but am having a very hard time understanding how to use it. Say for example I want to find how to print an interger as a rounded number where would that be located in the API?

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
On the top of the API page, click Index then click R and scroll down to "round".

BTW, how can an integer not be a rounded number?

Mike Gershman
SCJP 1.4, SCWCD in process
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Don't panic - part of the process of learning any language is discovering where in the language's API a particular piece of functionality might be. If we take your example: you wouldn't ever need to round an integer, since integral values are always whole numbers. But for general mathematic manipulation you have the java.lang.Math class.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Hi Rob, and welcome to the JavaRanch.

The Java API tells you what classes you have at your disposal, and services
the classes offer.

I want to find how to print an interger as a rounded number where would that be located in the API?

First you locate the class, that might contain the method that we are looking for:

First try: We will use the Integer class*.

What do you see?
- There is a description of the class. We learn, that an object of type
Integer contains a single field whose type is int

- Then there's the field summary, which tells you which public variables you
can use. Here we see, that we can get the maximum(minimum) integer value
by using Integer.MAX_VALUE (or Integer.MIN_VALUE).

- On to the methods (and your question): What we do is we take a look at
the methods, and the description. We (sadly) find, that the interger class
doesn't have a "rounding" method!

Suddenly you remember, that there's a class called Math! let's take a look
at it:

We find, that the class Math indeed have two methods called round. Sadly
the first take a double as an argument, and the other takes a float.

A final question: Are you sure, your looking for a method that can round
and integer? if you wish to get such a method, you have to make your own
as (to my knowleadge) there arn't any such methods in the J2SE API.

Had your question been: How do I round a double or float and print the value,
my answer would have been:

Hope it helped - else ask again

/Svend Rost

*If you have a more "exotic" class, that is in a package you can search
for it by presseing the "all classes" link in the upper left corner, and
then do a "find and search" in the left frame containing the classes.
Rob Freiberger

Joined: Mar 13, 2005
Posts: 8
Thanks for much for the clear example. I'm sorry I actually used the wrong wording and ment to type "double" instead of "interger". Bascially it's for a application that calculates sales tax and the problem I had was computing sales tax. The answer kept showing too many numbers bwyond the decimal point.

Thanks again,
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can also look at NumberFormat in the API.

And be aware that mixing money with floating point can lead to very confusing bugs. I think most people wind up using BigDecimal or long in pennies rather than dollars, eg $1.00 is stored as 100.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I started learning Java during a college course. Our textbook was Core Java(TM) 2, Volume I--Fundamentals which is in its 7th edition now. In each chapter it has good discussion of the related classes from the API. I usually find tutorials such as these a good place to start. Later when I need to perform a specific task, I at least have a vague idea where to look to figure out how to accomplish it.

Since you are in a college course, I assume you have a textbook of some kind. Hopefully it provides a decent description of the common classes from the API. If you are interested in learning more on your own, I suggest that you use Sun's Java Tutorial. It has several "trails" about specific parts of the API.
There are also other online tutorials that can give you an introduction to groups of classes from the API. Unfortunately, I don't have any bookmarked at the moment, but you can probably find some suggestions that have been given here previously by using the Big Moose Saloon search tool. Finally, you can come here to ask questions. If you need help solving a particular problem, some of us may know of relevant classes in the API, so if we mention the name, you can at least look up the methods the classes provide.

Keep Coding!


p.s. I'm glad to see that someone knows about the API docs. We were having a discussion in another thread about the apparent lack of knowledge by some posters here.
[ March 14, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Google is also great for searching the API. If you want quickly want the Integer API, just type "Integer java API". If you want to see some examples of the API in action, use
I agree. Here's the link:
subject: Problem understanding API
It's not a secret anymore!