Java.lang学习笔记

来源:互联网 发布:长沙蓝狐网络 编辑:程序博客网 时间:2024/05/19 22:46

String类

字符串长度

String name = "John";System.out.println(name.length());

字符串比较

  1. boolean equals() 检查组成字符串内容的字符
  2. boolean equalsIgnoreCase() 此方法比较两个字符串,忽略大小写形式
  3. int compareTo()
    按字母顺序比较两个字符串。
    如果两个字符串相等,则返回 0;
    如果字符串在该值之前,则返回值小于 0;
    如果字符串在该值之后,则返回值大于 0
  4. boolean startsWith() 检查一个字符串是否以另一个字符串开始。
  5. boolean endsWith() 检查一个字符串是否以另一个字符串结束。

    String temp1,temp2,temp3,temp4;
    temp1 = new String("hhhhh");
    temp2 = new String("ppppp");
    temp3 = new String("HHHHH");
    temp4 = new String("abcd");
    System.out.println(temp1.equals(temp3)+ " " + temp1.equalsIgnoreCase(temp3));
    System.out.println(temp1.compareTo(temp2)+ " " + temp1.compareTo(temp3));
    System.out.println(temp4.startsWith("a"));
    System.out.println(temp4.endsWith("d"));

搜索字符串

  1. int indexOf(character) :
    返回找到的第一个匹配的位置索引
    如果没有找到匹配,则返回 -1
String temp = new String("abcd");char c = 'c',e = 'e';System.out.println(temp.indexOf(c) + " " + temp.indexOf(e));    

提取字符串

  1. public char charAt(int index)
    此方法用于从指定位置提取单个字符,该位置由索引指定,索引中的值必须为非负
  2. public String substring(int index)
    此方法用于提取从位置索引开始的字符串部分
  3. public String substring(int beginindex, int endindex)
    此方法用于提取 beginindex 和 endindex 位置之间的字符串部分
  4. public String concat(String str)
    此方法用于连接两个字符串,并新建一个包含调用字符串的字符串对象
  5. public String replace(char old, char new)
    此方法用于将调用字符串中出现某个字符的所有位置都替换为另一个字符
  6. public String trim()
    此方法用于返回一个前后不含任何空格的调用字符串的副本
String temp = new String("abcdacd");System.out.println(temp.charAt(0));System.out.println(temp.substring(1));System.out.println(temp.substring(1, 3));System.out.println(temp.replace('a', 'b'));System.out.println(temp.trim());

更改字符串中字符的大小写

  1. toLowerCase() 全部变小写
  2. toUpperCase() 全部变大写
String temp = new String("ABCDacd");System.out.println(temp.toLowerCase());System.out.println(temp.toUpperCase());

StringBuffer 类

构造方法

  1. StringBuffer()
    默认构造器是由系统自动分配容量,默认是16个字符。由于没有赋值,所以缓冲对象的长度就是0.
  2. StringBuffer(int length) 设定容量大小的构造器
  3. StringBuffer(Stringvalue)
    接收字符串参数,用来设置初始内容,
    并在不重新分配的情况下保留 16 个字符的空间
    StringBuffer temp;    temp = new StringBuffer("");    System.out.println(temp.capacity());    System.out.println(temp.length());    temp = new StringBuffer(100);    System.out.println(temp.capacity());    System.out.println(temp.length());    temp = new StringBuffer("aa");    System.out.println(temp.capacity());    System.out.println(temp.length());

方法

  • StringBuffer insert(int pos,String s) 在指定位置后插入字符串
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp = temp.insert(1, "hhh");        System.out.println(temp);        temp = temp.insert(temp.length(), "bbb");        System.out.println(temp);        StringBuffer temp;        temp = new StringBuffer("abcd");        System.out.println(temp.insert(1, "hhh"));        System.out.println(temp.insert(temp.length(), "bbb"));
  • int length() 确定 StringBuffer 对象的长度
  • void setCharAt(int pos, char ch) 使用 ch 指定的新值设置 pos 指定的位置上的字符
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp.setCharAt(0, 'b');        System.out.println(temp);
  • String toString() 转换为字符串形式
  • StringBuffer reverse() 表示的是将一个输入流倒叙输出。
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp = temp.reverse();        System.out.println(temp);
  • StringBuffer delete(int start, int end) 此方法将删除调用对象中从 start 位置开始直到 end –1 位置的字符序列
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp = temp.delete(0, 2);        System.out.println(temp);
  • StringBuffer deleteCharAt(int pos) 此方法将删除 pos 指定的索引处的字符
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp = temp.deleteCharAt(0);        System.out.println(temp);
  • StringBuffer replace(int start, int end, String s) 此方法使用一组字符替换另一组字符。将用替换字符串从 start 指定的位置开始替换,直到 end 指定的位置结束
        StringBuffer temp;        temp = new StringBuffer("abcd");        temp = temp.replace(0, 2, "hhh");        System.out.println(temp);
  • StringBuffer append()
    这里写图片描述

区别

String:
是对象不是原始类型.
为不可变对象,一旦被创建,就不能修改它的值.
对于已经存在的String对象的修改都是重新创建一个新的对象,然后把新的值保存进去.
String 是final类,即不能被继承.

StringBuffer:
是一个可变对象,当对他进行修改的时候不会像String那样重新建立对象
它只能通过构造函数来建立,

总结: 如果在程序中需要对字符串进行频繁的修改连接操作的话.使用StringBuffer性能会更高

包装类

原始数据类型 包装类 byte(字节) Byte char(字符) Character int(整型) Integer long(长整型) Long float(浮点型) Float double(双精度) Double boolean(布尔) Boolean short(短整型) Short

命令行输入

在main 函数中 :public static void main(String[] args),此args就是在执行java程序的时候,输入的参数:
java 中args一般存在main主类方法内,String args[]或者String[]args表示给主方法传一个字符串数组. 而args是一个字符串数组的变量名,不是关键字,是arguments的缩写,只是一个默认名,
可以通过点击【Run】选择【Run Configurations。。。】,选择【arguments】,然后输入字符串,空格隔开
比如下一段代码可以通过输入两个字符串然后通过parseInt()函数转换现加法.
PS:
1. Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了。
2. Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性

        int a = Integer.parseInt(args[0]);        int b = Integer.parseInt(args[0]);        System.out.println(a + "+" + b + "=" + (a+b));

介绍几个包装类:

Character 类

(为什么会有这种方法。。)
这里写图片描述

Math 类

这里写图片描述

ceil方法

PS:
在javaSE 5.0 沿用了C语言的格式化输出printf()方法
f 表示浮点数;
s表示字符串;
d表示十进制数;

        double a = 0.5862;        double b;        b = Math.ceil(a);        System.out.printf("%.2f", b);        System.out.println();        b = Math.floor(a);        System.out.printf("%.2f", b);

总结

默认情况下,会将 java.lang 包导入到每个 Java 程序中
包装类可以以类的形式封装简单的原始类型
StringBuffer 类用作构建字符串的构建块
字符串是不可变的,也就是说字符串是常量并且不能改变它们的值
Math 是一个 final 类,用于定义基本数字运算和三角函数的方法

原创粉丝点击