0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java学習メモ② Javaの基本ルール

0
Last updated at Posted at 2026-05-19

Java学習メモ

最初必ず書くコードは?

下記は、Javaのプログラムを動かすためには必ず必要な記述

public class Main {
    public static void main(String[] args) {
      System.out.println("Hello World");
    }
}
  • mainメソッドの{}中にプログラムを書く。
  • 文の末尾に;を書く(忘れるとコンパイルエラー)
// コメント
  • コメントは//の後ろに書く

System.out.println(printlnは、print lineの略)

下記のコードで"Hello World"が出力される。

public class Main {
    public static void main(String[] args) {
      System.out.println("Hello World");
    }
}
  • ()の中の文字列をダブルクォート"で囲む

変数の定義方法

  • Javaでは、変数を宣言するときは必ず型を指定する。
  • 変数名を付ける際は、キャメルケースを使う。
  • キャメルケースとは、小文字から始まり単語の区切りを大文字にする。
     例:userName
public class Main {
    public static void main(String[] args) {
        // 変数に代入
        String userName = "マロン";

        // 表示
        System.out.println(userName);
    }
}

Javaのデータ型

String = 文字列 ※これは参照型なので大文字で書く

プリミティブ型 用途・特徴 例
byte 小さい整数 byte b = 10;
short やや小さい整数 short s = 1000;
int 普通の整数(よく使う) int n = 12345;
long 大きい整数 long l = 100000L;
float 小数(精度低め) float f = 3.14f;
double 小数(よく使う) double d = 3.14159;
char 1文字 char c = 'A';
boolean true / false boolean flag = true;

メソッドの定義1

public class Main {
    public static void greet() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
      // メソッドの呼び出し
        greet();
    }
}

メソッドの定義2

public class Main {
    public static void weatherInfo() {
        System.out.println("今日の天気は晴れです");
        System.out.println("気温は25度です");
        System.out.println("洗濯日和ですね");
    }

    public static void main(String[] args) {
        weatherInfo();
    }
}

注意

mainメソッドの中にメソッドを書かない。


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?