Java Examples
Java Syntax
Syntax ExplainedJava Comments
Single-line comment before a line of code
Single-line comment at the end of a line of code
Multi-line comment
Comments Explained
Java Variables
Create a string variable
Create an integer variable
Create a variable without assigning the value, and assign the value later
Overwrite an existing variable value
Create a final variable (unchangeable and read-only)
Combine text and a variable on display
Add a variable to another variable
Declare many variables of the same type with a comma-separated list
Variables Explained
Java Data Types
A demonstration of different data types in Java
Create a byte type
Create a short type
Create an int type
Create a long type
Create a float type
Create a double type
Create a boolean type
Create a char type
Create a String type
Data Types Explained
Java Type Casting
Type Casting ExplainedJava Operators
Addition operator
Subtraction operator
Multiplication operator
Division operator
Modulus operator
Increment operator
Decrement operator
Assignment operator
Comparison operator
Logical operator
Operators Explained
Java Strings
Create a string
Quotes inside a string
Find the length of a string
Using methods to convert strings to uppercase and lowercase
Finding a string in a string
String concatenation
If you add a number and a string, the result will be a string concatenation
Strings Explained
Java Math
Math.max(x,y) - return the highest value of x and y
Math.min(x,y) - return the lowest value of x and y
Math.sqrt(x) - return the square root of x
Math.abs(x) - return the absolute (positive) value of x
Math.random() - return a random number between 0 and 1
Math Explained
Java Booleans
Create a boolean type
Find out if an expression is true or false
Use the "equal to" operator to evaluate a boolean expression
Booleans Explained
Java If...Else (Conditions)
If...Else ExplainedJava Switch
Switch ExplainedJava Loops
Loops ExplainedJava Arrays
Create and access an array
Change an array element
Find the length of an array
Access an array
Loop through an array
Loop through an array with for-each
Multidimensional array
Loop through a multidimensional array
Arrays Explained
Java Methods
Create and call a method
Call a method multiple times
Method with parameters
Method with multiple parameters
Return value
A method with if...else
Method overloading
Recursion
Recursive function with a halting condition
Methods Explained
Java Classes and Objects
Create a class and an object of a class
Create multiple objects of a class
Accessing class attributes (variables)
Modify attributes
Override existing attribute values
Multiple attributes
Create a class method
Method with parameters
Access class methods with an object
Create a class constructor
Constructor with parameters
Classes and Objects Explained
Java Modifiers
Public class
Default class
Public attributes
Private attributes
Default attributes
Protected attributes
Final class
Abstract class with abstract methods
Modifiers Explained
Java Encapsulation
Encapsulation ExplainedJava Packages
Packages ExplainedJava Inheritance
Inheritance ExplainedJava Inner Classes
Inner class
Private inner class (error when trying to access it from an outside class)
Static inner class
Access outer class from inner class
Inner Classes Explained
Java Abstraction & Interfaces
Interfaces ExplainedJava Enums
Create an enum with a group of constants
An enum inside a class
An enum in a switch statement
Loop through an enum
Enums Explained
Java User Input
User Input ExplainedJava Dates
Dates ExplainedJava ArrayList
Create an ArrayList
Access an item in an ArrayList
Remove an item from an ArrayList
Remove all items from an ArrayList
Get the size of an ArrayList
Loop through an ArrayList
Loop through an ArrayList with for-each
Create an ArrayList that should store numbers (integers)
ArrayList Explained
Java HashMap
Create a HashMap
Access an item in a HashMap
Remove an item from a HashMap
Remove all items from a HashMap
Get the size of a HashMap
Loop through a HashMap
Create a HashMap that should store String keys and Integer values
HashMap Explained
Java HashSet
Create a HashSet and add items to it
Check if an item exists in a HashSet
Remove an item from a HashSet
Remove all items from a HashSet
Get the size of a HashSet
Loop through a HashSet
Create a HashSet that should store Integer values
HashSet Explained
Java Iterator
Iterator ExplainedJava Wrapper Classes
Create an ArrayList that should store numbers (integers)
Create wrapper objects
Using wrapper methods
Convert wrapper objects to strings
Wrapper Classes Explained
Java Exceptions (Try...Catch)
Exceptions ExplainedJava Regular Expressions
Regular Expressions ExplainedJava Threads
Running a thread by extending the thread class
Running a thread by implementing the Runnable interface
Concurrency problems
Avoid concurrency problems
Threads Explained