• 字符串
  • 静态static

1.字符串

String:适用于少量的字符串操作的情况。
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况。
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况。
在运行方面速度快慢为:StringBuilder > StringBuffer > String
string用法
1.

public class str
{
    public static void main(String[] args)
    {
        byte[] b={97,98,99};
        String str=new String(b);
        System.out.println(str);
    }
}//输出结果为abc

2.

public class strTest
{
    public static void main(String[] args)
    {
        char[] c={'H','e','l','l','o'};
        String str=new String(c);
        System.out.println(str);
    }
}//输出结果为hello

3.

public class strTest
{
    public static void main(String[] args)
    {
        byte[] b={97,98,99,100,101,102};
        String str=new String(b,3,2);
        System.out.println(str);
    }
}//从数组元素b[3]开始(包括b[3]在内)的连续两个元素以字符串形式输出。

字符串的一些常见函数

连接concat()、提取substring()、charAt()、length()、equals()、equalsIgnoreCase()等
连接concat()
String str1="you";
String str2=" welcome";
System.out.println(str1.concat(str2));//输出结果:you welcome
提取substring()

String str="we are students and he is a techer";
System.out.println(str.substring(2,10));//输出结果: are stu

charAt()
charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法.
charAt()方法返回指定索引位置的char值。索引范围为0~length()-1.
如: str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符.

 String str="we are students and he is a worker";
 System.out.println(str.charAt(1));//输出结果:e

equals()
比较两个字符串是否相等

public class test {
 
    public static void main(String[] args) {
         String str1="we are students and he is a worker";
         String str2="we are students and he is a worker";
            System.out.println(str1.equals(str2));
    }
}//输出true

equalsIgnoreCase()
忽略字符大小写的字符串比较用equalsIgnoreCase()

public class test {
 
    public static void main(String[] args) {
         String str1="we are students and he is a worker";
         String str2="We ARE students AND he is a worker";
            System.out.println(str1.equalsIgnoreCase(str2));
    }
}//输出true

检索函数

public class test {
 
    public static void main(String[] args) {
        String str="我们一起数到6吧!";
        System.out.println(str.indexOf("一"));
                System.out.println(str.indexOf("6"));
                System.out.println(str.startsWith("我"));
                System.out.println(str.endsWith("!"));
    }
}
输出:2
6
true
true

2.静态static

(1)static成员变量:

Java类提供了两种类型的变量:用static关键字修饰的静态变量和不用static关键字修饰的实例变量。静态变量属于类,在内存中只有一个复制,只要静态变量所在的类被加载,这个静态变量就会被分配空间,因此就可以被使用了。对静态变量的引用有两种方式,分别是“类.静态变量"和”对象.静态变量"

实例变量属于对象,只有对象被创建后,实例变量才会被分配内存空间,才能被使用,它在内存中存在多个复制,只有用“对象.实例变量”的方式来引用。
(2)static成员方法:

Java中提供了static方法和非static方法。static方法是类的方法,不需要创建对象就可以被调用,而非static方法是对象的方法,只有对象被创建出来后才可以被使用

static方法中不能使用this和super关键字,不能调用非static方法,只能访问所属类的静态成员变量和成员方法,因为当static方法被调用时,这个类的对象可能还没被创建,即使已经被创建了,也无法确定调用哪个对象的方法。同理,static方法也不能访问非static类型的变量。

单例设计模式:

static一个很重要的用途就是实现单例设计模式。单利模式的特点是该类只能有一个实例,为了实现这一功能,必须隐藏类的构造函数,即把构造函数声明为private,并提供一个创建对象的方法,由于构造对象被声明为private,外界无法直接创建这个类型的对象,只能通过该类提供的方法来获取类的对象,要达到这样的目的只能把创建对象的方法声明为static,程序实例如下:

class Singleton{
    private static Singleton instance=null;
    private Singleton(){}
    public static Singleton getInstance(){
        if(instance==null){
            instance=new Singleton();
        }
        return instance;
    }
}

(3)static代码块
static代码块在类中是独立于成员变量和成员函数的代码块的。注意: 这些static代码块只会被执行一次

(4)static与final结合使用表示的意思:
对于变量,若使用static  final修饰,表示一旦赋值不能修改,并且通过类名可以访问。
对于方法,若使用static final修饰,表示该方法不可被覆盖,并且可以通过类名直接访问。

注:本文静态static部分摘自「互联网极客」的原创文章,仅供学习笔记
原文链接:https://blog.csdn.net/jsqfengbao/article/details/44724219

Last modification:March 12th, 2021 at 09:16 pm
如果觉得我的文章对你有用,请随意赞赏