忍者ブログ
まにょのITメモ
[7]  [8]  [9]  [10]  [11]  [12]  [13]  [14]  [15]  [16
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

インナークラスの制限事項

・外部クラスと同じ名前はつけられない

・staticなフィールドを持てない
PR
②ローカルインナークラス

public class Outer {

    public void method() { 

        class Inner2{ 

        }
    }
}


・Outerクラスのmethodメソッドの中にInner2クラスがある。

・newの仕方
    Inner2 in = new Inner2();

・使用可能な修飾子
    abstract  final

①インナークラス

public class Outer {

    public class Inner1 { 

    }
}


・Outerクラスの中にInner1クラスがある。

・newの仕方
    Outer.Inner1 in = new Outer().new Inner1();

・使用可能な修飾子
    public  protected  default  private  abstract  final
 

public class Operando {

 public static void main(String[] args) {
  
  int i = 1;
  if((i == 0)&&(i++ == 1)) {
   System.out.println("true : " + i);
  }else {
   System.out.println("false : " + i);
  }
  
  int n = 1;
  if((n == 0)&(n++ == 1)) {
   System.out.println("true : " + n);
  }else {
   System.out.println("false : " + n);
  }
  
  int t = 1;
  if((t == 1)||(t++ == 1)) {
   System.out.println("true : " + t);
  }else {
   System.out.println("false : " + t);
  }
  
  int x = 1;
  if((x == 1)|(x++ == 1)) {
   System.out.println("true : " + x);
  }else {
   System.out.println("false : " + x);
  }
 }
}


実行結果

false : 1
false : 2
true : 1
true : 2

&& や || は、まず左オペランドの比較を見ます。
左オペランドだけで結果を返せる場合は右オペランドを見ません。
& や | は左オペランドを見て、その後、右オペランドも必ず見ます。
右オペランドにインクリメントなどがある場合は気をつけましょう。

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalSample {

    public static void main(String[] args) {
  
        BigDecimal bd = new BigDecimal("0.123456");
  
        System.out.println("切り上げ : " + bd.setScale(4, RoundingMode.CEILING));
        System.out.println("切り捨て : " + bd.setScale(4, RoundingMode.FLOOR));
        System.out.println("四捨五入 : " + bd.setScale(4, RoundingMode.HALF_UP));

    }

}


これを実行すると

切り上げ : 0.1235
切り捨て : 0.1234
四捨五入 : 0.1235

となる。


計算したい数値(BigDecimal型).setScale(表示したい小数点以下の位 , 数字の丸め方)

忍者ブログ * [PR]