Java Tutorial for Beginners
Programming with Mosh
Mosh is a software engineer with over two decades of experience and has taught over 3 million people
... [Show More] how to code. He has designed this course for anyone who wants to learn Java. If you're a beginner, don't worry - Mosh promises to make Java super simple and hold your hand through this course. He hopes you'll stick around and learn this beautiful and powerful programming language.
The Anatomy of a Java Program
In this Java tutorial, we'll be looking at the basics of a Java program. The smallest building block in Java programs are functions. If a function is a block of code that performs a task, think of the buttons on the remote control of your TV. Every Java program should have at least one function and that function is called main.
Writing Java Code
After the parentheses, we have a pair of curly braces and inside these braces, we write the actual Java code. Every Java program should have at least one function and that function is called main. So, main is the entry point to our programs. Whenever we execute a Java program, the main function gets called and the code inside this function gets executed.
Downloading IntelliJ IDEA
IntelliJ IDEA is absolutely free and it's more than enough for this course, so download it now.
Download IntelliJ IDEA
A class is a container for related methods, so we use classes to organize our code. By the same token, we have a concept called package, and we use a package to group related classes.
As our applications grow, we're going to end up with hundreds or even thousands of classes, so we should properly organize these classes into packages. The base package for a Java project is the domain name of your company in reverse.
The code editor might look a little bit intimidating at first, but trust me it's really easy and you're going to learn about it throughout this course. For now, just type a base package for your project; it can be your name or whatever, it doesn't really matter.
Marsh has put together a comprehensive cheat sheet with summary notes that you can download below this video.
IntelliJ is building our application and we can see the result in this little terminal window. So, here's our "Hello World" message. That was our first Java program.
Next, I'm going to explain how Java code gets executed under the hood.
IntelliJ uses the Java compiler to compile our code into a different format called Java bytecode. This Java code is platform-independent, meaning it can run on Windows, Mac, Linux, or any operating system that has a Java Runtime Environment. Java applications are portable, or platform-independent.
Java was developed by James Gosling in 1995 at Sun Microsystems, which was later acquired by Oracle in 2010. Currently, close to 9 million developers worldwide use Java. About 3 billion mobile phones run Java, as well as 120 million TV sets and every Blu-ray player. The average salary of a Java developer is just over $100,000 per year in the US.
This course is the first part of my complete four-part Java series. Each part is about three to four hours long, so you can easily complete it in a day or two. The first part will give you a solid foundation on how to start programming in Java. In the second part, we'll talk about Object-Oriented Programming, which is a style used in most, if not all, Java applications. The third part will cover Core Java APIs, or Application Programming Interfaces.
Once you learn all this, I'm going to give you a project. You're going to build a mortgage calculator on your own, so make sure to pay great attention to all the materials. Make sure to use most of them in this project. Are you ready? Now, let's jump in and get started.
Variables in Java
In this tutorial, we're going to talk about variables in Java. We use variables to temporarily store data.
Types in Java
In the next tutorial, we're going to talk about various types in Java. We have two categories of types: primitive types and non-primitive types, or reference types for storing complex objects. All these types are for storing whole numbers that don't have a decimal point. In one byte, we can store values from -128 to 127.
Using Underscore in Large Numbers
In Java, whenever you deal with a large number like this, you can use an underscore to separate every three digits, just like how we use a comma in our documents to make our numbers more readable.
Integers
With integers, we can store values up to two billion. But let's say the number of times this video has been watched is three billion, so I had a three here, now we have a red line that indicates an error.
Intellij automatically adds this line for us: import java. This is one of the differences between primitive and reference types. When declaring primitive types, we don't need to allocate memory. When dealing with reference types, we should always allocate memory.
Code snippets are helpful since they allow us to quickly generate code. Let's pass our data object here. Note that I have not surrounded this variable with double quotes because this is a string.
In this example, we have two different variables, x and y, and these two variables are at different memory locations, so they're completely independent of each other.
Java has a Point class that is defined in the package java.awt. Let's declare a variable likePoint1 and set it to new Point. Here we can pass the initial values for x and y, so I'm going to pass 1 and 1. Intelligent automatically adds these labels.
When we declare a primitive variable like a byte, the value that we assigned to that variable will be stored in that memory location. But when we use a reference type, our variable is going to hold the ax of that point object in memory, not the actual pointer object.
Java's lang package is automatically imported, so we can use any classes defined in this package. We can join or concatenate a string with another using the plus operator. We can also use this to check if our string ends with a character or sequence of characters, for example. The message dot length is useful when we want to check the length of user input. Another useful method, index of, returns the index of the first occurrence of a character, string, or sentence we pass as a parameter. With this method, we can check if a string contains certain characters or words or sentences.
Java includes special characters in strings, such as a tab or a new line or a backslash or double quotes. Sometimes our users type unnecessary spaces at the beginning or end of a string in form fields, so using the trim method, we can remove these white spaces.
Methods:
length: Returns the length of the string.
indexOf: Returns the index of the first occurrence of the parameter string.
trim: Eliminates leading and trailing whitespace.
Special Characters:
\t: Tab
\n: New Line
\\: Backslash
\" : Double Quotes
String str1 = "Hello";String str2 = "World";String result = str1 + " " + str2;int length = result.length(); // 11int index = result.indexOf("World"); // 6String input = " Hello World ";String trimmed = input.trim(); // "Hello World"
Sometimes, if we have something written after a string in quotes, the Java compiler thinks this is the termination of our string. Therefore, we may encounter a compilation error.
In this tutorial, we will discuss arrays in Java. Arrays are used to store a list of items. To indicate a tab, use a backslash t. For example, "c:\twindows" means the C drive and Windows.
In Java, arrays have a fixed size. Once we create them, we cannot add or remove additional items. If you need to manipulate items, use one of the collection classes that we will cover later in the course.
Remember, arrays are used to store a list of objects in Java. We can also create multidimensional arrays, such as a 2D array to store data for a cube, which is useful in scientific computations. To access individual items in this array, we need to supply two indices: the row index and column index [Show Less]