メインコンテンツにスキップ
eLearner.app
モジュール 1 · レッスン 1 / 2コース内の 1/14~10 min
モジュールのレッスン (1/2)

変数と基本構造

Java is a strongly typed, object-oriented programming language. Every line of code in Java must reside inside a class, and program execution always starts from a special entry point called the main method.

The Basic Structure

A minimal Java program looks like this:

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

Let's analyze the key components:

  • public class Main: In Java, all code is enclosed in a class. The name of the public class must exactly match the filename (e.g., Main.java).
  • public static void main(String[] args): This is the starting point (entry point) of any Java application. When we run the program, the JVM (Java Virtual Machine) looks for this method and executes the code inside it.
  • System.out.println(...): It is the standard statement to print text to the screen, followed by a new line.

Primitive Variables

In Java, before using a variable, we must declare its type. The fundamental primitive types are:

  1. int: Integers (e.g., 42, -10).
  2. double: Double-precision floating-point numbers (e.g., 19.99, -0.5).
  3. boolean: Logical values, which can only be true or false.
  4. char: A single character, enclosed in single quotes (e.g., 'A', '7').

The declaration syntax is:

Code
type variableName = value;

Practical example:

Code
int score = 100;
double temperature = 36.5;
boolean isGameOver = false;
char grade = 'A';

Strings

Although not a primitive type, strings (String) in Java are used to handle sequences of characters. They are declared with a capital S, and the value must be enclosed in double quotes:

Code
String name = "Alice";
System.out.println("Hello, " + name);

Comments in Java

To make code readable and documented, Java supports two types of comments:

  1. Single-line comments: start with // and continue to the end of the line.
  2. Multi-line comments: enclosed between /* and */.
Code
// This is a single-line comment
int age = 30;

/* This is a
   multi-line comment */
String role = "Developer";

Naming Conventions and Case Sensitivity

Java is strictly case-sensitive (it distinguishes uppercase from lowercase letters). A variable named score is different from Score. By convention:

  • Variables and methods use camelCase notation (e.g., discountedPrice, calculateTax).
  • Classes use PascalCase notation (e.g., InvoiceManager, Main).

Printing to the Screen: print vs println

The System.out system class offers two main methods to display output:

  • System.out.print(...): prints the value without adding a new line. Subsequent output will start on the same line.
  • System.out.println(...): prints the value and moves the cursor to the next line (print line).

Try it yourself

運動#java.m1.l1.e1
試行回数: 0読み込み中…

Declare an integer variable named age with an initial value of 25. Then, print it to the screen using System.out.println.

エディターを読み込み中…
ヒントを表示

Write `int age = 25;` to declare the variable, then use `System.out.println(age);` on the next line.

3 回の試行後に解決策が利用可能になります

運動#java.m1.l1.e2
試行回数: 0読み込み中…

Declare a double variable named price with an initial value of 19.99 and a boolean variable named isAvailable set to true. Print both variables on separate lines.

エディターを読み込み中…
ヒントを表示

Define `double price = 19.99;` and `boolean isAvailable = true;`, then print them one by one using `System.out.println`.

3 回の試行後に解決策が利用可能になります

運動#java.m1.l1.e3
試行回数: 0読み込み中…

Declare a char variable named grade set to 'A' and a String variable named student set to "John". Print to the screen the text: student + " : " + grade.

エディターを読み込み中…
ヒントを表示

Declare char grade = 'A'; and String student = "John";, then print them concatenated using System.out.println(student + " : " + grade);.

3 回の試行後に解決策が利用可能になります