package com.web;
public class Test34 {
public static void main(String[] args) {
//byte即字节的意思,是java中的基本类型,存储字节用,后面还会用到字节数组,再字节流的时候。
//通常在读取非文本文件时(如图片,声音,可执行文件)需要用字节数组来保存文件的内容,在下载文件时,也是用byte数组作临时的缓冲器接收文件内容。所以说byte在文件操作时是必不可少的。不管是对文件写入还是读取都要用到。
//-128<= byte <=127,范围[-128,127]
// //3.设置两个流,进行流拷贝操作
// 读取文件资源
// FileInputStream fin = new FileInputStream(file);
// //从response中获取输出流
// ServletOutputStream outputStream = response.getOutputStream();
//int len = 0;
// byte[] arr = new byte[1024];
//
// while ((len=fin.read(arr))!= -1){
// 把字节转成String 从0到len变成String
// outputStream.write(arr,0,len);
// }
// fin.close();
byte b1 = 127;
byte b2 = -128;
//byte b3 = 128;
//byte b4 = -129;
System.out.println("b1 = " + b1);
System.out.println("b2 = " + b2);
//范围小的类型向范围大的类型提升,`byte、short、char` 运算时直接提升为`int` 。
//byte、short、char-->int-->long-->float-->double
//int i = 1;
// byte b = 2;
// byte x = b + i; // 报错
// int类型和byte类型运算,结果是int类型
int i1 = (int) b1;//可以写成int i1 = b1;
System.out.println("i1 = " + i1);
int i2 = 150;//i范围[-128,127]内转换没有错误
byte b3 = (byte) i2;
System.out.println("b3 = " + b3);
//Stringとbyte配列の変換
//String→byte変換
String str1 = "ありがとうございました。";
byte[] bytes = str1.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println("b[" + i + "] = " + bytes[i]);
}
//byte→String変換
byte[] sbyte = "12345".getBytes();
String str2 = new String(sbyte);
System.out.println("str1 = " + new String(bytes));
System.out.println("str2 = " + str2);
}
}
java数据类型,byte类型的用法
Guess you like
Origin blog.csdn.net/djydjy3333/article/details/121490909
Recommended
Ranking