Java基础 - 常见对象(4) : 基本类型包装类

1. 为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型

	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 + "个");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值