Digital learning java string

java learning is based on information provided on how2j organize any similarity, it is normal.

Wrapper Class

All basic types, the package has a corresponding class.

Digital Wrapper Class

Digital Wrapper class has Byte, Short, Integer, Long, Float, Double, these classes are abstract classes, word Number. Such as classes is encapsulation int Integer, Integer wrapper class is called.

How that basic types of packages into classes.

public class TestNumber{
    public static void main(String[] args){
        int i = 1;
        //基本类型转换为封装类
        Integer it = new Integer(i);
    }
}

How that wrapper classes into basic types

public class TestNumber{
    public static void main(String[] args){
        int i = 1;
        Integer it = new Integer(i);
        //封装类转换为基本类型
        int i2 = it.intValue();
    }
}

That Shajiao automatic boxing and unboxing. That is not like the above-described packing method call the constructor, the type Integer can be automatically converted by =.

public class TestNumber{
    public static void main(String[] args){
        int i = 2;
        //自动装箱
        Integer it = i;
    }
}

That automatic unpacking, no need to call the Integer intValue method. Automatically converted by = int type, called unpacking.

public class TestNumber{
    public static void main(String[] args){
        Integer it = 5;
        //自动拆箱
        int i = it;
    }

There is a relatively common Int maximum and minimum values ​​and by calling Integer.MAX_VALUE of Integer.MIN_VALUE;

public class TestNumber{
    public static void main(String[] args){
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
    }
}

Exercises automatic boxing and unboxing

byte a = 3;
short b = 3;
float c = 3;
double d = 3;
//byte自动装箱和拆箱
Byte az=new Byte(a); 或 Byte az = a;
byte ac=az.byteValue(); 或 byte ac = az;
//short自动装箱和拆箱
Short bz = new Short(b);或 Short bz = b;
short bc = bz.shortValue();或short bc = bz;

How to turn digital string? We have considered this issue yet. There are two methods.

方法1. 使用String的静态方法valueOf
方法2. 使把基本类型装箱为对象,然后调用对象的toString

public class TestNumber{
    public static void main(String[] args){
        int i = 5;
        
        //方法1
        String str = String.valueOf(i);
        System.out.pringln(str);
        //方法2
        Integer i2 = i;
        String str2 = Integer.toString(i2);
        System.out.println(str2);
    }
}

That string of numbers turn it?

调用Integer的parseInt方法

public class TestNumber{
    public static void main(String[] args){
        String str = '999';
        //调用Integer的parseInt方法
        Integer i = Integer.parseInt(str);
        System.out.println(i);
    }
}

That the float into a string 3.14 "3.14" in the string "3.14" Converting to float 3.14

public class TestNumber{
    public static void main(Strign[] args){
        float f = 3.14;
        //使用方法一
        String str = String.valueOf(f);
        //使用方法二
        Float f1 = f;
        String str1 = Float.toString(f1);
        Float f2 = Float.parseFloat(str1);
    }
}

java also provide some mathematical methods, and is present in the form of a static method, it means that you can be called directly. I have a few common.

public class TestNumber{
    public static void main(String[] args){
        float f1 = 5.4f;
        float f2 = 5.5f;
        
        //5.4四舍五入到5
        System.out.println(Math.round(f1));
        
        //5.5四舍五入到6
        System.out.pringln(Math.round(f2));
        
        //得到0-1之间的随机浮点数(取不到1)
        System.out.println(Math.random());
        
        //得到0-10之间的随机数(取不到10),这个比较常用
        System.out.println((int)(Matn.random()*10));
        
        //开方
        System.out.println(Math.sqrt(9));
        
        //次方
        System.out.println(Math.pow(2,4));
        
        //派
        System.out.println(Math.PI);
        
        //自然常数
        System.out.println(Math.E);
    }
}

Then how do formatted output?

%s 表示字符串
%d 表示数字
%n 表示换行

Code demonstrates. as follows

public class TestNumber{
    public static void main(String[] args){
        String name = "小明";
        int count = 3;
        String title = "一等奖";

        //直接使用+进行字符串连接
        String sentence = name + "获得"+count+"次"+title;
        System.out.println(sentence);
        //使用格式化输出
        String sentence1 = "%s获得%d次%s";
        System.out.printf(sentence1,name,count,title);
    }
}

Use printf format and can achieve exactly the same effect

public class TestNumber{
    public static void main(String[] args){
        String name = "小明";
        int count = 3;
        String title = "一等奖";

        //直接使用+进行字符串连接
        String sentence = name + "获得"+count+"次"+title;
        System.out.println(sentence);
        //使用格式化输出
        String sentence1 = "%s获得%d次%s";
        System.out.format(sentence1,name,count,title);
    }
}

character

Character type package type Character.

public class TestChar{
    public static void main(String[] args){
        char c1 = '1';
        //装箱
        Character c = c1; 
        //拆箱
        char c2 =c;
    }
}

A common method wrapper classes that char Character of the following categories.

Character.isLetter('a') //判断是否为字母
Character.isDigit('a') //判断是否数字
Character.isWhitespace(' ')//判断是否为空白
Character.isUpperCase('a') //判断是否为大写
Character.isLowerCase('a') //判断是否为小写
Character.toUpperCase('a') //转换为大写
Character.toLowerCase('A') //转换为小写 

Character type into a string.

public class TestChar{
    public static void main(String[] args){
        //下面的部分会报错,char无法转换成String
        String a = 'a';
        System.out.println(a);
        //可修改成如下
        String a = Character.toString('a');
        System.out.println(a);
    }
}

Will error, error follows.

TestChar.java:4: 错误: 不兼容的类型: char无法转换为String
        String a = 'a';
                   ^

String

Create a string There are three common ways.

1. 每当有一个字面值出现的时候,虚拟机就会创建一个字符串
2. 调用String的构造方法创建一个字符串对象
3. 通过+加号进行字符串拼接也会创建新的字符串对象


public class TestString{
    public static void main(String[] args){
        String gareen = "盖伦";
        String temmo = new String("提莫");
        char[] cs = new char[]{'小','明'};
        String hero = new String(cs);
        String hero3 = gareen + temmo;
    }
}

String is modified as final, it can not be inherited. Strings are immutable, immutable.

String str = "张三";
str = "李四";
System.out.println(str); //输出李四,str="李四",是重新引用了一个对象,而不是修改了张三这个对象

String common method.

The method explained
charAt (int index) Gets the specified character position
toCharArray () Gets a character array corresponding
subString () taken string
split according to the split separator
trim trailing spaces removed
toLowerCase all lowercase
toUpperCase all capitalized
indexOf string Analyzing or the position of the substring occurring
contains contains substring
replaceAll replace all
replaceFirst only replace the first

How to determine whether or not, can be judged by the same object ==, see the following code.

public class TestString{
    public static void main(String[] args){
        String str1 = "the light";
        String str2 = new String(str1);
        String str3 = "the light";
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
    }
}

Determine whether the contents of the same. Compare strings using equals content must be the same case. equalsIgnoreCase ignore case to determine whether the content is consistent.

public class TestString{
    public static void main(String[] args){
        String str1 = "the light";
        String str2 = new String(str1);
        String str3 = "the light";
        String str4 = "THE LIGHT";
        //判断对象是否一致
        System.out.println(str1 == str2);
        System.out.println(str1 == str3);
        //equals内容一样返回true
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str4));
        //忽略大小写的比较
        System.out.println(str1.equalsIgnoreCase(str4));
    }
}

To determine whether a string starts with xx, xx whether to end.

public class TestString1 {

    public static void main(String[] args) {
        String str1 = "the light";

        String start = "the";
        String end = "Ight";

        System.out.println(str1.startsWith(start));//以...开始,得到true
        System.out.println(str1.endsWith(end));//以...结束,得到flase

    }
}

StringBuffer variable length strings.

The method explained
append additional
delete Delete
insert inserted
reverse inversion
length length
capacity Capacity

Look at the code.

public class TestString{
    public static void main(String[] args){
        String str1 = "let here";

        StringBuffer sb = new StringBuffer(str1);

        //在最后追加
        sb.append("be light");
        System.out.println(sb);

        //删除4到10之间的字符
        sb.delete(4,10);
        System.out.println(sb);

        //在4插入there
        sb.insert(4,"there ");
        System.out.println(sb);

        //反转
        sb.reverse();
        System.out.println(sb);
    }
}

Why StringBuffer can be longer, and the interior is the same as a character array String, StringBuffer also maintains an array of characters. However, this array of characters, leaving a surplus length.

Guess you like

Origin www.cnblogs.com/yihang996/p/11668867.html