Join Todd Perkins for an in-depth discussion in this video Variables, part of Learning Java Applications.
- So let's review some Java basics before we start developing applications. So I'm in Eclipse right now and I'm actually in the Chapter 02 workspace. So if you want to change your workspace, you can do that under File, Switch Workspace, choose a recent workspace here or just click Other, which opens up that workspace launcher, and then you can click Browse and go to the appropriate chapter. So inside of Chapter 02, I have this existing project called "Review." Remember, that you can create a project by clicking the New Project button here, so the button there at the top, choosing Java Project, or go to File, New, Java Project.
From there, give the project a name and then click Finish. So we have this project called "Review" right here. And we're going to create a class file. So I'm going to right click the source folder and then choose Class under New. So in here, I'm going to leave the package at the default. Remember that packages are used to organize your classes. That your class that might be named "Review" for example can be differentiated from another developer's class with the same name, so long as it has a different package.
Usually developers use a reverse domain structure for their packages. For example, since I own toddperkins.com, my package would be com.toddperkins. And then I would name the class "Review," and if I worked with another developer who owned a different website and had a different package name, their review class would not conflict with mine. But for what we're doing, we don't even need packages, so I'm going to delete that. So I'll name the class "Review," and I'm going to choose to add a main method here. And I'm going to leave the rest of the settings at the defaults, and click Finish.
So here's my class in here. Now you may notice that my fonts are a little bit large. You can always change your font sizes under Eclipse, Preferences on Mac, or Windows Preferences on PC under General, Appearance, Colors and Fonts. Select the text editor or text font under Basic or under Java. Click the Edit button and change the font there. I'm using Monaco 18. So back into Eclipse, and let's start working with variables. Before we do that, I'm just going to get a little bit more space by minimizing some of these windows.
So I'm clicking that minimize button on some of these views, so it's easier to see. So I'm going to put my cursor, tab in inside of the main function, and we're going to create a variable. In Java, we create variables by typing the data type first, then we type in the name of the variable. And then we set the value. So this variable's going to be of the type String. It has the name of myName, and I set it equal to this string Todd. Now let's print that out. System.out.print.
We'll use println, passing in myName, and then we'll run the application. So I'm going to save the file. File, Save, or command S on the Mac, Control S on the PC. And then I can run the application. So I'm clicking the Run button in the toolbar, and then there I see my name in the console. And I'll click the button to move the console back to the bottom. And I'll just leave it up for now. I'll create another variable, under myName, and this type will be a float, call this favoriteNumber, and my favorite number will be five for this example.
And now when we print, we'll add some extra text on, so we'll concatenate the String by adding a plus, after the variable myName, apostrophe s, space, favorite number is, and then a space, and then we'll add on favoriteNumber. Save, run the application, which you can do right here, or from Run. Run. Todd's favorite number is 5.0. So remember, when you're creating variables in Java, you declare the data type, the variable name, and then you set the value with the equal sign.
And when you're working with the data, you can concatenate Strings by using the plus operator.
- Installing Java and Eclipse
- Understanding basic Java syntax
- Handling Java errors
- Creating a UI with Swing
- Creating tables and connecting to data sources for tables
- Publishing a JAR file
- Installing NetBeans
- Creating JSP pages
- Setting up and connecting to databases
- Creating Java-based Android Studio projects