Prerequisites:
Basic Computer Literacy
Basic Understanding of Programming Concepts
A Computer (PC, Mac, or Linux)
Java Development Kit (JDK)
IDE (Integrated Development Environment)
Course Textbook or Reference Materials
Access to Online Course Portal (if applicable)
This course introduces the core concepts of Java programming and equips students with the essential skills to develop basic Java applications. Through practical lessons and hands-on exercises, learners will gain a deep understanding of Java's syntax, object-oriented programming (OOP) principles, and essential libraries. By the end of the course, students will be able to write and debug Java programs, understand fundamental coding concepts, and apply problem-solving skills to software development challenges.
This course is designed for beginners with little to no programming experience, as well as those looking to refresh or solidify their Java knowledge. It’s ideal for anyone interested in pursuing software development, web development, or Android development, as Java is a foundational language in these fields.
By the end of the Java Programming Essentials course, students should be able to achieve the following outcomes:
Write Basic Java Programs:
Master Object-Oriented Programming (OOP) Principles:
Handle Errors with Exception Handling:
Work with Arrays and Collections:
Understand and Apply Java Libraries:
java.util
and java.io
) to perform common tasks like file reading/writing and handling collections.Write Basic File Input/Output (I/O) Programs:
Work Efficiently with Development Tools:
Solve Basic Programming Problems:
Prepare for Intermediate Java Learning:
What is Java? Java is a popular programming language, created in 1995. It is owned by Oracle, and more than 3 billion devices run Java. It is used for: Mobile applications (specially Android apps) Desktop applications Web applications Web servers and application servers Games Database connection And much, much more! Why Use Java? Java works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc.) It is one of the most popular programming languages in the world It has a large demand in the current job market It is easy to learn and simple to use It is open-source and free It is secure, fast and powerful It has huge community support (tens of millions of developers) Java is an object oriented language which gives a clear structure to programs and allows code to be reused, lowering development costs As Java is close to C++ and C#, it makes it easy for programmers to switch to Java or vice versa Get Started When you are finished with this tutorial, you will be able to write basic Java programs and create real-life examples. It is not necessary to have any prior programming experience.
Java Install Some PCs might have Java already installed. To check if you have Java installed on a Windows PC, search in the start bar for Java or type the following in Command Prompt (cmd.exe): C:\Users\Your Name>java -version If Java is installed, you will see something like this (depending on version): java version "22.0.0" 2024-08-21 LTS Java(TM) SE Runtime Environment 22.9 (build 22.0.0+13-LTS) Java HotSpot(TM) 64-Bit Server VM 22.9 (build 22.0.0+13-LTS, mixed mode) If you do not have Java installed on your computer, you can download it for free at oracle.com. Note: In this tutorial, we will write Java code in a text editor. However, it is possible to write Java in an Integrated Development Environment, such as IntelliJ IDEA, Netbeans or Eclipse, which are particularly useful when managing larger collections of Java files. Java Quickstart In Java, every application begins with a class name, and that class must match the filename. Let's create our first Java file, called Main.java, which can be done in any text editor (like Notepad). The file should contain a "Hello World" message, which is written with the following code: Main.java public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } Don't worry if you don't understand the code above - we will discuss it in detail in later chapters. For now, focus on how to run the code above. Save the code in Notepad as "Main.java". Open Command Prompt (cmd.exe), navigate to the directory where you saved your file, and type "javac Main.java": C:\Users\Your Name>javac Main.java This will compile your code. If there are no errors in the code, the command prompt will take you to the next line. Now, type "java Main" to run the file: C:\Users\Your Name>java Main The output should read: Hello World Congratulations! You have written and executed your first Java program. W3Schools' Java Editor When learning Java at W3Schools.com, you can use our "Try it Yourself" tool, which shows both the code and the result. It is used to write, run, and test code right in your browser: Main.java public class Main { public static void main(String[] args) { System.out.println("Hello World"); } }
Java Syntax In the previous chapter, we created a Java file called Main.java, and we used the following code to print "Hello World" to the screen: Main.java public class Main { public static void main(String[] args) { System.out.println("Hello World"); } } Example explained Every line of code that runs in Java must be inside a class. And the class name should always start with an uppercase first letter. In our example, we named the class Main. Note: Java is case-sensitive: "MyClass" and "myclass" has different meaning. The name of the java file must match the class name. When saving the file, save it using the class name and add ".java" to the end of the filename. To run the example above on your computer, make sure that Java is properly installed: Go to the Get Started Chapter for how to install Java. The output should be: Hello World The main Method The main() method is required and you will see it in every Java program: public static void main(String[] args) Any code inside the main() method will be executed. Don't worry about the keywords before and after it. You will get to know them bit by bit while reading this tutorial. For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method. System.out.println() Inside the main() method, we can use the println() method to print a line of text to the screen: public static void main(String[] args) { System.out.println("Hello World"); } Note: The curly braces {} marks the beginning and the end of a block of code. System is a built-in Java class that contains useful members, such as out, which is short for "output". The println() method, short for "print line", is used to print a value to the screen (or a file). Don't worry too much about how System, out and println() works. Just know that you need them together to print stuff to the screen. You should also note that each code statement must end with a semicolon (;).
Print Text You learned from the previous chapter that you can use the println() method to output values or print text in Java: You can add as many println() methods as you want. Note that it will add a new line for each method:
Java Comments Comments can be used to explain Java code, and to make it more readable. It can also be used to prevent execution when testing alternative code. Single-line Comments Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java (will not be executed). This example uses a single-line comment before a line of code:
ava Variables Variables are containers for storing data values. In Java, there are different types of variables, for example: String - stores text, such as "Hello". String values are surrounded by double quotes int - stores integers (whole numbers), without decimals, such as 123 or -123 float - stores floating point numbers, with decimals, such as 19.99 or -19.99 char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes boolean - stores values with two states: true or false
Java Type Casting Type casting is when you assign a value of one primitive data type to another type.In Java, there are two types of casting: Widening Casting (automatically) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double Narrowing Casting (manually) - converting a larger type to a smaller size type double -> float -> long -> int -> char -> short -> byte
Java Operators Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values:
Java Strings Strings are used for storing text. A String variable contains a collection of characters surrounded by double quotes
A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. Why use methods? To reuse code: define the code once, and use it many times.
Parameters and Arguments Information can be passed to methods as a parameter. Parameters act as variables inside the method. Parameters are specified after the method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
Method Overloading With method overloading, multiple methods can have the same name with different parameters:
Java Scope In Java, variables are only accessible inside the region they are created. This is called scope.
Java Recursion Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve. Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
Java - What is OOP? OOP stands for Object-Oriented Programming. Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Java Classes/Objects Java is an object-oriented programming language. Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
Java Class Attributes In the previous chapter, we used the term "variable" for x in the example (as shown below). It is actually an attribute of the class. Or you could say that class attributes are variables within a class:
Java Class Methods You learned from the Java Methods chapter that methods are declared within a class, and that they are used to perform certain actions
Java Constructors A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes
Modifiers By now, you are quite familiar with the public keyword that appears in almost all of our examples:
Encapsulation The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must: declare class variables/attributes as private provide public get and set methods to access and update the value of a private variable
Java Packages & API A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories: Built-in Packages (packages from the Java API) User-defined Packages (create your own packages)
Java Inheritance (Subclass and Superclass) In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class superclass (parent) - the class being inherited from To inherit from a class, use the extends keyword
Java Polymorphism Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
Java Inner Classes In Java, it is also possible to nest classes (a class within a class). The purpose of nested classes is to group classes that belong together, which makes your code more readable and maintainable.
Abstract Classes and Methods Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces (which you will learn more about in the next chapter).
Interfaces Another way to achieve abstraction in Java, is with interfaces. An interface is a completely "abstract class" that is used to group related methods with empty bodies:
Enums An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma. Note that they should be in uppercase letters
Java User Input The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings
Java Dates Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.
Java ArrayList The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want.
Java LinkedList In the previous chapter, you learned about the ArrayList class. The LinkedList class is almost identical to the ArrayList
Java Sort a List In the previous chapters, you learned how to use two popular lists in Java: ArrayList and LinkedList, which are found in the java.util package. Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically.
Java HashMap In the ArrayList chapter, you learned that Arrays store items as an ordered collection, and you have to access them with an index number (int type). A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String).
Java HashSet A HashSet is a collection of items where every item is unique, and it is found in the java.util package
Java Iterator An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an "iterator" because "iterating" is the technical term for looping.
Java Wrapper Classes Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.
Java Exceptions When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things. When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).
What is a Regular Expression? A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for. A regular expression can be a single character, or a more complicated pattern.
Java Threads Threads allows a program to operate more efficiently by doing multiple things at the same time. Threads can be used to perform complicated tasks in the background without interrupting the main program
Java Lambda Expressions Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
Java Advanced Sorting In the List Sorting Chapter, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it? To sort objects you need to specify a rule that decides how objects should be sorted. For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first.
ava File Handling The File class from the java.io package, allows us to work with files.
To create a file in Java, you can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if the file already exists. Note that the method is enclosed in a try...catch block.
Read a File In the previous chapter, you learned how to create and write to a file. In the following example, we use the Scanner class to read the contents of the text file we created in the previous chapter
Delete a File To delete a file in Java, use the delete() method:
Add Two Numbers Learn how to add two numbers in Java:
Count Number of Words in a String You can easily count the number of words in a string
Reverse a String You can easily reverse a string by characters
A computer instructor and software developer
JAVA PROGRAMMING ESSENTIALS