Java Tutorial
Introduction
Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation.
Setting Up the Environment
Before you start writing Java programs, you need to set up your Java environment.
Install JDK
Download and install the Java Development Kit (JDK) from Oracle's official website.
Set up the Path
After installing the JDK, set up the path variable to point to the JDK bin directory.
Example for Windows:
C:\> set PATH=C:\Program Files\Java\jdk1.8.0_221\bin;%PATH%
Your First Java Program
Let's create a simple Java program to print "Hello, World!" to the console.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Compiling the Program
Save the above code in a file named HelloWorld.java
and compile it using the following command:
javac HelloWorld.java
Running the Program
Run the compiled Java program using the following command:
java HelloWorld
Hello, World!
Basic Syntax
Java syntax is the set of rules defining how a Java program is written and interpreted.
Comments
Comments can be used to explain Java code, and to make it more readable. Comments are ignored by the compiler.
// This is a single-line comment /* This is a multi-line comment that spans multiple lines */
Data Types
Java is a strongly-typed language. Every variable must have a declared type.
int
- Integer data typefloat
- Floating point data typechar
- Character data typeboolean
- Boolean data type
Variables
Variables are containers for storing data values.
int myNum = 5; float myFloat = 5.99f; char myLetter = 'D'; boolean myBool = true;
Control Structures
Control structures are the basic blocks that manage the flow of your code.
If-Else
The if-else statement is used to test a condition.
int x = 10; if (x > 5) { System.out.println("x is greater than 5"); } else { System.out.println("x is not greater than 5"); }
For Loop
The for loop is used to iterate a part of the program several times.
for (int i = 0; i < 5; i++) { System.out.println(i); }
0
1
2
3
4
While Loop
The while loop is used to execute a block of code as long as a specified condition is true.
int i = 0; while (i < 5) { System.out.println(i); i++; }
0
1
2
3
4
Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which are data structures that contain data and methods.
Classes and Objects
A class is a blueprint for creating objects. Objects are instances of classes.
class Car { int year; String model; void display() { System.out.println("Year: " + year + ", Model: " + model); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.year = 2020; myCar.model = "Tesla Model S"; myCar.display(); } }
Year: 2020, Model: Tesla Model S
Edge Computing with Java
Edge computing is a distributed computing paradigm that brings computation and data storage closer to the sources of data. Java is widely used in edge computing for its portability and performance.
Java and Edge Devices
Java can be used to develop applications for edge devices such as Raspberry Pi, smart sensors, and IoT devices.
Example: Reading Sensor Data
Below is an example of a simple Java program that reads data from a temperature sensor and prints it to the console.
import java.util.Random; public class TemperatureSensor { public static void main(String[] args) { Random rand = new Random(); int temperature = rand.nextInt(100); // Simulate reading from a sensor System.out.println("Current Temperature: " + temperature + "°C"); } }
Current Temperature: 23°C