Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Introduction to Java Programming Language

Notes 2

Definition of a Program

A program is a structured sequence of instructions designed for a computer to perform specific operations or solve problems.

Overview

Java is a programming language with features common to many languages, specifically engineered for distributed environments like the internet. It shares syntactic similarities with C++ but is more user-friendly and fully embraces object-oriented principles. Java applications can execute on standalone machines or be distributed across network servers and clients. Additionally, Java supports small application modules or applets for web pages, enabling interactive user experiences. Introduced by Sun Microsystems in 1995, Java quickly transformed interactive web applications. Most internet browsers include a Java Virtual Machine, and Java compilers are integrated into nearly all operating systems.

Origins of Java

In 1990, Sun Microsystems aimed to develop a universal control system for smart homes. Due to limitations in C++, such as lack of garbage collection, portability, distributed computing, and multithreading, the team created a new object-oriented language named Oak. By 1994, the FirstPerson subsidiary developed WebRunner, a web browser using Oak, which received positive feedback. After renaming to Java due to trademark issues, Sun released it in 1995 as an open-source, freely available language, gaining widespread popularity.

Application Domains

  • Enterprise Applications: Complex software systems for large organizations, including websites in finance, telecommunications, transportation, and e-commerce.
  • Android Platform Applications: Android apps are primarily written in Java, with development proficiency heavily reliant on core Java skills.
  • Mobile and Embedded Applications: Used in consumer and embedded devices like set-top boxes, in-car entertainment systems, automotive communication devices, and point-of-sale scanners.

Technology Platforms

  • Java SE (Standard Edition): Supports desktop applications (e.g., Windows apps) with complete core APIs, formerly known as J2SE.
  • Java EE (Enterprise Edition): Provides solutions for enterprise application development, including technologies like Servlet and JSP for web apps, formerly J2EE.
  • Java ME (Micro Edition): Enables Java programs on mobile devices (e.g., phones, PDAs) with streamlined APIs and mobile-specific support, formerly J2ME.

Key Characteristics

  • Object-oriented programming (OOP).
  • Robustness ensured by strong typing, exception handling, and automatic garbage collection.
  • Platform independence: Compiled .class files run across multiple systems.
  • Interpreted nature: Unlike compiled languages like C/C++, Java code requires an interpreter (JVM) for execution.

Core Mechanism: Java Virtual Machine (JVM)

The JVM is a virtual computer with instruction sets and memory management, included in the JDK. It abstracts platform differences, enabling "write once, run anywhere" functionality through platform-specific implementations.

JDK and JRE

  • JDK (Java Development Kit): Includes JRE plus development tools (e.g., javac, java). Used by developers; installing JDK eliminates the need for separate JRE installation.
  • JRE (Java Runtime Environment): Comprises JVM and core libraries. Sufficient for running compiled Java programs.
  • Relationship: JDK = JRE + development tools; JRE = JVM + core libraries.

Getting Started with Java

  1. Write code in a file named Hello.java:
class Greeting {
    public static void main(String[] parameters) {
        System.out.println("Hello World!");
    }
}
  1. Compile with javac Greeting.java to produce Greeting.class.
  2. Execute with java Greeting to run the bytecode.
  • Compilation: Translates source code to JVM-readable bytecode.
  • Execution: Loads .class files into the JVM to processing. Recompile after modifications.

Guidelines for Java Programs

  • Source files use .java extension and are class-based.
  • Entry point: public static void main(String[] args).
  • Case-sensitive; statements end with semicolons; braces must pair.
  • Maximum one public class per file; filename matches public class name if present. Main method can reside in non-public classes.

Escape Characters

Escape Sequence Description
\t Tab
\n Newline
\\ Backslash
\" Double quote
\' Single quote
\r Carriage return

Comments

Comments enhance code readability and are ignored during execution.

  • Single-line: // comment
  • Multi-line: /* comment */
  • Documentation: /** comment */ (for javadoc). Avoid nesting multi-line comments.

Coding Standards

  • Use javadoc for class and method documentation.
  • Non-javadoc comments aid maintainers.
  • Indent with Tab; shift with Shift+Tab.
  • Space operators (e.g., 1 + 1 - 2).
  • Encode source files in UTF-8.
  • Limit line width to 80 characters.
  • Adopt either next-line or end-of-line style for braces.
  • Understand relative vs. absolute paths for file handling.

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.