DEV Community

Cover image for Java Foundation
Gustavo Perez
Gustavo Perez

Posted on

Java Foundation

What is Java

Java is a high-level, object-oriented, and platform-independent programming language. It is widely used for building web applications, mobile applications, enterprise software, and more. Its "write once, run anywhere" capability is powered by the Java Virtual Machine (JVM).


Key Characteristics of Java

  • Platform Independence: Code written in Java can run on any platform with a JVM.
  • Object-Oriented: It uses concepts like classes, inheritance, and polymorphism to structure programs.
  • Robustness: Strong memory management and exception handling make Java less prone to crashes.
  • Security: Features like a security manager and bytecode verification ensure secure execution.
  • Multithreading: Java supports concurrent programming to run multiple tasks simultaneously.
  • Rich Libraries: Extensive standard libraries for networking, file handling, and more.

JVM (Java Virtual Machine)

The JVM is a runtime environment that executes Java bytecode, making Java platform-independent. It interprets or compiles bytecode into machine code specific to the host system. It also handles memory management, garbage collection, and security checks.


JDK (Java Development Kit)

The JDK is a software development environment that provides tools for developing, debugging, and running Java applications. It includes the Java Compiler (javac), libraries, and the JRE (Java Runtime Environment).


Java Compiler (javac) and How It Works

The javac compiler translates Java source code (files with .java extension) into bytecode (files with .class extension). Bytecode is an intermediate, platform-independent representation of the program, which is then executed by the JVM.


JRE (Java Runtime Environment)

The JRE provides the libraries, JVM, and other components necessary to run Java applications. It does not include development tools like the compiler. It is intended solely for end-users who want to execute Java programs.

Variables

In Java, a variable is a container for storing data that can be used and manipulated within a program. Each variable has a type that defines the kind of data it can hold.

Example:

int age = 25; // An integer variable
String name = "John"; // A string variable
Enter fullscreen mode Exit fullscreen mode

Data Types

Data types define the type of data that a variable can store. Java is statically typed, so each variable must be declared with a data type.

Common Data Types:

Primitive: int, double, boolean, char, etc.
Non-Primitive: String, arrays, objects, etc.

Example:

int number = 10; // Integer
double price = 19.99; // Decimal number
boolean isAvailable = true; // Boolean
char grade = 'A'; // Character
Enter fullscreen mode Exit fullscreen mode

Concatenation

Concatenation in Java is the process of joining two or more strings or combining strings with other data types. The + operator is commonly used for this purpose.

Example:

String firstName = "Jane";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // "Jane Doe"

int age = 30;
String message = "Age: " + age; // "Age: 30"
Enter fullscreen mode Exit fullscreen mode

Constants

Constants are variables whose values cannot be changed once assigned. In Java, the final keyword is used to declare constants.

Example:

final double PI = 3.14159;
final String WELCOME_MESSAGE = "Welcome to Java Programming";

// Uncommenting the line below will cause an error
// PI = 3.14;
Enter fullscreen mode Exit fullscreen mode

Summary Example

public class Main {
    public static void main(String[] args) {
        // Variables and Data Types
        int age = 25;
        double height = 5.9;
        String name = "Alice";
        boolean isStudent = true;

        // Concatenation
        String message = name + " is " + age + " years old.";
        System.out.println(message); // Output: Alice is 25 years old.

        // Constants
        final double GRAVITY = 9.8;
        System.out.println("Gravity: " + GRAVITY); // Output: Gravity: 9.8
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)