Free · 121 Topics · No Signup
Java Notes
Core Java — syntax, OOP, collections, I/O and advanced topics — written by CodingNow 2.0's mentors. Free to read, structured to actually help you learn.
Java notes by CodingNow 2.0 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 CodingNow 2.0's mentor-led Java course in Pitampura, Delhi.
Java Tutorial
Java Tutorial chapters
Java Methods
Java Methods chapters
Java Classes
Java Classes chapters
Java Errors
Java Errors chapters
Java File Handling
Java File Handling chapters
Java I/O Streams
Java I/O Streams chapters
Java Data Structures
Java Data Structures chapters
Java Advanced
Java Advanced chapters
Java Projects
Java Projects chapters
No notes found. Try a different search term, or browse all Java notes.
Java Tutorial
55 of 55 topics publishedJava HOME
Java is one of the world's most widely used programming languages.
Java Intro
Java is a popular and powerful programming language, created in 1995.
Java Get Started
At W3Schools, you can try Java without installing anything.
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:
Statements
A computer program is a list of "instructions" to be "executed" by a computer.
Java Output
You learned from the previous chapter that you can use the println() method to output values or print text in Java:
Print Numbers
You can also use the println() method to print numbers.
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.
Java Variables
Variables are containers for storing data values.
Print Variables
The println() method is often used to display variables.
Multiple Variables
To declare more than one variable of the same type, you can use a comma-separated list:
Identifiers
All Java variables must be identified with unique names.
Constants (Final)
When you do not want a variable's value to change, use the final keyword.
Real-Life Examples
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
Java Data Types
As explained in the previous chapter, a variable in Java must be a specified data type:
Numbers
Primitive number types are divided into two groups:
Booleans
Very often in programming, you will need a data type that can only have one of two values, like:
Characters
The char data type is used to store a single character. The character must be surrounded by single quotes, like 'A' or 'c':
Real-Life Example
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 Types
Non-primitive data types are called reference types because they refer to objects.
The var Keyword
The var keyword was introduced in Java 10 (released in 2018).
Java Type Casting
Type casting means converting one data type into another. For example, turning an int into a double .
Java Operators
Operators are used to perform operations on variables and values.
Arithmetic
Arithmetic operators are used to perform common mathematical operations.
Assignment
Assignment operators are used to assign values to variables.
Comparison
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.
Logical
As with comparison operators, you can also test for true or false values with logical operators.
Precedence
When a calculation contains more than one operator, Java follows order of operations rules to decide which part to calculate first.
Java Strings
A String variable contains a collection of characters surrounded by double quotes ( "" ):
Concatenation
The + operator can be used between strings to combine them. This is called concatenation:
Numbers and Strings
Warning: WARNING! Java uses the + operator for both addition and concatenation. Numbers are added. Strings are concatenated.
Special Characters
Because strings must be written within quotes, Java will misunderstand this string, and generate an error:
Java Math
The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Java Booleans
Very often in programming, you will need a data type that can only have one of two values, like:
Real-Life Example
Let's use booleans in a real-life example where we want to find out if a person is old enough to vote.
Java If...Else
Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.
else
The else statement lets you run a block of code when the condition in the if statement is false .
else if
Use the else if statement to specify a new condition to test if the first condition is false .
Short Hand If...Else
There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.
Nested If
You can also place an if statement inside another if . This is called a nested if statement.
Logical Operators
You can combine or reverse conditions using logical operators. These work together with if , else , and else if to build more complex decisions.
Real-Life Examples
This example shows how you can use if..else to "open a door" if the user enters the correct code:
Java Switch
Instead of writing many if..else statements, you can use the switch statement.
Java While Loop
Loops can execute a block of code as long as a specified condition is true.
Do/While Loop
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
Real-Life Examples
To demonstrate a practical example of the while loop, we have created a simple "countdown" program:
Java For Loop
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:
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
For-Each Loop
There is also a "for-each" loop, which is used exclusively to loop through elements in an array (or other data structures):
Real-Life Examples
To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
Java Break/Continue
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.
Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
Loop Through an Array
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.
Real-Life Examples
To demonstrate a practical example of using arrays, let's create a program that calculates the average of different ages:
Multidimensional Arrays
A multidimensional array is an array that contains other arrays.
Java Methods
6 of 6 topics publishedJava Methods
A method is a block of code which only runs when it is called.
Java Method Parameters
Information can be passed to methods as a parameter. Parameters act as variables inside the method.
Return Values
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
Java 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 where 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 simpler problems which are ea
Java Classes
21 of 21 topics publishedJava OOP
OOP stands for Object-Oriented Programming.
Java Classes/Objects
Java is an object-oriented programming language.
Java Class Attributes
In the previous chapter, we used the term "variable" for x in the example (as shown below).
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.
Java this Keyword
The this keyword in Java refers to the current object in a method or constructor.
Java Modifiers
By now, you are quite familiar with the public keyword that appears in almost all of our examples:
Non-Access Modifiers
Non-access modifiers do not control visibility (like public or private ), but instead add other features to classes, methods, and attributes.
Java Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
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
Java Inheritance
Java Inheritance (Subclass and Superclass)
Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Java super Keyword
In Java, the super keyword is used to refer to the parent class of a subclass.
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
Java Abstraction
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
Java Interface
Another way to achieve abstraction in Java, is with interfaces.
Java Anonymous
An anonymous class is a class without a name. It is created and used at the same time.
Java Enum
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).
Enum Constructor
An enum can also have a constructor just like a class.
Java User Input
The Scanner class is used to get user input, and it is found in the java.util package.
Java Date
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
Java Errors
5 of 5 topics publishedJava Errors
Even experienced Java developers make mistakes. The key is learning how to spot and fix them!
Java Debugging
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.
Java Exceptions
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
Java Multiple Exceptions
Sometimes, different errors (exceptions) can happen in the same try block. You can handle them with multiple catch blocks.
Java try-with-resources
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
Java File Handling
5 of 5 topics publishedJava Files
File handling is an important part of any application.
Java Create Files
In Java, you can create a new file with the createNewFile() method from the File class.
Java Write Files
If you are just starting with Java, the easiest way to write text to a file is by using the FileWriter class.
Java Read Files
In the previous chapters, you learned how to create and write to a file.
Java Delete Files
To delete a file in Java, use the delete() method:
Java I/O Streams
5 of 5 topics publishedJava I/O Streams
You've already seen how to create, read, and write simple text files.
Java FileInputStream
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
Java FileOutputStream
Earlier, you learned how to write text to files using FileWriter .
Java BufferedReader
BufferedReader and BufferedWriter make reading and writing text files faster.
Java BufferedWriter
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
Java Data Structures
16 of 16 topics publishedJava Data Structures
Data structures are ways to store and organize data so you can use it efficiently.
Java Collections
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
Java List
The List interface is part of the Java Collections Framework and represents an ordered collection of elements.
Java ArrayList
An ArrayList is like a resizable array.
Java LinkedList
In the previous chapter, you learned about the ArrayList class. The LinkedList class is almost identical to the ArrayList :
Java List Sorting
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.
Java Set
The Set interface is part of the Java Collections Framework and is used to store a collection of unique elements.
Java HashSet
A HashSet is a collection of elements where every element is unique.
Java TreeSet
A TreeSet is a collection that stores unique elements in sorted order.
Java LinkedHashSet
A LinkedHashSet is a collection that stores unique elements and remembers the order they were added.
Java Map
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.
Java HashMap
A HashMap stores items in key/value pairs, where each key maps to a specific value.
Java TreeMap
A TreeMap is a collection that stores key/value pairs in sorted order by key.
Java LinkedHashMap
A LinkedHashMap stores keys and values, and keeps them in the same order you put them in.
Java Iterator
An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet .
Java Algorithms
In the previous chapters, you learned how data structures (like ArrayList , HashMap , etc.) are used to store and organize data.
Java Advanced
7 of 7 topics publishedJava Wrapper Classes
Wrapper classes provide a way to use primitive data types ( int , boolean , etc..) as objects.
Java Generics
Generics allow you to write classes, interfaces, and methods that work with different data types, without having to specify the exact type in advance.
Java Annotations
Annotations are special notes you add to your Java code. They start with the @ symbol.
Java RegEx
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
Java Threads
Threads allows a program to operate more efficiently by doing multiple things at the same time.
Java Lambda
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
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?
Java Projects
1 of 1 topics publishedReady to go from notes to a real career?
Join CodingNow 2.0's Java course — live mentorship, hands-on projects, and 100% placement support in Delhi NCR.
Enroll Now — Free Demo AvailableJava Notes – FAQs
What students search before reading Java notes.