Java Tutorial – 15 – Powers and Square Roots (Math Functions)

square in java
This is a topic that many people are looking for. is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, would like to introduce to you Java Tutorial – 15 – Powers and Square Roots (Math Functions). Following along are instructions in the video below:

hello welcome to this lesson of mastering Java here were going to learn how to continue using the math functions the built-in math functions and what were going to learn now is how to take powers of numbers in other words exponents and also square roots so lets jump back to our document that weve showed you how to navigate to on the Oracle website here are the methods in the math class and if you scroll down there is a method under P these are in alphabetical order so go up to P theres P o W and that stands for power so it says it returns the value of the first argument raised to the power of the second argument notice that it tells you that in order to use the power function you should pass it a double number which is a decimal point number and then you can have another double number there so the way this would work would be number a raised to the power of number B so if you wanted to do 3 squared or 4 squared then you would have a 4 up here and then a 2 there so its the first number raised to the second now it tells you that you have to pass double double numbers there but it works fine if you pass it just integers as well and Ill show you how that works here in a minute so here we have a blank canvas here and if we wanted to just show you how this works we could say system dot out dot print Ln open this guy up inside here we can just pass a calculation its going to return a number and thatll be handled by the print line function or method to output that to the screen so as always we have to use these math functions by referencing the

class which is the math class dot in this case P Oh W notice its trying to tell me that heres a method here called P Oh W and its a method so were going to open a parenthesis notice its asking for argument 0 and argument 1 its trying to tell you that the the power method requires two arguments and heres what theyre called here so I can just kind of get rid of all that stuff and I can put my own numbers in there so for instance if I wanted to calculate what is 3 squared then you do 3 comma 2 so its the first number raised to the power of whatever the second number I have in there so 3 squared this method is going to return a number with is then dumped into the print statement and thats why I can do it like this let me go and hit save and the answer is gonna be 9 because 3 squared is 9 notice that the result comes back as a decimal point here so lets go back in here and you can see when youre looking at the power method here it requires number a which is supposed to be a double which is a floating-point number and also a number B and it tells you it returns a raise to the power of B here is the answer but notice over here dont worry about static look at the second word double what this means that whatever is written out out in front of the method is what is returned by the method so the the power method calculates something and it returns the answer as a double which means its a decimal number so what came out of this was a decimal number 9.0 now notice that in the definition of the method here it said it required double

a and double B which are two decimals but I didnt actually give it decimals I gave it a whole number 3 and a whole number 2 which are like integers so the the math library is smart enough to know that when I pass integers itll itll do that conversion into 3.0 and 2.0 and giving me the result in terms of a double a double variable there or a double number so for instance I could do 3.0 and I can do 2.0 and Im going to of course get exactly the same result I can do 3.2 5 raised to the power of 2.8 7 so 3.2 5 raised to the power of 2 point 8 7 and the answer I get back of course is going to be a double precision number because thats how the math libraries are generally structures to return double numbers back but it works just fine if I put whole numbers in because behind the scenes Java is doing the conversion and casting it into a double before it does the calculation so if I do 7 and raise it to the power of 2 then I expect to get 49 because 7 times 7 is 49 if I raise to the power 5 I expect a much larger number back and in fact I get sixteen thousand eight hundred and seven so Im passing numbers here in but just remember you can declare any variables doubles or integers or whatever and you and pass them in as variable names rather than just these numbers here so you could be doing a calculation with some variables that would then yield an answer and I can store the result in some other variable as well so thats how to handle powers if Im doing 10 squared 10 to the power of 3 10 to the power 4 whatever often times we

also would like to calculate the square root of something square roots up is something that we frequently need to do so let me jump back in here Ill show you where that is if we scroll down eventually were going to get to sq RT which tells you it returns the correctly rounded positive square root of a double value so its telling you that you should pass into this method a double which is a double variable double precision variable and the outcome of that should be a double but again in many cases in Java if you pass it an integer itll be smart enough to cast it into a double do the calculation and give you the answer back so lets test this here math capital m and then sq RT notice its trying to tell me theres a method called square root that requires a double and then in there I can just say for now of course it says it requires a double so Ill put 4.0 inside of there and then I should get an answer of 2 because the square root of 4 is 2 now if I take this decimal point away lets see if it works works just fine because Java knows that whenever Im using these math functions frequently Ill give it a whole number and itll just put a invisible decimal point there converting its double prime to the calculation actually being done so you can put anything in here 935 takes square root of 935 for instance and Ill get a number with lots of precision after it now the question remains or I guess I shouldnt say the question but the statement remains is that it returns the correctly rounded positive square root all right remember you learn from math class that when you take the square root of something for instance 4 there really

is usually theres always two answers so in this case let me go ahead and hit run here were getting the positive 2 actually the other valid answer here is negative 2 because negative 2 times negative 2 also gives you a positive 4 well Java doesnt handle it doesnt give you the the other result it only gives you the positive square root answer its the same thing that you would get out of a calculator just make sure I understand that the other thing is you learn a long time ago in math class that square roots of negative numbers dont work too well so for instance if I put negative 2 here hit save and run notice it says in a n so if you ever say in a n it means not a number and thats basically saying that the result of this calculation doesnt give me a real number back the key word there being a real number if you if you go and more advanced math classes youll find out that you can take the square root of negative numbers all day long what you get is something called an imaginary number well this is a programming language it deals with a real number so youre not going to be doing complex or I should say imaginary number math with the math libraries here its if it gives you a positive real result itll give you the number if it gives gets into something that it doesnt know how to do or it requires imaginary numbers to give you the answer its going to give you something like in a n so bottom line cannot take square root of negative numbers in Java so thats a basic idea of how you calculate the powers anything raised to an exponent of something else and also how to calculate the square root of numbers in Java

basic java, basic java programming, java, java course, java for beginners, java online course, java online tutorial, java programming, java tutorial, learn j…
Thank you for watching all the articles on the topic Java Tutorial – 15 – Powers and Square Roots (Math Functions). All shares of are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment