Java no memo

自分のためのJavaメモ。

Java

コメント

//
/*~*/

文字列

  • 文字は''、文字列は""で囲む。

文字列の連結

System.out.println(100 + 80 + "$"); //180$
System.out.println("$" + 100 + 80); //$10080

文字コード

char c;
c = 'a';  System.out.println(c); // a
c = 97;   System.out.println(c); // a
c = 0x61; System.out.println(c); // a

エスケープシーケンス

\n 改行

数値

  • 8進数 0で始まり0から7までの数字で数値を表す。
  • 16進数 '0x'又は'0X'で始まり0から9までの数字かAからFまでの記号(小文字のaからfでも可)で数値を表す。

浮動小数点数

  • 小数点形式と指数形式

データ型

b oolean、char、byte、short、int、long、float、double
※String型はない。

整数のデータ型

byte、short、int、long
基本はint。long にする場合 Lかlをつける。

浮動小数点数のデータ型

float、double
基本はdouble。floatにする場合はFかfをつける。

除算と剰余の注意点

double d;
d = 3.1 / 0;    // Infinity
d = 3.1 % 0;    // NaN

Stringクラス

String str; 
char c[] = {'a','b','c'}; 
str = new String(c);   // abc
String str = "abc";    // 上記と同じ
String str = "abc"; // "abc"の格納された位置が入る
str = "あいう";     // 上記は破棄され"あいう"の格納された位置が入る
String str1 = "abc";
String str2 = "ab"; str2 += "c";
System.out.println(str1 == str2);           // false
System.out.println( str1.equals(str2) );    // true
char c[] = str.toCharArray();   // char型の配列に入れるとき
String str = String.valueOf(c); // char型の配列から文字列を作るとき
for (char ch:c){ 
    System.out.println(ch); 
}  // ループ

インクリメント、デクリメントの前置と後置

int n = 0;
System.out.println(n++);    // 0 この行の処理の後、追加
System.out.println(++n);    // 2 追加後、この行の処理

for文、拡張for文(for-each文)

for (int i = 1; i <= 10; i++){
    
}
int sum = 0, count = 0;
for (; count < 2;){
  sum += 2;
  count++;
}
for (int i = 0 ; ; ){
    
}
System.out.println(i);  // iは呼び出せない
int x,y;
for(x = 0, y = 10; x < 10; x++, y--){
    
}
String data[] = {"東京","大阪","名古屋"};
for(String city:data){
    System.out.println(city);
}

while文、do while文

int i = 0;
while(i < 2){
    i++;
}
int i = 0;
do{
    i++;
}while(i < 2);

上記は同じだが、条件がいきなりfalseになる場合、do~while文は1回は処理を実行する。

条件分岐

int n = 1;
if (n = 0) {
    
}else if (n = 1){
    
}else{
    
}
switch(i){
    case 0:
        break;
    case 1:
        break;
    case 2:
    case 3:
        break;
    default:
        break;
}

ラベル付きbreak文

outside: for (int i = 1; i < 5; i++){
  for (int j = 1; j < 5; j++){
    if (i * j > 10){
      break outside;
    }
  }
}
outside:{
    for (int i = 1; i < 5; i++){
        for (int j = 1; j < 5; j++){
            if (i * j > 10){
            break outside;
            }
        }
    }
}

ラベル付きcontinue文

int num = 0;
outside: while(num < 31){
    num++;
    if(num % 3 == 0){
        continue outside;
    }
    System.out.println(num);
}

Point!! 配列の要素数に注意。

配列 ※要素数に注意!!

int a[];
a = new int[5]; //5こ

String a[] = new String[5];

int[] a;
a = new int[5];

String[] a = new String[5];

String city[] = new String[3];
city[0] = "東京";
city[1] = "大阪";
city[2] = "名古屋";
for (int i = 0; i < city.length; i++){
    System.out.println(city[i]);
}

String a[] = {"東京", "大阪", "名古屋"};
String b[];
b = a;  // 配列は参照型、aとbには値の位置が入る。
System.out.println(a[0] + "、" + a[1]);   //東京、大阪
System.out.println(b[0] + "、" + b[1]);   //東京、大阪

String a[] = {"東京", "大阪"};
String b[];
b = a;
b[1] = "名古屋";
System.out.println(a[0] + "、" + a[1]);   //東京、名古屋
System.out.println(b[0] + "、" + b[1]);   //東京、名古屋

多次元配列

String str[][] = new String[2][2];  // String[][] str = new String[2][2]; もOK
str[0][0] = "Tokyo";
str[0][1] = "東京";
str[1][0] = "Osaka";
str[1][1] = "大阪";
for (int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
    System.out.println(str[i][j]);
    }
}

String str[][];
str = new String[2][];
str[0] = new String[2];
str[1] = new String[2];

String str[][] = { {"Tokyo","東京"}, {"Osaka","大阪"}, {"Nagoya","名古屋"} };
System.out.println(str.length);     // 3
System.out.println(str[0].length);  //2

クラス、フィールド、メソッド

class Counter{
    int num;
    
    void init(){
        num = 0;
    }
    
    void count(){
        num = num + 1;
    }
    void printNum(){
         System.out.println(num);
    }
}

mainメソッドは静的メソッド(static) クラスからモノを作らなくても呼び出すことができる特殊なメソッド

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

戻り値がないメソッド
private static void sayname(String _name){
    System.out.println(_name);
}

戻り値があるメソッド
private static String getName(String _name){
    return _name + "さん";
}

配列を返す
private static int[] test(){
    int ary[] = new int[2];
    ary[0] = 0; ary[1] = 1;
    return ary;
}
int newary[]; newary = test();

メソッドオーバーロード

private static int plus(int i1, int i2){
    return i1 + i2;
}
private static Double plus(Double d1, Double d2){
    return d1 + d2;
}

引数や戻り値の型や数が違えば、同じメソッド名がつけられる。

可変引数

private static int plus(int ...nums){
    int ret = 0;
    for(int i = 0; i < nums.length; i++){
        ret += nums[i];
    }
    return ret;
}
plus(1,2);
plus(1,2,3);