java判斷integer
❶ java中怎麼判斷變數是不是int類
java里沒有typerof ,要用instanceof
基啟余礎類型明旁首不行,要用對象
Integer i = 0;
if (i instanceof Integer) {
System.out.println("激數haha");
}
String str = "abc";
if (str instanceof Object) {
System.out.println("haha");
}
❷ java 裡面怎麼判斷兩個int型的數值相等
如果是int類型,判斷相等的話直接使用 "=="來判斷,例如:
int i = 10;
int j = 10;
System.out.print(i == j);
如果是Integer類型,則可以使用equals方法進行相等比較。
int與Integer的基本使用對比
(1)Integer是int的包裝類;int是基本數據類型;
(2)Integer變數必須實例化後才能使用;int變數不需要;
(3)Integer實際是對象的引用,指向此new的Integer對象;int是直接存儲數據值 ;
(4)Integer的默認值是null;int的默認值是0。
❸ java里怎麼判斷一個變數,是否是int型,或者是否是double型
你可以用一個字元串來保存你的輸入,然後你可以將輸入利用String類庫中的函數進行轉換,或者利用contains()函數看,是否還有小數點。
System.out.println("please input the ten strings:");
Scanner sc = new Scanner(System.in);
String number = sc.nextLine();
if(number.contains(".")){
//說明是一個小數
double d = Double.valueOf(s);
}
else{
//
int i = Integer.valueOf(s);
}