1
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の基本

Last updated at Posted at 2025-07-24

学習しながら更新しています
間違いなどあればコメントいただけると幸いです。

説明が足りないところもありますが、後ほど完成形にするのでご容赦ください。

Java

組み込みシステム・業務システム・Web/モバイルアプリケーションに使われている

基本

コメント

複数行のコメント

/*
* 複数行のコメントです
* Javaの練習をしています
* author: taro
*/

一行のコメント

//一行のコメントです

数字を表示してみる

System.out.println(10);
output
10

色々な数値を表示

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println(-8); //負の数
        System.out.println(2.5); //小数点
        System.out.println(1_000_000); //数値が大きいときに_を入れると見やすくなるデバッグでは無視してくれる
        System.out.println(1.2e3); //1.2に10の3条をかけたもの  1.2*1000 = 1200
        System.out.println(1.2e-3); //1.2に10の-3条をかけたもの = 0.0012
    }
}
output
-8
2.5
1000000
1200.0
0.0012

数値の演算

System.out.println(10 + 3); //足し算  13
System.out.println(10 - 3); //引き算  7
System.out.println(10 * 3); //掛け算  30
System.out.println(10 / 3); //割り算  3
System.out.println(10 % 3); //あまり  1
System.out.println(10 + 2 * 3);     16
System.out.println((10 + 2) * 3);   36

変数と定数のルール

変数のルール

命名例
○ price 小文字
○ myName 2つ目以降の単語は大文字から
○ name1 数字が途中や最後にある
× 1name 数字が最初はダメ
× ifやforなどの予約語は使えない

int price = 150;

一度intで変数を宣言したらあとからStringやDoubleの値を代入できない
代入は宣言したintのみ

× price = 40.55 ←double
× price = "値段" ←String
 price = 38 ←int

定数のルール

命名例
○ DATE 大文字
○ MY_NAME 2つ目以降の単語は_で区切る
○ NAME1 数字が途中や最後にある
× 1NAME 数字が最初はダメ

final String MY_NAME = "Taro";

定数はたとえ同じStringでも再代入できない
一度決めたら変更不可

× MY_NAME = "Hanako"

変数

基本

将来的に値が変わるところに変数を使う

int price = 3;
System.out.println(price * 10);  //3 * 10 = 30
System.out.println(price * 20);  //3 * 20 = 60
System.out.println(price * 30);  //3 * 30 = 90

再代入

変数はあとから値を変更(再代入)できる

price = 100;
System.out.println(price * 10);  //100 * 10 = 1000
System.out.println(price * 20);  //100 * 20 = 2000
System.out.println(price * 30);  //100 * 30 = 3000

再代入は以下の書き方もできる

price = price + 50;
price += 50;

定数

finalをつける
定数は、宣言と同時に値の代入もしないといけない。
変数は、宣言だけできてあとから値の代入ができる。
定数の命名は全て大文字
単語の区切りは_をいれる
例:MY_NAME

final double TAX = 0.08; //消費税
System.out.println(100 + 100 * TAX); //金額 + 消費税
System.out.println(200 + 200 * TAX);
System.out.println(300 + 300 * TAX);
output
108.0
216.0
324.0

文字列

基本

System.out.println("Hello, Java!");

""を文字として認識させる

文字列中にダブルクオーテーション("")を入れたい場合
表示したい"の前に\をいれることで"が文字として認識される
(\はoptionキーを押しながら¥を押す(Mac))

System.out.println("Hello, \"Java!\"");
output
Hello, Java!
Hello, "Java"!

改行

\nをいれる

System.out.println("Hello, \nJava!");
output
Hello, 
Java!

文字列と変数を組み合わせる

String name = "Taro";
System.out.println("Hello, " + name);
output
Hello, Taro

変数が多い場合

(String.format("文字列 %s %s", 変数名, 変数名))
String name1 = "Taro";
String name2 = "Hanako";
System.out.println(String.format("Hello. %s, %s", name1, name2));
output
Hello. Taro, Hanako

配列

書き方

[]をつける
配列の順番の番号(インデックス)は、0から始まる
○ 0→1→2...
× 1→2→3...

//書き方1

int[] scores;            //scoresという変数を宣言
scores = new int[3];     //宣言したscoresに3つの整数を代入

scores[0] = 60;          //scores配列の0番目に60を代入
scores[1] = 70;          //scores配列の1番目に70を代入
scores[2] = 80;          //scores配列の2番目に80を代入
 //書き方2
 
int[] scores = new int[3];

scores[0] = 60;
scores[1] = 70;
scores[2] = 80;
//簡単な書き方

int[] scores = {60, 70, 80};

配列の要素を取り出す

int[] scores = {60, 70, 80};
System.out.println(scores[0]);
System.out.println(scores[1]);
System.out.println(scores[2]);
output
60
70
80

配列の要素数を取得

int[] scores = {60, 70, 80};
System.out.println(scores.length);
output
3

複雑な配列

書き方

//つの配列は点数の配列だから1つの配列にまとめたい
int[] firstYearScores = {60, 70, 80};
int[] secondYearScores = {30, 40, 50};


//つにまとめる書き方
int[][] firstAndSecondScores = {{60, 70, 80}, {30, 40, 50}};

//改行して良い
int[][] firstAndSecondScores = {
{60, 70, 80},
{30, 40, 50}
};
それぞれのインデックス
{60, 70, 80} → firstAndSecondScores[0]
{30, 40, 50} → firstAndSecondScores[1]


//firstAndSecondScores[0]...
{60, 70, 80} → [0], [1], [2]

//firstAndSecondScores[1]...
{30, 40, 50} → [0], [1], [2]

要素を取り出す

System.out.println(firstAndSecondScores[1][2]);
output
//firstAndSecondScoresのインデックス[1]の中の[2]だから
50

条件分岐 if

入力できるようにする

矢印のコードをかくとVSCode下のターミナルで入力が可能になる

import java.util.Scanner;  ←←←

public class ・・・ {
    public static void main(String[] args) {
        int 変数名 = new Scanner(System.in).nextInt();  ←←←
    }
}

数値の条件分岐

if (score >= 90) {
    System.out.println("A判定"); //scoreが90以上だったらA判定
} else {
    System.out.println("Aではありません"); //それ以外だったらAではありません
}

実際に入力して確認してみる
確認方法
①実行ボタンを押す
②画面下のターミナルで任意の値を入力
③Enterボタン押す
④入力した値の下に条件分岐の結果が出力される

output
89(←入力した値)
Aではありません

文字列の条件分岐

String weather = new Scanner(System.in).next();

if (weather.equals("晴れ")) {
    System.out.println("Tシャツで出かけよう☀️");
} else if (weather.equals("雨")) {
    System.out.println("傘を忘れずに☂️");
} else if (weather.equals("雪")) {
    System.out.println("暖かいコートを着よう⛄️");
} else {
    System.out.println("天気を取得できません");
}

条件分岐 Switch

String weather = new Scanner(System.in).next();
switch (weather) {
    case "晴れ":
        System.out.println("Tシャツで出かけよう☀️");
        break;
    case "雨":
        System.out.println("傘を忘れずに☂️");
        break;
    case "雪":
        System.out.println("暖かいコートを着よう⛄️");
        break;
    default:
        System.out.println("天気を取得できません");
        break;
}
1
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
1
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?