Core Java — syntax, OOP, collections, I/O and advanced topics — written by Aivasu Tech Institute's mentors. Free to read, structured to actually help you learn.
Java notes by Aivasu Tech Institute cover 121 topics — from java home to java projects — each explained with short definitions, syntax and runnable code examples. They are 100% free, need no signup, and work as quick revision for college exams, Java interviews and Aivasu Tech Institute's mentor-led Java course in Pitampura, Delhi.
Java Tutorial chapters
Java Methods chapters
Java Classes chapters
Java Errors chapters
Java File Handling chapters
Java I/O Streams chapters
Java Data Structures chapters
Java Advanced chapters
Java Projects chapters
No notes found. Try a different search term, or browse all Java notes.
Java is one of the world's most widely used programming languages.
Java is a popular and powerful programming language, created in 1995.
At W3Schools, you can try Java without installing anything.
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:
A computer program is a list of "instructions" to be "executed" by a computer.
You learned from the previous chapter that you can use the println() method to output values or print text in Java:
You can also use the println() method to print numbers.
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.
Variables are containers for storing data values.
The println() method is often used to display variables.
To declare more than one variable of the same type, you can use a comma-separated list:
All Java variables must be identified with unique names.
When you do not want a variable's value to change, use the final keyword.
Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types, and so on). This is done to
As explained in the previous chapter, a variable in Java must be a specified data type:
Primitive number types are divided into two groups:
Very often in programming, you will need a data type that can only have one of two values, like:
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
Non-primitive data types are called reference types because they refer to objects.
The var keyword was introduced in Java 10 (released in 2018).
Type casting means converting one data type into another. For example, turning an int into a double .
Operators are used to perform operations on variables and values.
Arithmetic operators are used to perform common mathematical operations.
Assignment operators are used to assign values to variables.
Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.
As with comparison operators, you can also test for true or false values with logical operators.
When a calculation contains more than one operator, Java follows order of operations rules to decide which part to calculate first.
A String variable contains a collection of characters surrounded by double quotes ( "" ):
The + operator can be used between strings to combine them. This is called concatenation:
Warning: WARNING! Java uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.
Because strings must be written within quotes, Java will misunderstand this string, and generate an error:
The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Very often in programming, you will need a data type that can only have one of two values, like:
Let's use booleans in a real-life example where we want to find out if a person is old enough to vote.
Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.
The else statement lets you run a block of code when the condition in the if statement is false .
Use the else if statement to specify a new condition to test if the first condition is false .
There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.
You can also place an if statement inside another if . This is called a nested if statement.
You can combine or reverse conditions using logical operators. These work together with if , else , and else if to build more complex decisions.
This example shows how you can use if..else to "open a door" if the user enters the correct code:
Instead of writing many if..else statements, you can use the switch statement.
Loops can execute a block of code as long as a specified condition is true.
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true. Then it will repeat
To demonstrate a practical example of the while loop, we have created a simple "countdown" program:
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
It is also possible to place a loop inside another loop. This is called a nested loop.
There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data structures):
To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
You have already seen the break statement used in an earlier chapter of this tutorial. It was used to "jump out" of a switch statement.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:
A multidimensional array is an array that contains other arrays.
A method is a block of code which only runs when it is called.
Information can be passed to methods as a parameter. Parameters act as variables inside the method.
In the previous page, we used the void keyword in all examples (like static void myMethod(int x) ), which indicates that the method should not return a value
With method overloading, multiple methods can have the same name with different parameters:
In Java, variables are only accessible inside the region where they are created. This is called scope.
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simpler problems which are ea
OOP stands for Object-Oriented Programming.
Java is an object-oriented programming language.
In the previous chapter, we used the term "variable" for x in the example (as shown below).
You learned from the Java Methods chapter that methods are declared within a class, and that they are used to perform certain actions:
A constructor in Java is a special method that is used to initialize objects.
The this keyword in Java refers to the current object in a method or constructor.
By now, you are quite familiar with the public keyword that appears in almost all of our examples:
Non-access modifiers do not control visibility (like public or private ), but instead add other features to classes, methods, and attributes.
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
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
Java Inheritance (Subclass and Superclass)
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
In Java, the super keyword is used to refer to the parent class of a subclass.
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
Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract c
Another way to achieve abstraction in Java, is with interfaces.
An anonymous class is a class without a name. It is created and used at the same time.
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
An enum can also have a constructor just like a class.
The Scanner class is used to get user input, and it is found in the java.util package.
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
Even experienced Java developers make mistakes. The key is learning how to spot and fix them!
After learning about common errors, the next step is understanding how to debug your Java code - that is, how to find and fix those errors effectively.
As mentioned in the Errors chapter, different types of errors can occur while running a program - such as coding mistakes, invalid input, or unexpected situatio
Sometimes, different errors (exceptions) can happen in the same try block. You can handle them with multiple catch blocks.
When working with files, streams, or other resources, it is important to close them after use. If you forget to close a resource, it may keep using memory or ev
File handling is an important part of any application.
In Java, you can create a new file with the createNewFile() method from the File class.
If you are just starting with Java, the easiest way to write text to a file is by using the FileWriter class.
In the previous chapters, you learned how to create and write to a file.
To delete a file in Java, use the delete() method:
You've already seen how to create, read, and write simple text files.
So far, you have used the Scanner class to read text files. Scanner is very convenient for text because it can split input into lines, words, or numbers. Ho
Earlier, you learned how to write text to files using FileWriter .
BufferedReader and BufferedWriter make reading and writing text files faster.
The BufferedWriter class is used to write text to a file, one line or one string at a time. If the file already exists, its contents will be replaced (overwri
Data structures are ways to store and organize data so you can use it efficiently.
Before we explore ArrayList , HashSet , HashMap , and other data structures in more detail, it's important to understand that all of these are part of someth
The List interface is part of the Java Collections Framework and represents an ordered collection of elements.
An ArrayList is like a resizable array.
In the previous chapter, you learned about the ArrayList class. The LinkedList class is almost identical to the ArrayList :
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.
The Set interface is part of the Java Collections Framework and is used to store a collection of unique elements.
A HashSet is a collection of elements where every element is unique.
A TreeSet is a collection that stores unique elements in sorted order.
A LinkedHashSet is a collection that stores unique elements and remembers the order they were added.
The Map interface is a part of the Java Collections Framework and is used to store key-value pairs. Each key must be unique, but values can be duplicated.
A HashMap stores items in key/value pairs, where each key maps to a specific value.
A TreeMap is a collection that stores key/value pairs in sorted order by key.
A LinkedHashMap stores keys and values, and keeps them in the same order you put them in.
An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet .
In the previous chapters, you learned how data structures (like ArrayList , HashMap , etc.) are used to store and organize data.
Wrapper classes provide a way to use primitive data types ( int , boolean , etc..) as objects.
Generics allow you to write classes, interfaces, and methods that work with different data types, without having to specify the exact type in advance.
Annotations are special notes you add to your Java code. They start with the @ symbol.
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 wh
Threads allows a program to operate more efficiently by doing multiple things at the same time.
A lambda expression is a short block of code that takes in parameters and returns a value. Lambdas look similar to methods, but they do not need a name, and the
In the List Sorting Chapter, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it?
Join Aivasu Tech Institute's Java course — live mentorship, hands-on projects, and 100% placement support in Delhi NCR.
Enroll Now — Free Demo Available✳ Common questions
What students search before reading Java notes.