Well done!
You have completed Java Basics!
You have completed Java Basics!
In this video we will explore the development environment we will be using for the rest of the course, Workspaces. We will write, compile and run a command line program that will introduce ourselves.
Additional Content
- Learn more about the command line in the Introduction to the Terminal course.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upHello and welcome. 0:09 I'm Travis and I'm a teacher here at Treehouse. 0:10 In this course, I'll be introducing you to some very common tools 0:12 available in almost every programming language. 0:16 Whether you're completely new to programming or have written some code 0:19 before, you'll find these concepts invaluable on your coding journey. 0:22 When going through this course, 0:29 I want you to think of Java learning a new foreign language. 0:30 I'll introduce you to the rules, or syntax, of the language as you need it. 0:34 Just as you would learn a foreign language through immersion, 0:38 you'll naturally start recognizing patterns 0:41 and concepts that repeat across different parts of code. 0:43 Don't worry if you encounter unfamiliar terms or expressions. 0:47 That's a normal part of the learning process. 0:51 We'll focus on one concept at a time, and while I might occasionally ask you 0:53 to temporarily skip over certain bits of code, I promise we'll circle 0:57 back to them when the time is right. 1:01 So let's start our Java journey the same way you'd begin 1:05 learning a foreign language, by learning how to introduce ourselves. 1:07 Our first program will be simple but meaningful. 1:11 We'll write code to display, hello, my name is, and then your name on the screen. 1:14 Now before we write our first line of code, 1:19 let me introduce you to our coding environment, Workspaces. 1:21 Look for the Launch Workspace button right beneath the video. 1:24 Go ahead and click it, 1:27 and a new browser window will open with everything you need to start coding. 1:28 Let's go! 1:32 Alright, welcome to your Workspace. 1:34 It's all set up and ready for you. 1:36 On the side there a list of all the files available to you in this Workspace 1:38 I already created a file to get us started 1:43 called Introductions.java. Let's open it up and take a look 1:45 Right here, you'll see a note. 1:51 This is called a comment. 1:53 In Java, comments start with two forward slashes. 1:54 Everything after these slashes is ignored by Java when it runs your program. 1:58 Programmers use comments to explain what their code 2:03 does in plain English or to leave notes for others. 2:05 One important goal in programming is to write code that's clear 2:09 enough to not need any comments. 2:12 But since you're just starting out, please feel free to add comments 2:14 whenever you want to remind yourself what your code is doing. 2:17 Now, remember I said I may ask you to skip over some parts until later? 2:20 Right now, that's what we'll do with most of the code you see on the screen. 2:24 It's setup code that every Java program needs, 2:28 and I've already taken care of it for you, 2:31 so don't worry if it doesn't make sense quite yet. 2:33 There is really only one line I want you to focus on for now, this one right here. 2:36 You don't need to understand exactly how it works just yet. 2:40 What's important is that this line creates something called a Java object. 2:43 Don't worry, we'll explain objects in detail later. 2:48 This object has a special function, 2:51 called a method, which lets us write text to the terminal down here. 2:53 If for some reason you don't see a terminal at the bottom, 2:58 you can come up here to View and select Show Console and it should pop in for you. 3:00 I know I just mentioned some new terms objects and methods. 3:06 Don't worry if they sound complicated. 3:09 We'll explore them step by step as we go. 3:11 For now, just keep familiarizing yourself with the language. 3:14 I'll explain these ideas when they become relevant. 3:17 So thanks to the setup I've done, we have access to this object called Console. 3:20 We can use this to write text on the screen. Objects have methods 3:25 that let them perform actions. We want to print text and the method for 3:29 that is called printf. Here's how we do it. 3:33 First type console with a lowercase c, 3:37 then a dot to access its methods, 3:40 followed by the name of the method, printf. 3:43 To call a 3:46 method, we add a pair of opening and closing parentheses. 3:47 Inside these parentheses, we can add some double quotes 3:51 and inside of those you can write the text you want to print. 3:54 So let's write, hello my name is Travis. 3:57 You can put your name in there of course though. 4:00 We then finish 4:05 with a semicolon to end the statement. 4:06 You may have noticed a small dot showing up in the tab 4:09 when I started editing the file. 4:12 This means there are unsaved changes. 4:14 Now to save the file I'll click file and then save. 4:16 But as you can see, you can also quickly save by using 4:19 the shortcut Command-S on Mac or Control-S on Windows. 4:22 That's what I'll be using going forward. 4:26 Java is a compiled language, which means 4:31 we need to run a program called a compiler to turn our human-readable Java code 4:33 into computer-readable code before we'll be able to run it. 4:38 To do this, we go to the console and type 4:42 the command javac Introductions.java 4:45 In this command, 4:53 javac stands for java compiler. 4:53 This tells the compiler to convert the code 4:57 in Introductions.java into an executable file. 5:00 I'll press enter now. 5:03 If we list the files 5:08 in this folder by typing ls, 5:09 We see a new file called Introductions.class. 5:13 You could also right in the sidebar here and click 5:17 refresh. 5:19 By the way, 5:23 ls means list, and it shows all the files in the current folder. 5:24 We'll be using some terminal commands 5:28 this throughout the course, and I'll explain them as we go. 5:30 If you want to learn more about using the terminal, 5:33 check out our terminal course 5:35 which I've linked in the teacher's notes below this video. 5:36 Okay, next, to run our program, we type java Introductions. 5:40 Notice this time the command is java, without the c. 5:47 The java command runs the program. 5:50 Also, we only type the class name introductions, without the extension. 5:53 Java c required us to type the .java extension, this java command does not. 5:57 I'll press enter, 6:03 and here on the screen we see the output. 6:05 Nice! 6:07 You've just compiled and run your first Java program. 6:10 You're learning a lot already. 6:12 Before we learn even more, 6:14 let's do an exercise to review what we've covered so far. 6:15 But first, I want to remind you about the Treehouse community. 6:19 If you have questions, chances are someone else 6:23 has already had the same question and maybe even found an answer. 6:25 Learning how to ask questions as well as looking for answers 6:29 is one of the best ways to learn. 6:32 And also, if you already have some Java experience, 6:34 you might be familiar with IDEs, or Integrated Development Environments. 6:37 These tools make coding easier with features code completion, and 6:42 while IDEs are great, I encourage you to keep following along in this workspace. 6:46 Doing things manually helps build your confidence and understanding 6:51 by removing some of the magic that IDEs provide. 6:55 We'll explore IDEs in a future course when the time is right. 6:58
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up