byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
2. Integer的构造方法
public Integer(int value)
public Integer(String s) : 注意:这里的字符串必须是由数字字符组成,否则会报格式异常
public class IntegerDemo {
public static void main(String[] args) {
int i = 100;
Integer ii = new Integer(i);
System.out.println("ii:" + ii);
String s = "100";
// String s = "abc"; // NumberFormatException
Integer iii = new Integer(s);
System.out.println("iii:" + iii);
}
}
3. int类型和String类型的相互转换
/*
* int类型和String类型的相互转换
*
* int -- String
* String.valueOf(number)
* Integer.toString(number)
*
* String -- int
* Integer.parseInt(s)
*/
public class IntegerDemo {
public static void main(String[] args) {
// int -- String
int number = 100;
// 1
String s1 = "" + number;
System.out.println("s1:" + s1);
// 2
String s2 = String.valueOf(number);
System.out.println("s2:" + s2);
// 3
// int -- Integer -- String
Integer i = new Integer(number);
String s3 = i.toString();
System.out.println("s3:" + s3);
// 4
// public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);
System.out.println("-----------------");
// String -- int
String s = "100";
// 1
// String -- Integer -- int
Integer ii = new Integer(s);
// public int intValue()
int x = ii.intValue();
System.out.println("x:" + x);
// 2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y:"+y);
}
}
4. Integer的面试题
Integer的数据直接赋值,如果在-128~127之间,会直接从缓冲池里获取数据。
5.统计字符串中大写小写以及数字字符出现的次数案例
import java.util.Scanner;
public class CharacterTest {
public static void main(String[] args) {
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符串来查询大写小写以及数字字符的个数:");
String line = sc.nextLine();
char[] chs = line.toCharArray();
for (int x = 0; x < chs.length; x++) {
char ch = chs[x];
if (Character.isUpperCase(ch)) { //判断是否为大写字母
bigCount++;
} else if (Character.isLowerCase(ch)) { //判断是否为小写字母
smallCount++;
} else if (Character.isDigit(ch)) { //判断是否为数字
numberCount++;
}
}
System.out.println("大写字符" + bigCount + "个");
System.out.println("小写字符" + smallCount + "个");
System.out.println("数字字符" + numberCount + "个");
}
}