• Register
Welcome to Kodlogs, programming questions and answer website.
+2 votes


What is  java.lang.Math in  java.lang.Object?

by (1.6k points)  
edited by

10 Answers

0 votes

Java Math in java.lang.Math

To perform mathematics calculations on numbers java math provides many methods as following:

abs(double a)

abs(float a)

abs(int a)

abs(long a)

acos(double a)

acos(double a)

asin(double a)

atan(double a)

atan2(double y, double x)

ceil(double a)

copySign(double magnitude, double sign)

copySign(float magnitude, float sign)

cos(double a)

by (9.8k points)  
0 votes

Methods which have inheritance from class java.lang.Object


by (9.8k points)  
0 votes

Some Java Math Methods:






by (9.8k points)  
0 votes

Logarithmic Math Methods

Math.log() It restores the normal logarithm of a twofold worth. 

Math.log10() It is utilized to restore the base 10 logarithm of a twofold worth. 

Math.log1p() It restores the regular logarithm of the total of the contention and 1. 

Math.exp() It returns E raised to the intensity of a twofold worth, where E is Euler's number and it is around equivalent to 2.71828. 

Math.expm1() It is utilized to compute the intensity of E and subtract one from it.

by (9.8k points)  
0 votes
At times essential number juggling isn't sufficient to take care of an issue and you have to utilize

one of the numerous math capacities that Java brings to the table. Indeed, there's such huge numbers of

that you couldn't in any way, shape or form recollect them all, and we arranged a reality sheet for

you. Let me simply go over the most significant one. Suppose you have to register

a power, for example, 2 to the tenth. You can't compose that type conveniently in Java,

rather you utilize a capacity and the language structure resembles this. The capacity name is

pow, it's in a class called Math. You need to reference it as Math.pow. At that point,

you offer it to the two operands, the 2 goes here, and the 10 turns into the second

one. On the off chance that you have to process the square root, you utilize the square root work.

Once more, it's in the Math class, Math.sqrt, and whatever it goes inside the root

image in Math documentation goes inside the enclosures here. For total qualities,

there is another capacity. It's called abs and whatever you put between the

vertical bars in Math documentation goes on inside here. Presently, these sorts of

capacities come up regularly enough that I need you to rehearse this a smidgen.

Here, I have a lot of capacity calls, and I'd quite recently like you to let me know for

every one of them, what is the outcome? In the event that you like, you can register it in BlueJ, or

you can take care of business in your mind.
by (9.8k points)  
0 votes
Now let's see here, 10 raised to the 3rd power, that's 10x10x10. That's 1,000.

The square root of 4, well 2 times 2 is 4, so that's 2. The absolute value of 3

minus 5, that's the absolute value of minus 2. Absolute value takes away the

minus sign, and that's 2. Intuitively, what it means it's the magnitude of the

distance to go from 3 to 5. So you have to go two steps, you use the absolute

value, so that the direction doesn't matter. The absolute value of 5 minus 3 is

the same as the absolute value of 3 minus 5. It's just the distance between

these two values. Now, I've tossed in a couple of others just to see if you can

guess these. Min and max are the smaller and the larger of these arguments

here. So the miniumn of 300 and 255 is the smaller one, 255. The larger one of

zero and minus 1. Well, zero is pretty small, but it is larger than minus 2 so

that's zero. And our next image processing exercise will put min and max to

good use.
by (9.8k points)  
0 votes

We are putting the math back in Java. Let's say you want to show some

tree structures, you know: these nice elements with little handles to open

children. this is what I mean. root node, child 1, child 2, and

these also have children. and this is generated on demand as far as you want

it. Is that easy to make in Java? No. It's not. Is it easy in JavaX? Yes, it is! [Laughter] Make

the fonts a little bigger, declare the root node, and make one single function

that converts a node into its children. note that all the Java classes like

DefaultMutableTreeNode are completely hidden. You just don't need to see them

even. and you can use any object type that has a toString method. this is the

machinery behind it. it's not very simple. but I built this once and for all so you

can use this here because that's the mathematical way. this is really just

defined by one function and one root element. so that's what I'm spending my

Sundays for: making the best Java ever. and the next version of the operating

system is coming out!

by (9.8k points)  
0 votes
Now let's talk for a minute about wrapper classes. And when I'm talking about wrapper

classes, I'm not talking about

[Professor beatboxing and rapping]

[?Boy, you should have known by now, Spencer does it!]

That's not what we're talking about. We're talking about wrapper classes, w r a p p e

r classes, wrapper classes. And these are useful when we have information of one type

that we want to convert to another type. And so let me go ahead and create a class here.



Okay. So if we have information that I will go ahead and import the Java util class, util

scanner class. And then set up an instance of a scanner.


And then if I read in some data, so I'm going to get information in as a string. I'll just

call in input = "" to begin with. So I say


And I say something like, "Please enter the year you were born." Okay. And I read that

into the input. So input = in.next and because it is a string, it is going to be a next line,

right? Or a next I guess we could do. So now we have stored that information. Okay. So

if I print this out


Let's just print out what we read in, make sure everything came in okay. So I'll save

