1、基本类型

类型 字节数 范围
byte 1 -128 ~ 127
short 2 -32768 ~32767
int 4 −231 ~ 231−1
long 8
float 4
double 8
boolean 1
char 2

注意: short、char、byte 参与运算时直接抬升到 int 型。

思考题:

  • 0 是整数还是负数?
  • 字节数 - 128 在内存中的存储形态是如何的?
  • 字节范围为何是从 - 128 到 127 之间,而不是 - 127 到正的 128?

2、位运算

运算符 说明
&
^ 异或
~ 取反
<< 左移
>> 右移
>>> 无符号右移动

3、补码

正数的取反 + 1。

4、&& 和 & 区别

&& 存在短路,& 无短路操作,& 通过位运算完成。

5、字符集

字符集 表示字节数
asc 1 个字节的 7 位
gb2312 2 字节
gbk 2 字节
big5 5 个字节
iso8859-1 1 个字节 8 位
utf-8 中文 3 个字节
unicode 2 个字节,含有两个字节的头

任何字符集都是 asc 的超集。

6、练习

  1. 找出自己的名字对应的 unicode
  2. 定义函数,取出整数内存中的存储形态对应的 16 进制字符串
  3. 定义函数,取出整数内存中的存储形态对应的 2 进制字符串
  4. java 中 unicode 字符的全量输出

7、UML

Unified Modeling Language,同一建模语言,使用图形化元素描述事物以及之间的关系。

8、Rose

rose 是 UML 建模软件中的一种,IBM 公司开发,业界使用比较普遍。Rose 的安装步骤如下:

  1. 管理员运行 setup.exe 文件
  2. 指定安装目录,不要使用中文和空格
  3. 装后导入 license 文件
  4. 解决启动 rose 出现缺少 dll 文件问题
    4.1) 进入 rose 安装目录的 Common 目录下
    4.2) 复制 suite objects.dll + license.dll 文件到 C:\Windows\System32\和 C:\Windows\SysWOW64\下
  5. 启动 Rose
  6. OK

9、OOP

面向对象编程的特征:

  • 封装
  • 继承
  • 多态

10、IO

10.1 IO 划分

输入输出流,操纵文件的读写过程。java 的流架构图如下:

基本数据类型-位运算-字符集-流 - 图1

IO 类型的划分为:

  • 数据类型
    • 字节流
      InputStream / OutputStream
    • 字符流
      Reader / Writer
  • 方向
    • 输入流
      InputStream / Reader
    • 输出流
      OutputStream / Writer
  • 功能
    • 缓冲区流
      BufferedInputStream / BufferedReader / BufferedOutputStream / BufferedWriter
    • 转换流
      InputStreamReader

10.2 内存流

  1. @Test
  2. public void writeByteArrayOutputStream() throws Exception {
  3. ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
  4. baos.write("abc".getBytes("unicode"));
  5. byte[] bytes = baos.toByteArray();
  6. baos.close();
  7. System.out.println(bytes.length);
  8. }

10.3 压缩流

  1. @Test
  2. public void testZip() throws Exception {
  3. FileOutputStream fos = new FileOutputStream("d:\\java\\xxx.zip") ;
  4. ZipOutputStream zos = new ZipOutputStream(fos) ;
  5. byte[] buf = new byte[1024] ;
  6. int len = -1 ;
  7. File dir = new File("d:\\java\\zip") ;
  8. File[] files = dir.listFiles();
  9. for(File f : files){
  10. if(f.isFile()){
  11. String fname = f.getName() ;
  12. FileInputStream fin = new FileInputStream(f) ;
  13. zos.putNextEntry(new ZipEntry(fname));
  14. while((len = fin.read(buf)) != -1){
  15. zos.write(buf , 0 , len);
  16. }
  17. fin.close();
  18. }
  19. }
  20. zos.close();
  21. fos.close();
  22. }
  23. @Test
  24. public void testUnzip() throws Exception {
  25. FileInputStream fin = new FileInputStream("d:\\java\\xxx.zip") ;
  26. ZipInputStream zis = new ZipInputStream(fin) ;
  27. byte[] buf = new byte[1024] ;
  28. int len = 0 ;
  29. ZipEntry entry ;
  30. String ouputDir = "d:\\java\\zip\\out" ;
  31. while((entry = zis.getNextEntry()) != null){
  32. String fname = entry.getName() ;
  33. File f = new File(ouputDir , fname) ;
  34. FileOutputStream fos = new FileOutputStream(f) ;
  35. while((len = zis.read(buf)) != -1){
  36. fos.write(buf , 0 , len);
  37. }
  38. fos.close();
  39. }
  40. zis.close();
  41. }

10.4 对象流

  1. @Test
  2. public void testSerial() throws Exception {
  3. Person p = new Person("");
  4. p.setId(15);
  5. p.setName("tomas");
  6. p.setAge(22);
  7. FileOutputStream fos = new FileOutputStream("d:\\java\\p.dat") ;
  8. ObjectOutputStream oos = new ObjectOutputStream(fos) ;
  9. oos.writeObject(p);
  10. oos.close();
  11. fos.close();
  12. }
  13. @Test
  14. public void testDeserial() throws Exception {
  15. FileInputStream fis = new FileInputStream("d:\\java\\ppp.dat") ;
  16. ObjectInputStream ois = new ObjectInputStream(fis) ;
  17. Person p = (Person) ois.readObject();
  18. ois.close();
  19. fis.close();
  20. System.out.println(p.getName());
  21. System.out.println(p.isMarried());
  22. }

11、设计模式

12、作业

  1. 把 long 数据转换成字节数组
  2. 把字节数组数据转换成 long
  3. 有 5 亿整数 (非负),去重计算不同整数的个数,300M 内存
  4. 通过程序创建文本文件,内容是 abc,采用 uncode 码,文件大小是 10 字节
  5. 将 byte 变换成无符号的整数 (0 ~ 255 , 正数不变)
    https://www.cnblogs.com/xupccc/p/9594153.html
    https://www.cnblogs.com/xupccc/p/9594153.html