课程: Learning Java 17
Using built-in functions in Java
- [Instructor] In this chapter, we've been creating our functions from scratch. We've defined various functions, added parameters, added return types, and then called them in the main function. We refer to these functions as user-defined functions because we, as the users of Java and software engineers are creating them. We're defining the finite steps, naming the function appropriately and then calling it in our code. There are many functions that are commonly used, and as a courtesy, the creators of Java have defined some of them for us. We just have to call the function by name, we don't have to define it. One function that we've been using a lot that's built into Java is println. We never defined println, but we can call it because it's a built-in function in Java. Back at the beginning of this course, we talked about operations on data types. Operations we can run on Strings on ends, and many of these operations are really functions. The charAt operation that we've run on Strings is really a String function. The value we add between the parentheses is the index for the character we want to access. The way we call built-in functions might seem a little different than our user-defined functions, and that's because it is. We're often using the dot operator to get access to these built-in functions. For system.out.println, we have to access out from system and then println from out. For charAt, we have to have a String that's already created in order to access it. We'll talk more about the dot operator in the next chapter, but the main takeaway here is that sometimes we'll define our own custom functions and sometimes we'll use built-in functions. So how do you find out about all these built-in functions? Let's say you want to create a custom function for exponentiation, something that will take a given number to the second power or the fifth power. This is a common task and there might be something in Java that already does this. So you'll Google around and try to find some examples of a built-in function that already does exponentiation, or an example of a user-defined function. Let's try doing that now. All right, here's one. There's no operator, but there's a method. Math.pow. We access pow from the math class. Let's see if we can find something more official like the Java documentation. Here we have the math class. If we scroll down or rather search for exponentiation, here's something that might help us. And there's the pow operation. It returns the value of the first argument or the first input raised to the power of the second argument or the second input. A to the power of B, where A and B are inputs. With this function, we take the base to the power of the exponent, hence the name P-O-W or pow. The way we'll access it is through math using the dot operator since it's in the math class. Let's try writing this in our program. We'll write math. and a whole bunch of stuff pops up. These are all the built-in functions that live in the math class, a class that's already in Java. we'll want to use pow, which takes in a double A and a double B and it returns a double. This is another benefit to using an IDE. You get so much information about what's built into Java. Let's use it. We'll have two as our base or the value of A in this formula, in this function, and we'll have B be five. So we're taking two to the power of five. We'll save it in a variable called result, and then we'll print it to the console. Let's run it. We get 32. That's two to the power of five. Now these functions that we saw with math. are actually part of the standard library, the Java standard library. It contains some different built-in functions that we can access. So what's the benefit of using built-in functions? Well, you don't have to write the implementation of the function yourself, it's already defined and you can just call it. All you have to know is what the function takes as input and what the expected output is. Usually, if there's a built-in function for the thing you want to accomplish, you should use it instead of creating your own custom function. Built-in function have been tested a lot by the creators of the programming language, so they're basically guaranteed to do what they're described to do.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。