this. Okay. That's the information we get out is a string. What then, if I wanted to

calculate at what age, if I enter another year, so let's go with something easier, 2000.

And if I want to calculate the age that I become an adult, so 18 years after I am born.

The problem is, if I calculate that age, so let's say I create an int called adultAge

and set that equal to zero to begin with. If after I have read in this information,

I want to do a calculation. So I want to take that input, which was way too generic of a

name, I should have given it a better name, or sorry, adultAge and set it equal to whatever

the input was plus 18. It is going to tell me, incompatible types. This is a string or

words or characters. Even if I entered a number like 2000 into this input, it is still considered

letters because it is in a string, it is characters I guess I should say. It is not a number,

it is ASCII characters. And so I can't add 18 onto it, that doesn't make any sense. What

does a + f equal? It doesn't make sense, right? So in order to make this into a number, so

that I can do something with it, first of all, I can just declare it as a number and

then read it in as a next int. But we also have available to us these wrapper classes.

And the wrapper classes work like this: If I want to turn this input into an integer,

then there's an integer class that has a parseInt method in it that I pass a string. So it is

Integer.parseInt and then I pass it a string. And that will going into the string and try

and create a number out of it. Okay, so if I do this now, and I enter the year 2000,

well the problem is I am printing out input still. So let me print out instead, adultAge,

let's try that. Okay, and now I say 2000. Then now it will print out 2018 because it

parsed an integer out of the input and then it added 18 onto it and that's what we get

for the age. So adultAge is 2018. Wouldn't it have been convenient to have been born

in the year 2000? It would be so easy to calculate everything.

What if I enter something like hi? Well, I get an error because hi is not something that

I can parse an integer out of. And we'll talk about catching these errors later on so that

it doesn't just crash the program like it just did. What if I say, hi2000? Well, same

problem. What if I say 2000hi? Same problem. But if I enter something that is potentially

an integer, so, if I had been born in the year 1950, then it tells me that I would be

an adult in 1968 and we use that by again using these wrapper classes, integer.parseInt.

There's a long.parse, guess it, long, yeah. Integer.parseLong. There's a Float.parseFloat.

There's a Double.parseDouble. If I wanted to take this and make it a double, well, I'll

get an error but just to show you. Double.parse, and instead of parseInt, that wouldn't make

any sense, it is going to be a parseDouble. So I Double.parseDouble, again Double being

the class, parseDouble being the method. It is going to tell me possible loss of precision

because now it is adding a double onto an int and then trying to store it into an int.

So I could make this a double, or I could cast this double as an int, right? Remember

how to do that? In parenthesis, we put that keyword, int, and that says make whatever

comes next an integer. And now it will work just fine. And now we get a double out of

there. Now, why I did that, just to show you that's doesn't make any sense to do a double

and then just cast it back to an int. Why wouldn't I have just done an int in the first


But these wrapper classes are very useful. In fact, I'm aware of individuals, and this

is a fairly common thing, that will read everything in as a string and then just parse out what

they need to, rather than reading it in as an integer. And really, I guess it just depends

on the situation, but that is a possibility. On the other hand, we can go the other way.

We can take an integer, adultAge and make that a string by saying, let's take that adultAge

now and, so I'm going to set, I better create one more variable


And I can say, output is going to be equal to Integer.toString and then pass it the adultAge,

which is an integer that I have just calculated. So remember, adultAge is an integer, I'm going

to say, in that integer class, there's a toString class that will take whatever is in the integer

and make it into a string and that's what I'm going to set my output to and then I can

print out the output. Now, this isn't going to look any different, so this is kind of

a stupid example. And I know what you're saying, most of these examples are stupid. That's

true. There we get our 2018, but instead of being a number, it is actually a string. If

I try and say output plus 5, it is going to yell at me and say, okay, it is not going

to yell at me, but notice it didn't do the math. It just tacked the 5 onto the end because

now it is all treated as text. Okay? Does that make sense?

All right. Very good. So these are what we refer to as wrapper classes and they are very

useful to see, and these are just some of them, right? If we go back to our window,

if we go back to our window and say Java Integer Class, then here's going to be the information

about the integer class. So here's the integer class, there are some fields. And then here's

all the methods that we can do. We can do a bitCount, byteValue, compareTo, decode,

and for any of these, we can click on them and go look at the details. So the parseInt

method takes a string and parses the string argument as a signed decimal integer. The

characters in the string must be all decimal digits, except the first character may be

an ASCII minus. So you can have a minus in there if you are doing negative numbers. So

it gives you a little bit of information. Again, this is like the instruction manual,

this API is the instruction manual for how to use these methods and there are just a

ton of them out there.

So in other words, I guess what I'm getting at is I didn't go through like a comprehensive

view of all of the methods that are part of the integer class or any of the wrapper classes,

but rather, giving you an example of how they might work. And everything we're doing here

is like adding a tool to our tool belt. So at some future point, when we've got a string

and we're trying to turn it into an integer, we say, that's right, there was a thing to

do that. What was it? And we remember what we were supposed to do and then go and get

that tool out of our tool belt and implement it. So I'm just kind of feeding you the tools

