A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马伟恒 中级黑马   /  2012-4-19 12:35  /  1409 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class TestByteArray {
public static void main(String[] args) throws Exception{
  String s = "NI HAO BEIJING";
  ByteArrayInputStream bis = null;
  ByteArrayOutputStream bos = null;
  
  bis = new ByteArrayInputStream(s.getBytes());
  bos = new ByteArrayOutputStream();
  
  int temp = 0;
  while((temp=bis.read())!=-1){
   char c = (char)temp;
   bos.write(Character.toLowerCase(c));
  }
  System.out.println( bos.toString());
     bis.close();
   bos.close();
   }
}


程序中的Character.toLowerCase(c)它的返回值是char类型但是bos.write()方法里面接收的是int类型的数据  为什么还可以这样写?




2 个回复

倒序浏览
  1. package com.heima.test;

  2. public class test {

  3.         public static void main(String[] args) {

  4.                 char ch='A';
  5.                 int i=ch;
  6.                 System.out.println(i);
  7.         }
  8. }
复制代码
输出的结果是:
65
所以你的bos.write()方法接收一个由char类型自动转换成的int类型。
回复 使用道具 举报
bos.write(Character.toLowerCase(c));执行这行代码的时候,char类型会自动提升类型为int类型。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马