Java
Like for C++, I’ve already talked about why we might want to learn Java earlier. So this lesson will just be another crash course.
This Lesson Will Cover
- 🌍 Hello World
- 📦 Variables and Data Types
- 🔄 Control Flow
- 🏛️ Classes and Objects
- 📚 Using External Libraries
Hello World
The classic starting point:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Here:
public class Maindefines the class.main(String[] args)is the entry point of the program.System.out.printlnoutputs text to the console.
To run this program:
- Save the code in a file named
Main.java - Compile it:
javac Main.java - Run it:
java Main
Variables and Data Types
Java is statically typed, meaning we must specify the data type for variables:
int num = 42; // Integer
double pi = 3.14; // Floating-point number
char letter = 'A'; // Character
boolean isAI = true; // Boolean
String name = "AI"; // String
Java also supports dynamic collections using libraries like ArrayList:
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
Control Flow
Loops and conditionals:
for (int i = 0; i < 5; i++) {
System.out.println("Iteration: " + i);
}
int num = 42;
if (num > 40) {
System.out.println("Number is greater than 40");
} else {
System.out.println("Number is 40 or less");
}
Classes and Objects
Classes are at the core of Java programming:
public class Dataset {
String name;
int size;
public void load() {
System.out.println("Loading dataset: " + name);
}
}
public class Main {
public static void main(String[] args) {
Dataset dataset = new Dataset();
dataset.name = "MNIST";
dataset.size = 60000;
dataset.load();
}
}
Using External Libraries (Apache Commons Math Example)
The Apache Commons Math library is used for statistical calculations, e.g.
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
public class StatisticsExample {
public static void main(String[] args) {
// Create a DescriptiveStatistics instance
DescriptiveStatistics stats = new DescriptiveStatistics();
// Add some values
double[] values = {1.0, 2.0, 3.0, 4.0, 5.0};
for (double v : values) {
stats.addValue(v);
}
// Print some statistics
System.out.println("Mean: " + stats.getMean());
System.out.println("Standard Deviation: " + stats.getStandardDeviation());
System.out.println("Median: " + stats.getPercentile(50));
}
}
To run this code:
-
Option 1: Using Maven (recommended)
- Create a
pom.xmlfile:
<project> <dependencies> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.6.1</version> </dependency> </dependencies> </project>- Run:
mvn compile - Run:
mvn exec:java -Dexec.mainClass="StatisticsExample"
- Create a
-
Option 2: Manual Download
- Download the JAR file from Maven Central Repository
- Add it to our classpath when compiling and running:
javac -cp commons-math3-3.6.1.jar StatisticsExample.java java -cp ".:commons-math3-3.6.1.jar" StatisticsExample- On Windows, use
;instead of:in the classpath.
I ran this using Option 2 and got the following output:
Mean: 3.0
Standard Deviation: 1.5811388300841898
Median: 3.0
Conclusion
Getting the external library code sample above was a lot more difficult than I’d like to admit. I had originally tried to do an LLM integration, but gave up on that.
I did, however, include an example of a Java LLM integration in the “AI Deployment > LLMs > API Integrations” lesson. Feel free to hop there now and check it out while you’re still in the Java zone. ☕
Key Takeaways
🖥️ Java is a statically typed language, requiring explicit data type definitions.
🔄 Control flow in Java uses loops (
for,while, conditional statements, etc.).🏛️ Object-oriented programming (OOP) is at the core of Java.
📦 External libraries, such as Apache Commons Math, can enhance Java’s functionality.
🌍 Java is widely used for enterprise applications, Android development, and more.
Next: Python
That’s it for low-level languages! Time to deep dive into something I find a lot more fun.
In fact, it’s one of my favorite things in the world: Python!
Comments (0)
No comments yet.