here with what we're doing.
by (9.8k points)  
0 votes
we want to introduce the Math class, which is a Java-provided class

with standalone capabilities for all the standard math operations.

Okay, let's get started.

Whenever we have a need in our programs to do any standard mathematical calculations,

we can draw upon the Java-provided Math class to do this.

The Math class is part of the java.lang package, which means that it is always available to

us and does not need to be explicitly imported.

So in that regard, it's comparable to using the String class in our programs.

The Math class provides two standard mathematical constants for the natural log E and for PI.

It also provides a number of methods to perform many common math calculations.

These lecture notes and the textbook will give only an abridged version of what's in

the Math class.

For a full list of its capabilities you'll need to explore its Java API, which we'll

do soon in a companion lecture.

One important thing to point out about the Math class is that all of its methods and

constants are static.

Remember that this means that such methods are called in the absence of any created objects,

they are called using the name of the class instead, as we see here.

And there's a good reason for this.

When we're performing math operations, we don't necessarily want to have to create individual

math objects to perform routine calculations, we want to simply compute a result and move

by (9.8k points)  
0 votes
So the static methods of the math class give us this kind of standalone, object-independent


Also, many of the Math methods are overloaded methods, which simply means there may be alternate

forms accepting different input datatypes.

So what this usually means for us is that these methods will accept ANY type of numerical

input, up to and including doubles, because all numerical input types can be implicitly

promoted up to doubles.

We'll see this a bit later when we take a look at the Math API.

For starters, we said that the Math class provides two static constants for PI and the

natural log E. To access these constants, we use them statically by preceding them with

the name of their containing class, as shown here.

Here, Math is our static class, and we know these are constants because they observe the

usual all-caps naming convention.

These two are nothing but freely available numerical constants.

So if we were to print these constants out, these are the values which would be displayed.

At this point, I'd like to refer you to the short code walk-thru video for MathExamples.java.

This example file will contain useful examples of all the Math class constants and methods

we'll be covering in this lecture video.

I'll be referring to this example file frequently, so I would suggest that you download this

file from the usual place, and open it up in jGRASP so that you can follow along.

You can also go back and forth to the accompanying code walk-thru video for more explanations.

But do make sure to examine that source code file, because there are many useful examples

in there.

Here are a handful of the most commonly used methods of the Math class.

We'll be using these in a current homework program assignment, so you'll want to know

these ones.

These are nothing but standard, workhorse mathematical operations: absolute value, natural

logarithm, base-10 logarithm, square root, and exponentiation.

For the absolute value method abs(), note that it will have several overloaded forms,

which can accept multiple datatype inputs: double, float, int, or long.

Then, the datatype of the result will be of the same type as the input.

For these other three methods, note that they accept only double datatype inputs, and return

a double datatype result.

This is okay though, because recall that double is the largest datatype, and any numerical

value or datatype can be implicitly or explicitly cast up to a double, with no loss of numerical


Besides, it really doesn't make sense to compute these in anything less than decimal precision


Also, note that Java provides the pow() method for raising a number to some power, because

it doesn't provide any exponentiation operator like these, as do some other programming languages.

Again, I will refer you to the MathExamples.java source code file or its accompanying code

walk-thru video, for good examples of using these methods.

Next, here's the round() method, used to round some decimal point number to its nearest whole

number equivalent, subject to these usual mathematical conventions.

Notice that here there are TWO forms of the method interface.

If the input datatype is a double, which will accommodate any floating-point number, the

resulting output datatype is a long, since that will accommodate the largest possible

integer values.

But there is also an alternate form as noted.

If the input datatype is a float instead, then the resulting output datatype is an int


Once again, I'll refer you to the MathExamples.java source code file or its accompanying code

walk-thru video, for examples of using this method.

Finally, the Math class provides min() and max() methods, for determining the smaller

or larger value of some input pair of numbers.

Later on, of course we could implement this for ourselves using some simple logic, but

Java provides a simple method to do this.

Again, notice that there are several overloaded versions of these methods, such that the datatype

of the result will be of the same datatype as the inputs.

So these methods can handle doubles, floats, ints, and longs.

While it's very straightforward to use these methods to find the min or max of any two

given numbers, more often than not we need to find the min or max of some series of numbers.

To do that using these min() or max() methods, we can proceed in two ways.

The first way would be to proceed in stages.

So let's say we find the smaller of the first two numbers, then we use that intermediate

result to compare against the next number, as shown in this first example.

Or, we could do this in one operation using nesting, as shown in the second example.

Here, this entire expression in blue is the same as this first step above, and it evaluates

to one single value, which is then compared to the third value for a final result.

This second approach can similarly be extended to 4 or more inputs, but practically speaking

it will quickly become needlessly complicated.

More realistically, this first approach better lends itself to comparing to additional values.

Or better yet, we could wrap some sort of loop around this and extend it to any arbitrary

number of values, but more on that in a future lecture.

Once again, I'll refer you to the MathExamples.java source code file or its accompanying code

walk-thru video for code examples of both these approaches.

Okay, that's it for now on the Math class.

Between the methods we've presented here and the examples in the source code file, you

should have what you need to complete this week's assignment involving the Math class.
by (9.8k points)