Saying Hello to Java
Saying Hello to Java
Difficulty
Intermediate
Duration
2h 7m
Students
249
Ratings
5/5
starstarstarstarstar
Description

This course explores the fundamentals of Java and puts them to use with some real-life examples looking at an average of three program and a mad libs project.

Learning Objectives

  • Learn how to print information out to the user, how to create and use variables, values, and constants, how these things have data types, and the differences and similarities among the data types
  • Learn about arithmetic, relational, and logical operators
  • Understand how to obtain input from the user of our programs

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In this lecture, we will take our very first steps into the wonderful world of Java programming. Java is a very popular programming language used in everything from desktop, to mobile, to game, to web development. Adding Java to your skillset will help you solve problems that are sometimes difficult or more challenging in some other languages and will help you become more marketable when looking for a job in software development. So, let's get going. In the IntelliJ IDE, which is the integrated development environment that we are using, we need to create a new project. This project will be used throughout the section. Let's call it Section2-Projects. 'New Project.' We will hit 'Next' because we have Java enabled.

Hit 'Next' again. And then this project, we will call it Section2-Projects. Make sure it's spelled right. Hit 'Finish'. Now we have to create a Java class file which we will call HelloWorld, but you can name it almost anything. We will create it in the source subdirectory of our project. Notice that spaces in class names are not allowed. Also, as a matter of convention, we should capitalize the first letter of each word in the word or phrase we are naming our class with. So, right here, right click 'New', 'Java Class', and we will name it HelloWorld. Note that Java code that we write is called source code. The computer does not directly understand Java, so that code is converted into a format that is run on the Java Virtual Machine, or JVM, on whatever platform you have it installed. When your Java source code is compiled, it is converted into a format called Java bytecode.

This bytecode is what is actually run or interpreted by the Java Virtual Machine, and that produces the results on your computer. Now that we've created a HelloWorld file, and IntelliJ has given us a skeleton class, really with nothing inside of it. Notice that the class's name is the same name of the file, in our case, HelloWorld. So, in some languages, this is just a standard convention. It's considered good practice, a good convention to use, to name the class the same as the file. But in Java, it is required that they match. So, what is a class anyway? For our purposes at this point, you can think of a class as an entity that can hold data and behaviors or operations. For this file, we're going to give this class a single behavior that is required by Java applications called the main method. So, let's write that; public static void main, and then in the set of parentheses, you will put String and then two square brackets, which are above the Enter key on a standard US/British keyboard. And we usually call it args.

And then right here, I put a open curly brace, and it will automatically generate a close curly brace to match. So, many of the details of what is in the code may not mean much to you right now, and that's completely okay. Especially if you've never programmed before, that's entirely expected. For now, you can just know that the public part means that the other classes and methods outside of this class can have full access to something. The word static means that the main method belongs to this HelloWorld class and can be called by the system directly on this class. We'll get more into static a lot later. So, that word will make a lot more sense down the line. So, don't worry if this doesn't really seem helpful at this point. Great. So, now we have a vague idea of what a class is: an entity that holds data and behaviors. And we have a single behavior, the main method. So, what is the main method? This name and syntax, public, static, void, main and the String args in the parentheses are all required. The main method is a special method that acts as the entry point of our application.

This method is loaded into memory when the program starts and whatever is in main is what is executed through the program's lifecycle. If we ran it now, it wouldn't really look like it was doing anything. So, let's make something a little more interesting happen. Let's add a little bit of output. So right here, I will put System.out.println for print line. And then in parentheses, I will put "Hello world!". Hello world. Now, this code might require some explaining. System is a class, just like the one we've created, but someone else created it, and we have access to that code ourselves that someone else has written for us. The word out is a reference to an object that lives in that class, called the standard output object, which gives us access to send the data to the standard output stream. So, what does that mean? Well, typically the standard output stream is the console, which is text-based. Finally, the println or print line is a method of the object out. So, you can send the data, in this case, the "Hello world!", to the output stream using this method println. Methods are behaviors and operations with which we can tell our programs to do something. And in this case, we're saying print the string "Hello world!" to the output stream.

And finally, notice that the string "Hello world!" is in double-quotes. And that tells us that this is a string, or that's also called a character string. This could be a single word, a couple words, an entire sentence, a paragraph, or an entire book. A string is just text data. At the very end is something very small but very important. We have this semicolon right here, this little tiny symbol right there. It isn't optional, and our program won't compile and run without the semicolon. So, let's run this simple application and see what we get. 

So to run this, I'm actually going to go over here, under src on my HelloWorld file, right-click it, and then I'm going to go down to 'Run 'HelloWorld.main()''. I click that. Takes it a few seconds. And after a little bit, voila. We have this Hello world! right here at the bottom. And this is the console output. So again, some of the output in the console is from the Java runtime environment and the IDE that we're working in, so this stuff right here. But we have our nice little Hello printed to the screen. So, note that if I make a small change to the code, like removing a semicolon or spelling one of the data types wrong, I can't run my application and I'll get an error. So, let's try to remove our semicolon there. You notice there is a little red squiggly line right there.

if I try to run it, sorry, If I try to right-click this and go to 'Run', it will actually tell us "Hey, there's a problem here." And it will say that on our line five, which is where the error is, it's as it expected a semicolon. Now, not all errors are that easy to track down, but in this case it was. So, we just add the semicolon back. And voila, we have it back. So, we can't have this type of error which is a compile-time error. It doesn't let us even compile our application code, meaning converting it to the machine code, or rather the bytecode in this case that the JVM will run, because the compiler can't make sense of the code now. Natural languages, like the ones we speak; English, German, Hindi, Portuguese, Spanish, or Arabic; are usually more forgiving.

If we mispronounce something or don't have perfect grammar, native or other fluent speakers will probably be able to make out what we mean as long as the errors are not catastrophic. Programming languages are much more strict. If you change even a small little piece of code, like removing a semicolon like we just did, you were violating the grammar of the programming language. The grammar and punctuation used in a programming language is called the syntax. When you violate those rules, you get a syntax error. So, we definitely want to avoid these.

Before we move on to the next lecture, I have a quick challenge for you. Using this same project and file, I want you right under where we're printing the "Hello world!", I want you to print a string containing "Hello", followed by your name. So, for me it would say "Hello John!". And you should run the application to test it out after you've changed this code. So, pause the video and try out this little challenge. Come back when you're done or if you need some help. How did that challenge go for you? Were you able to complete it? If you did, congratulations. That's great. But even if you didn't, that's okay. You'll get better with more practice.

Let's do it together. So, we're going to print underneath here, System.out.println. We're going to put "Hello John!". And you may or may not have put the exclamation point, that's fine. So that's our code, and we should try running it to make sure that it works. Right-click and then 'Run 'HelloWorld.main()''. Excellent, so we get our Hello world! and then we get the Hello John! in the next line. Awesome work, everyone. In the next lecture, we will discuss variables, constants, and data types which are some of the foundational building blocks we need to write code. I'll see you there.

 

About the Author
Students
1142
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics