Even with very simple code, you’re always using the classes provided with the core Java installation. The String class, the Math class, the helper classes that support working with primitive values, and so on—each has its own particular purpose. Knowing Java is as much about these classes and these APIs, as it is about simple syntax.
- [Instructor] Java code is encapsulated in classes. Java's JShell command line environment lets you experiment with arbitrary statements, but the code has to be wrapped in classes to be used in a real application. Each class can have as many fields, methods, and other members as you need to accomplish your application's tasks. Organizing your code is entirely up to you, though. I'll show you how to use classes that are included with the core Java installation. Even with very simple code, you're always using the classes provided with that installation: the string class, the helper classes that support working with primitive values, the math class, and so on.
Each has its own particular purpose; knowing Java is as much about understanding these classes, these APIs, as it is about simple syntax. Let's say you want to get input from the command line. There's a class for that. In my main method, I'll use a class named Scanner. I'll start typing the name of the class, and when it's shown in the list, I'll press Enter or Return. You can also press the Tab key to autocomplete the code. This class is a member of a package called "java.util".
To find out where a particular class is, in IntelliJ IDEA, you can move the cursor over the class name, then press Control on Windows, or Command on Mac, and you'll see the class's signature. That includes the package, listed at the top, the keywords that define the class's capabilities, and its inheritance, and even any interfaces that it implements. So that's the type of the object I'm creating, and I'll name it "scanner", lowercase.
Then I'll instantiate it with this code: I'll start with the keyword "new", and then once again the name of the class, "Scanner". This is the constructor method, that will be used to instantiate the class. To learn about the various versions of a constructor method, you can Control+P on Windows or Command+P on Mac, and you'll see all the constructor methods that this class supports. The scanner class can receive input from a variety of sources. I'm going to use an input stream, that's the second version of the constructor method on this list.
And the particular input stream I'm going to use, is called "system.in". Once again, you can find out what this object is by using the Control or Command key, and moving the cursor over the identifier. Next, I'll get some information from the scanner object. I'll create a string, I'll call it "input", and I'll get it from "scanner."... and these are all of the methods that are supported by this particular class. I'll use the method nextLine.
The nextLine method returns all the data, up to a line feed. Now, if you're using system.in as your input stream, practically speaking, that means you're receiving data until the user presses Enter or Return. Then, I'll report what the user typed in with system.out.println, and I'll pass in, "input". Now I'll run the application. In my Run window it looks like nothing is happening. But if I click into the window, I can type a value, I'll just type "some input", then I'll press Enter or Return, and I see the response, reporting the same value that I typed.
Now I want to give the user a hint about what they're supposed to do. So, above the existing code, I'll start with s-out, and then I'll change this from println to simply print, and then in a literal string that will be a prompt, telling the user what to do. I'll say, "Enter a value." So now the effect will be, on line 9, I'm displaying a prompt; on lines 10 and 11 I'm receiving input; and on line 12 I'm reporting what the user typed.
So now I'll make this a little friendlier, by outputting the string "You entered" followed by the input that the user typed. I'll run this little application again, and now the user sees what they're supposed to do, and once again I'll enter a value of some kind, then press Enter or Return, and I see the response. Now, there's quite a bit of work being done in the background, but as the developer, you don't need to worry about the complexities. You're using the scanner class, which encapsulates most of what you need to get the information, and system.in, which is a kind of input stream class.
Creating Java code is a matter of figuring out which classes you need, and plugging the classes together, to get the result you're looking for.
- Debugging Java code
- Handling exceptions
- Creating custom classes
- Working with inheritance
- Managing data collections
- Using Java packages and libraries
- Preparing a Java application for deployment