I’ve been reviewing Java for some job applications recently and decided to write about some interesting fundamentals while refreshing my knowledge. This post covers the basics of Java compilation, variable declaration, and how strings work in Java. I’ll dive deeper into specifics in future posts.

Java: A Compiled Language Link to heading

Java is a compiled language, which means that for standalone programs, you need to compile your code before running it. Unlike interpreted languages where you can run the source code directly, Java requires a two-step process.

Compilation and Execution Link to heading

Let’s say you create a simple Java program in a file called ItsADogsLife.java:

public class ItsADogsLife {
    public static void main(String[] args) {
        System.out.println("Woof! It's a dog's life!");
    }
}

To run this program:

  1. Compile the source code using javac:

    javac ItsADogsLife.java
    

    This creates a bytecode file called ItsADogsLife.class.

  2. Run the compiled bytecode using java:

    java ItsADogsLife
    

You’ll see the output: Woof! It's a dog's life!

Build Tools in Larger Projects Link to heading

In real-world Java projects, you typically don’t run javac and java manually. Build tools like Gradle or Maven handle the compilation, dependency management, and execution for you. For example, with Gradle, you might just run:

./gradlew build
./gradlew run

This abstraction makes development much smoother, especially for complex applications.

Comparison to Interpreted Languages Link to heading

This is different from interpreted languages like Python, JavaScript, or Ruby, where you can run the source code directly without a separate compilation step. For instance:

  • Python: python script.py
  • JavaScript (Node.js): node script.js
  • Ruby: ruby script.rb

Java’s compilation step allows for better performance optimization and early error detection, but it adds an extra step in the development process.

Declaring Variables in Java Link to heading

Java requires explicit type declaration when creating variables. This is different from dynamically-typed languages that infer types at runtime.

Static Typing in Java Link to heading

In Java, you must specify the variable type upfront:

int age = 25;
boolean isStudent = true;
double salary = 50000.50;
char grade = 'A';
String name = "Sarah";

Comparison to Dynamic Languages Link to heading

Languages like Python, JavaScript, and Ruby allow more flexible variable declaration:

Python:

age = 25          # int
is_student = True # bool
salary = 50000.50 # float
grade = 'A'       # str
name = "Sarah"    # str

JavaScript:

let age = 25;
const isStudent = true;
let salary = 50000.50;
let grade = 'A';
let name = "Sarah";

Ruby:

age = 25
is_student = true
salary = 50000.50
grade = 'A'
name = "Sarah"

Primitive Types vs. Reference Types Link to heading

Java has primitive types (int, boolean, double, char) that store values directly, and reference types (like String) that store references to objects. This distinction is important for understanding memory management and performance.

Java and the String Class Link to heading

One notable difference between Java and many other languages is how strings are handled. In Java, strings are not primitive types—they are objects of the String class.

Strings as Objects Link to heading

String greeting = "Hello, World!";
String name = new String("Sarah");

This is different from languages where strings are primitive types, such as:

  • C: Character arrays
  • Python: Built-in string type (but still an object)
  • JavaScript: Primitive string type

String Immutability Link to heading

Java strings are immutable, meaning once created, they cannot be changed. Operations that appear to modify strings actually create new string objects:

String original = "Hello";
String modified = original + " World!";  // Creates a new string

This immutability has implications for performance and memory usage, which is why Java provides the StringBuilder class for efficient string concatenation in loops.

String Methods Link to heading

Since strings are objects, they come with many useful methods:

String text = "Hello, Java!";
System.out.println(text.length());        // 12
System.out.println(text.toUpperCase());   // "HELLO, JAVA!"
System.out.println(text.substring(7));    // "Java!"
System.out.println(text.contains("Java")); // true

Conclusion Link to heading

Java’s compiled nature, static typing, and object-oriented approach to strings make it a powerful but sometimes verbose language. Understanding these fundamentals helps explain why Java is so widely used in enterprise applications, Android development, and large-scale systems.

While it requires more upfront planning than dynamic languages, Java’s explicitness leads to more maintainable and performant code. In future posts, I’ll dive deeper into Java’s object-oriented features, exception handling, and modern Java developments.

What Java fundamentals would you like me to cover next?