Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with Java Fundamentals

Tech May 16 2

0. Comments

// Single-line comment
/* Multi-line comment */
/** Documentation comment for classes and methods */

1. Basic Output

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

2. Reading Input from Console

import java.util.Scanner;

public class ReadInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double value = scanner.nextDouble();
        System.out.println("You entered: " + value);
    }
}

3. Identifiers

Identifiers in Java are sequences composed of letters, digits, underscores (_), and dollar signs ($). Rules include:

  • Must start with a letter, underscore, or dollar sign—not a digit.
  • Cannot be Java keywords (e.g., class, public).
  • Cannot be the literals true, false, or null.
  • Can be of any length.

Java is case-sensitive. Prefer descriptive names over abbreviations. Avoid using single characters except in auto-generated code.

4. Variable Declaration

// Declaration only
int count;
double price;
long total, balance;

// Declaration with initialization
int score = 100;
double width = 5.5, height = 3.2;

5. Assignment Statements

int y = 1;
double radius = 1.0;
int result = 5 * (3 / 2);  // Integer division: 3/2 = 1
result = y + 1;
result = result + 1;       // Increment by 1

6. Constants

final double PI = 3.14159;

7. Naming Conventions

Use clear, descriptive names for variables, constants, classes, and methods:

  • Variables and methods: Use camelCase—start with lowercase, capitalize subsequent words (e.g., studentCount, calculateArea).
  • Classes: Use PascalCase—capitalize the first lettter of every word (e.g., CircleCalculator, FileHandler).
  • Constants: Use uppercase with underscores between words (e.g., MAX_SIZE, DEFAULT_TIMEOUT).

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

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