Java的String类、Object类、包装类

2023-11-20

1. String类

1.1 String类的两种实例化方式:

//1.直接赋值:
String str="hello";
//2.使用构造方法new的形式赋值
String str=new String("hello");

1.2 String类定义的字符串的比较:

//2.使用"=="方法比较
String str1="hello";
String str2=new String("hello");
System.out.println(str1==str2);
//输出结果为:false

//解析:因为此时的"=="比较,是String类的对象的比较,并不是字符串本身内容的比较

具体解析进行画图,如下图所示:

//2.使用equals()方法比较
String str1="hello";
String str2=new String("hello");
System.out.println(str1.equals(str2));
//输出结果为:true

//解析:因为equals()方法进行的是字符串内容的比较

1.3 String类的两种实例化的区别:

//1.直接赋值
String str1="hello";
String str2="hell0";
String str3="hello";
System.out.println(str1==str2);
System.out.println(str2==str3);
System.out.println(str1==str3);
//输出结果为:
true
true
true

解析如下:

       因为String类的设计使用了共享设计模式,在JVM底层实际上会自动维护一个对象池(字符串对象池),如果采用了直接赋值的方式进行String类的对象实例化操作,那么该实例化对象的字符串内容将自动保存在这个对象池中,如果下次继续使用直接赋值的方式声明String类对象,那么此时对象池中若有指定内容,将直接进行引用;若没有,则开辟新的字符串对象然后将其保存在对象池中以供下次使用。所以在上述代码中,字符串"hello"并没有开辟新的堆内存空间,故str1、str2、str3都指向一块堆内存空间。

//2.使用构造方法赋值
String str1=new String("hello");
String str2=new String("hell0");
System.out.println(str1==str2);
//输出结果为:false

解析如下:

       第一点:使用String构造方法会在堆内存中开辟两块空间:1.字符串常量"hello"所开辟的堆内存空间 2.new所开辟的堆内存空间;然而str1指向的是new所开辟的空间,所以通过字符串常量开辟的空间就成为了垃圾空间。

       第二点:只要有new关键字就会在堆内存中开辟空间,所以str1所指向的位置与str2所指向的位置不同,故输出结果为false 

//3.字符串共享问题
String str1=new String("hello");
//此时的字符串常量"hello"并没有保存在对象池里
String str2="hello";
//此时的字符串"hello"保存在了对象池里
System.out.println(str1==str2);
//输出结果为:false

解析:因为str1所指向的字符串的内容没有入池,故str1和str2所指向的堆内存空间不同,所以为false

//4.使用intern()方法
String str1=new Strng("hello").intern();
//此时,new所开辟的堆空间的字符串"hello"入池
String str2="hello";
System.out.println(str1==str2);
//输出结果为:true

解析:由于new所开辟的堆内存中的字符串"hello"已入池,故直接赋值的方式的匿名对象str2就和str1指向同一块堆内存,所以输出结果为true。

1.4 解释String类中两种对象实例化的区别:

1.直接赋值:相同字符串常量只会开辟一块堆内存空间,并且该字符串对象可以自动保存在对象池中以供下次使用

2.使用构造方法赋值:会开辟两块堆内存空间,其中一块堆内存空间会成为垃圾空间,并且不会自动保存在对象池中,只有使用了intern()方法手动让new开辟的堆内存入池。

1.5 字符串常量不可变更

       即字符串常量一旦定义不可改变。

//观察如下代码:
String str="hello";
str=str+"world";
str=str+"!!!";
System.out.println(str);
//输出结果为:helloword!!!

画图作出具体解析:

由上述画图分析知:以上字符串的变更是字符串对象的变更而不是字符串常量的变更。

1.6 关于字符、字符数组、字节与字符串之间的转换

//1.字符串到字符的转换
//String类的普通方法public char charAt(int index)
//功能:取得指定索引位置的字符,索引从0开始
public class Test1 {
	public static void main(String[] args)
	{
		String str="hello";
		char data=str.charAt(0);
		System.out.print(data);
	}
}
//输出结果:h
//2.字符串到字符数组的转换
//String类的普通方法:public char[] toCharArray()
//功能:将字符串转换为字符数组
public class Test1 {
	public static void main(String[] args)
	{
		String str="hello";
		char[] data=str.toCharArray();
		for(char i:data)
		{
			System.out.print(i+" ");
		}
		
	}
}
//输出结果:h e l l o
//3.将字符数组转换为字符串
//String类的构造方法public String(char value[])
//功能:将字符数组中的所有内容变为字符串
public class Test1 {
	public static void main(String[] args)
	{
		char[] data=new char[] {'h','e','l','l','o'};
		String str=new String(data);
		System.out.println(str);
	}
}
//输出结果:hello
//4.将部分数组中的内容转换为字符串
//String类的构造方法public String(char value[],int offset,int count)
//功能:将字符数组的下标为offset开始的count个字符转换为字符串
public class Test1 {
	public static void main(String[] args)
	{
		char[] data=new char[] {'h','e','l','l','o'};
		String str=new String(data,2,3);
		System.out.println(str);
	}
}
//输出结果:llo
//5.将字节数组转换为字符串
//String类的构造方法public String(byte byte[])
public class Test1 {
	public static void main(String[] args)
	{
		byte[] data=new byte[] {'h','e','l','l','o'};
		String str=new String(data);
		System.out.println(str);
	}
}
//输出结果:hello
//6.将部分字节数组中的内容转换为字符串
//String类的构造方法public String(byte bytes[],int offset,int length)
//功能:将字节数组bytes下标为offset开始共length个字节展缓为字符串
public class Test1 {
	public static void main(String[] args)
	{
		byte[] data=new byte[] {'h','e','l','l','o'};
		String str=new String(data,1,2);
		System.out.println(str);
	}
}
//输出结果:el
//7.将字符串转换为字符数组
//String类的普通方法public byte[] getBytes()
//功能:将字符串以字节数组的形式返回
public class Test1 {
	public static void main(String[] args)
	{
		String str="hello";
		byte[] bytes=str.getBytes();
		for(byte i:bytes)
		{
			System.out.print(i+" ");
		}
	}
}
//输出结果:104 101 108 108 111

1.7 字符串之间的比较

//1.关于字符串之间区分大小写的比较
//String类的普通方法public boolean equals(Object anObject)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello";
		String str2="Hello";
		System.out.println(str1.equals(str2));
	}
}
//输出结果:false
//2.关于字符串之间不区分大小写的比较
//String类的普通方法public equalsIgnoreCase(String anotherString)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello";
		String str2="Hello";
		System.out.println(str1.equalsIgnoreCase(str2));
	}
}
//输出结果:true
//3.比较两个字符串大小关系
//String类的普通方法public int compareTo(String antherString)
//功能:两个字符串相等,返回0;不相等时,返回两个字符串的ASCII的差值
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello";
		String str2="Hello";
		String str3="hello";
		System.out.println(str1.compareTo(str2));
		System.out.println(str1.compareTo(str3));
	}
}
//输出结果:32 0

1.8 字符串查找

//1.判断一个字符串是否存在
//String类的普通方法public boolean contains(CharSequence s)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="hello";
		String str3="hi";
		System.out.println(str1.contains(str2));
		System.out.println(str1.contains(str3));
	}
}
//输出结果:true false
//2.从头开始查找指定字符串的位置,查到了返回位置的开始索引,查不到返回-1
//String类普通方法public int indexOf(String str)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		String str3="hi";
		System.out.println(str1.indexOf(str2));
		System.out.println(str1.indexOf(str3));
	}
}
//输出结果:6 -1
//3.从指定位置开始查找子字符串的位置
//String类的普通方法public int indexOf(String str,int formIndex)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		System.out.println(str1.indexOf(str2,4));
		System.out.println(str1.indexOf(str2,7));
	}
}
//输出结果:6 -1
//4.右后向前查找子字符串位置
//String类的普通方法public int lastIndexOf(String str)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		String str3="hi";
		System.out.println(str1.lastIndexOf(str2));
		System.out.println(str1.lastIndexOf(str3));
	}
}
//输出结果:6 -1
//5.从指定位置右后向前查找
//String类的普通方法public int lastIndexOf(String str,int formIndex)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		System.out.println(str1.lastIndexOf(str2,10));
		System.out.println(str1.lastIndexOf(str2,2));
	}
}
//输出结果:6 -1
//6.判断是否以字符串开头
//String类的普通方法public boolean startsWith(String prefix)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		String str3="hello";
		System.out.println(str1.startsWith(str2));
		System.out.println(str1.startsWith(str3));
	}
}
//输出结果:false true
//7.从指定位置开始判断是否以指定字符串开头
//String类的普通方法public boolean startsWith(String prefix,int offset)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		String str3="hello";
		System.out.println(str1.startsWith(str2,6));
		System.out.println(str1.startsWith(str3,2));
	}
}
//输出结果:true false
//8.判断是否以指定字符串结尾
//String类的普通方法public boolean endsWith(String suffix)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		String str2="world";
		String str3="hello";
		System.out.println(str1.endsWith(str2));
		System.out.println(str1.endsWith(str3));
	}
}
//输出结果:true false

1.9 字符串替换

//1.替换所有的指定内容
//String类的普通方法public String replaceAll(String regex,String replacement)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		System.out.println(str1.replaceAll("o","k"));
	}
}
//输出结果:hellk wkrld
//2.替换首个内容
//String类的普通方法public String replaceFirst(String regex,String replacement)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world";
		System.out.println(str1.replaceFirst("o","m"));
	}
}
//输出结果:hellm world

1.10 字符串拆分(可以将完整字符串按照指定的分隔符划分为若干个子字符串)

//1.将字符串全部拆分
//String类的普通方法public String[] split(String regex)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world hello world";
		//按照空格分隔符将str1字符串进行拆分为字符串数组
		String[] array=str1.split(" ");
		for(int i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
	}
}
//输出结果:
//hello 
//world 
//hello 
//world
//2.将字符串部分拆分,该数组长度就是limit极限
//String类的普通方法public String[] split(String regex,int limit)
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello world hello world";
		//按照空格分隔符将str1字符串进行拆分为字符串数组
		String[] array=str1.split(" ",2);
		//此时,字符串数组array中含有两个字符串
		for(int i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
	}
}
//输出结果:
//hello 
//world hello world
//3.当发现有些字符串无法正常拆分时,使用"\\"进行转义
public class Test1 {
	public static void main(String[] args)
	{
		String str1="hello.world.hello.world";
		//按照空格分隔符将str1字符串进行拆分为字符串数组
		String[] array=str1.split("\\.");  
		//以上的拆分使用了"\\"对"."进行了转义,发现如果不进行转义就不能进行拆分
		for(int i=0;i<array.length;i++)
		{
			System.out.println(array[i]);
		}
	}
}
//输出结果:
//hello 
//world 
//hello 
//world
//4.实际开发之中,经常会用到字符串的拆分,举例如下:
public class Test1 {
	public static void main(String[] args)
	{
		String str="xie:20|sun:19|yang:19";
		//将其字符串拆分成xie 20 sun 19 yang 19
		String[] str1=str.split("\\|");  
		//此时将str拆分成str1[3]={"xie:20","sun:19","yang:19"};
		for(int i=0;i<str1.length;i++)
		{
			System.out.println(str1[i]);
			//双重拆分
			String[] str2=str1[i].split(":");
			for(int j=0;j<str2.length;j++)
			{
				System.out.println(str2[j]);
			}
		}
	}
}
//输出结果:
//xie:20
//xie
//20
//sun:19
//sun
//19
//yang:19
//yang
//19

1.11 字符串截取

//1.从指定索引截取到结尾
//String类的普通方法public String subString(int beginIndex)
public class Test1 {
	public static void main(String[] args)
	{
		String str="my name is xie";
		String result=str.substring(8);
		System.out.println(result);
	}
}
//输出结果:is xie
//2.截取部分内容
//String类的普通方法public String subString(int beginIndex,int endIndex)
public class Test1 {
	public static void main(String[] args)
	{
		String str="my name is xie";
		String result=str.substring(3,7);
		//截取的是左闭右开的区间
		System.out.println(result);
	}
}
//输出结果:name

注:以上是关于字符串相关操作的常用方法,更多的方法自行了解!

1.12 String类、StringBuffer类、StringBuilder类的区别

(1)String类的特点:任何字符串常量都是String类的对象,并且String类的常量一旦声明不再改变字符串常量的内容,改变的只是其引用的指向而已。

(2)StringBuffer类的特点:String类的字符串常量具有不可修改性,但StringBuffer类的内容可以修改,若需要频繁修改字符串的内容,建议使用StringBuffer类。在String类中使用"+"进行字符串连接,但StringBuffer类的中字符串的修改需要append()方法,举例如下:

//StringBuffer类的字符串的连接
public class Test1 {
	public static void main(String[] args)
	{
		StringBuffer buffer=new StringBuffer();
		buffer.append("hello").append("world");
		System.out.println(buffer);
	}
}
//输出结果为:helloworld

(3)String类和StringBuffer类之间不能直接转换,需采用如下规则:

String类变为StringBuffer类:利用StringBuffer类的构造方法或者append()方法;

StringBuffer类变为String类:调用toString()方法。

(4)StringBuilder类的用法和StringBuffer类的用法一致,但StringBuilder类没有修饰符synchronized。

(5)StringBuffer类采用同步处理,属于线程安全操作;而StringBuilder类采用异步处理,属于线程不安全操作。

2.Object类

        Java中除了Object类,所有的类都存在继承关系,默认继承Object类,故所有类的对象都可以使用Object类进行接收。

2.1 使用Object类接收所有类的对象

class Person{}
class Student{}
public class Test1 {
	public static void main(String[] args)
	{
		Object obj1=new Person();
		Object obj2=new Student();
		System.out.println(obj1);
		System.out.println(obj2);
	}
}
输出结果:
Person@70dea4e
Student@5c647e05

由上述代码说明,Object类可以接收任何类的对象。

2.2 Object类的无参构造方法

//Object类的无参构造方法
//public Object()
public class Test1 {
	public static void main(String[] args)
	{
		Object obj=new Object();
		System.out.println(obj);
	}
}
输出结果:
java.lang.Object@70dea4e
//目的:无参构造方法为子类实例化需要所服务。

2.3 Object类的toString()方法

//Object类的toString()方法
//public String toString()
//功能:取得对象信息
public class Test1 {
	public static void main(String[] args)
	{
		Object obj=new Object();
		System.out.println(obj.toString());
	}
}
输出结果:
java.lang.Object@70dea4e

默认的Object类的toString()方法只能够得到一个对象的地址,这是所有类的对象都具有的特征。若觉得Object类默认提供的toString()方法功能不足,可以在需要的子类上覆写其toString()方法。下面讲述String类覆写Object类的toString()方法的功能。

2.4 String类覆写Object类的toString()方法的功能

//String类覆写toString()方法
//功能:字符串内容的比较
public class Test1 {
	public static void main(String[] args)
	{
		Object obj1=new String("hello");
		System.out.println(obj1);
	}
}
输出结果:hello

2.5 自定义类并覆写Object父类的toString方法

class Person{
	private String name;
	private int age;
	//Person类的构造方法
	public Person(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	//Person类覆写Object父类的toString()方法
	public String toString()
	{
		return "姓名:"+this.name+" 年龄:"+this.age;
	}
}
public class Test1 {
	public static void main(String[] args)
	{
		Person person=new Person("王麻子",18);
		System.out.println(person);//默认输出对象调用的就是toString()方法
   	}
}
输出结果:姓名:王麻子 年龄:18

2.6 Object类的equals()方法

//Object类的equals方法
///功能:对象的比较
public class Test1 {
	public static void main(String[] args)
	{
		Object obj1=new Object();
		Object obj2=obj1;
		Object obj3=new Object();
		System.out.println(obj1.equals(obj2));
		System.out.println(obj1.equals(obj3));
	}
}
输出结果:
true
false

2.7 覆写Object类的equals()方法

Object类中默认提供的equals()方法用于对象的比较,但对于功能不足的要求,所以子类可以覆写父类Object的equals()方法:

//覆写Object类的equals()方法,不再是用于对象比较,而是用于属性值的比较

class Person{
	private String name;
	private int age;
	//Person类的构造方法
	public Person(String name,int age){
		this.name=name;
		this.age=age;
	}
	//Person类覆写Object父类的equals()方法
	public boolean equals(Object obj)
	{
		if(obj==null)
			return false;
		if(obj==this)
			return true;
		//判断obj是不是由Person类实例化的
		if(!(obj instanceof Person))
		{
			return false;
		}
		Person person=(Person)obj;
		//比较属性值
		return this.name==person.name&&this.age==person.age;
	}
}
public class Test1 {
	public static void main(String[] args){
		Object object1=new Person("xie",20);
		Object object2=new Person("xie",20);
		Object object3=new Person("sun",19);
		System.out.println(object1.equals(object2));
		System.out.println(object1.equals(object3));
	}
}
输出结果:
true
false

2.8 Object类接收引用数据类型。之前分析Object类接收任意类的对象,因为Object类是所有类的父类,下面介绍关于Object类接收引用数据类型:类、数组、接口。

//1.使用Object类接收数组对象
public class Test1 {
	public static void main(String[] args)
	{
		//Object类接收数组对象,是向上转型
		Object obj=new int[] {1,2,3,4,5,6};
		//Object类对象的向下转型
		int[] data=(int[])obj;
		for(int i:data)
		{
			System.out.print(i+" ");
		}
	}
}
输出结果:1 2 3 4 5 6 
//2.Object类接收接口,这是Java中强制要求的,因为接口本身不能继承任何类
interface INew
{
	void fun();
}
class NewA implements INew{
	public void fun() {
		System.out.println("NewA类实现INew接口");
	}
}
public class Test1 {
	public static void main(String[] args)
	{
		//子类向父接口转型
		INew inew1=new NewA();
		//Object类接收接口对象
		Object object=inew1;
		System.out.println(object);
	}
}
输出结果:NewA@70dea4e

总结:Object类真正实现了参数统一,如果一个类希望接收所有的数据类型,就用Object

3.包装类

包装类就是将基本数据类型封装到类中。

3.1 自定义一个int数据类型的包装类

class IntClass
{
	private int data;
	public IntClass(int data)
	{
		this.data=data;
	}
	public int intValue()
	{
		return this.data;
	}
}
//此时的IntClass就相当于int数据类型的包装类
public class Test1 {
	public static void main(String[] args)
	{
		//利用Object类接收自定义包装类的实例化对象
		Object obj=new IntClass(55);
		//向上转型后,进行向下转型
		IntClass data=(IntClass)obj;
		//利用intValue()方法取出基本数据类型int的值
		System.out.println(data.intValue());
	}
}
输出结果:55

总结:1.为什么一定要将基本数据类型包装成一个类对象?实则为了使用Object类进行接收处理,达到参数统一。

          2.Java中的八种基本数据类型如果都按照以上方式提供其对应的包装类的话,存在以下问题:

                   (1)开发中代码重复

                   (2)在进行数值计算的时候,必须使用明确的方法将包装类中的数据取出来才可以计算    

          3.Java为了方便开发,专门提供了包装类的使用,对于包装类的使用,提供了两种类型:

                    (1)对象型(Object类的直接子类):Boolean、Character

                     (2)数值型(Number类的直接子类):Byte、Double、Short、Long、Integer、Float

3.2 装箱与拆箱

装箱:将基本数据类型变成包装类对象,利用每一个包装类提供的构造方法实现装箱处理。

拆箱:将包装类中包装的基本数据类型的值取出,利用Number类中提供的六种方法。

//1.下面以int和Integer举例
public class Test1 {
	public static void main(String[] args)
	{
		Integer data=new Integer(55);  //装箱
		int num=data.intValue();       //拆箱 
		System.out.println(num);
	}
}
输出结果:55

以上采用的是手工的装箱与拆箱,在JDK1.5以后提供了自动拆装箱的机制,从而可以直接利用包装类的对象进行各种数值计算。

//2.自动拆装箱
public class Test1 {
	public static void main(String[] args)
	{
		Integer data=55;   //自动装箱
		int num=data+5;    //自动拆箱
		System.out.println(num);
	}
}
输出结果:60

但在自动拆装箱中存在"=="与"equals()"问题,下面具体介绍是什么问题

//3.手动拆装箱中的"=="与"equals"
public class Test1 {
	public static void main(String[] args)
	{
		Integer data1=new Integer(11);
		Integer data2=new Integer(11);
		System.out.println(data1==data2);
		System.out.println(data1.equals(data2));
	}
}
输出结果:
false
true
总结:"=="进行的是对象的比较,"equals"进行的是数值的比较
//4.自动拆装箱中的"=="和"equals"
public class Test1 {
	public static void main(String[] args)
	{
		Integer data1=11;
		Integer data2=11;
		Integer data3=129;
		Integer data4=129;
		System.out.println(data1==data2);
		System.out.println(data1.equals(data2));
		System.out.println(data3==data4);
		System.out.println(data3.equals(data4));
	}
}
输出结果:
true
true
false
false

解析:对于自动装箱Integer data=数值,数值在-128到127范围内时,Integer对象是在IntegerCache.cache产生,会复用已有对象,这个区间内的Integer值可以直接用"=="判断大小,但在这个区间之外的所有数据,都会在堆上产生,并不会复用已有对象,故在今后的开发之中,对于数值的比较,建议使用equals()方法进行判断。

3.3 字符串与基本数据类型转换

1.String变为Integer(int):public static int parseInt(String s)

2.String变为Double(double):public static double parseDouble(String s)

3.String变为Boolean(boolean):public static boolean parseBoolean(String s)

//1.字符串变为int型
public class Test1 {
	public static void main(String[] args)
	{
		String str="123";
		int num=Integer.parseInt(str);
		System.out.println(num);
	}
}
输出结果:123
//2.字符串变为double型
public class Test1 {
	public static void main(String[] args)
	{
		String str="123";
		double data=Double.parseDouble(str);
		System.out.println(data);
	}
}
输出结果:123.0

但在字符串转换为数字的时候,若字符串中含有非数字,那么转换就会出现错误(NumberFormatExpection)。

//3.字符串中含有非数字时,转换出现错误
public class Test1 {
	public static void main(String[] args)
	{
		String str="123abc";
		double data=Double.parseDouble(str);
		System.out.println(data);
	}
}
出现错误,错误信息如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: "123abc"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
	at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
	at java.lang.Double.parseDouble(Unknown Source)
	at Test1.main(Test1.java:5)
//4.字符串与boolean的转换
public class Test1 {
	public static void main(String[] args)
	{
		String str1="123abc";
		String str2="true";
		boolean data1=Boolean.parseBoolean(str1);
		boolean data2=Boolean.parseBoolean(str2);
		System.out.println(data1);
		System.out.println(data2);
	}
}
输出结果:
false
true

在字符串到boolean转换时,只有当字符串为"true"时,boolean为true,其他任何字符串转换时,boolean都是false。

















本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java的String类、Object类、包装类 的相关文章

  • Java Sqlite Gradle

    我对 gradle 和 java 还很陌生 我有一个使用 sqlite 的项目 它通过 intellij idea 运行良好 但我无法从终端运行它 它会抛出异常 java lang ClassNotFoundException org sq
  • 如何降低圈复杂度?

    我正在开发一个将 RequestDTO 发送到 Web 服务的类 我需要在发送请求之前验证该请求 请求可以从 3 个不同的地方发送 并且每个 请求类型 有不同的验证规则 例如请求1必须有姓名和电话号码 请求2必须有地址等 我有一个 DTO
  • 在 Tomcat 上部署 Java Web 项目,无需 WAR 或 EAR

    我有一个 Java Web 项目 Struts Spring 在我的本地主机上完美运行 我必须将其部署在我的网站上 但虚拟主机提供的 Tomcat Manager 界面显示 由于安全原因 它无法上传 WAR 文件 当联系技术支持时 我被告知
  • 以点作为分隔符分割字符串

    我想知道我是否要在一个字符串上分割字符串 正确的方式 我的代码是 String fn filename split return fn 0 我只需要字符串的第一部分 这就是我返回第一项的原因 我问这个是因为我在 API 中注意到 意味着任何
  • 如何在Mac上使用eclipse安装jetty

    我是一个新手 jetty 和 RESTful API 我想使用 Jetty 创建 REST 服务 并希望将嵌入式 jetty 与 eclipse 一起使用 任何人都可以建议我在 Mac OS 中使用 Eclipse 安装 Jetty Jet
  • 使用 JAXB 编组 LocalDate

    我正在构建一系列链接类 我希望能够将其实例编组到 XML 以便我可以将它们保存到文件中并稍后再次读取它们 目前我使用以下代码作为测试用例 import javax xml bind annotation import javax xml b
  • java中队列的实现

    在 Java 中实现队列是一个非常常见的面试问题 我在网上冲浪 看到了许多实现 他们做了一些奇特的事情 比如实现队列接口和编写自己的addLast and removeFirst 方法 我的问题是我不能使用LinkedList 类并使用其预
  • org.postgresql.util.PSQLException:协议错误。会话设置失败

    我知道这些类型的问题已经存在 但提供的解决方案对我不起作用 在我的应用程序中 没有版本不匹配的黑白驱动程序和 PostgreSQL 服务器 我还没有找到任何其他解决方案 我正在使用 PostgreSQL 服务器 9 4 和 postgres
  • 如果按下 Esc 则中断循环

    我用 JAVA 语言编写了一个程序 它使用 Scanner 类接受来自控制台的输入 现在我想将此功能添加到我的代码中 以便在用户按下 Esc 按钮时存在循环 while 到目前为止 我认为键盘类可以帮助我 但它就像扫描仪一样 我尝试使用事件
  • BigDecimal 的 JPA @Size 注释

    我该如何使用 SizeMySQL 的注释DECIMAL x y 列 我在用着BigDecimal 但是当我尝试包括 Size max它不起作用 这是我的代码 Size max 7 2 Column name weight private B
  • 如何在Gradle中支持多种语言(Java和Scala)的多个项目?

    我正在尝试将过时的 Ant 构建转换为 Gradle 该项目包含约50个Java子项目和10个Scala子项目 Java 项目仅包含 Java Scala 项目仅包含 Scala 每个项目都是由 Java 和 Scala 构建的 这大大减慢
  • Kerberos 缓存票证

    我使用的是 Windows 7 64 位 我创建了一个简单的应用程序来对实现 PrivilegedAction 的类的 run 方法中的文件进行计数 以下是我的 jaas conf 文件 CountFiles com sun securit
  • 更改 RowLayout SWT Java 中元素的顺序

    有没有办法更改在行布局中创建的元素的顺序 我想将其显示在元素中 首先显示 例如 如果我创建 element1 则 element2 element3 element4 我想看到的布局为 元素4 元素3 元素2 元素1 这意味着最后创建的元素
  • 在 netBeans 中运行程序时,字体看起来非常奇怪

    我在我的新 MacBook M1 上设置了 netBeans 和 SceneBuilder 除了运行程序时的字体外 一切正常 它看起来像这样 我不知道为什么 按钮应显示 Click me 标签应显示 Hello 我收到的错误消息是 M rz
  • 无法仅在控制台中启动 androidstudio

    你好 我的问题是下一个 我下载了Android Studio如果我去 路径 android studio bin 我执行studio sh 我收到以下错误 No JDK found Please validate either STUDIO
  • Jenkins 管道和 java.nio.file.* 方法的问题

    我正在尝试使用 java nio file 中的方法在 Jenkins 管道中执行一些基本文件操作 无论代码存在于哪个节点块中 代码都在主节点上执行 在管道中 我已经验证了各个节点块都是正确的 它们唯一地标识了特定的节点 但是 pathEx
  • JPA - 非主键字段上的 @OneToOne 关系不起作用

    我有一个 Spring Data JPA 后端 使用 Hibernate 作为 ORM 实现 这是模型 Person MailConfig id PK uid PK FK Person uid uid Entity
  • Java中的媒体播放器库[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在评估用于在 Java 中播放音频 视频的库 它不需要 100 Java Java 与本机库的绑定
  • 在多线程环境中,Collections.sort 方法有时会抛出 ConcurrentModificationException。列表没有进行结构性修改

    package CollectionsTS import java util ArrayList import java util Collections import java util HashSet import java util
  • 编译时在代码中替换Java静态最终值?

    在java中 假设我有以下内容 fileA java class A public static final int SIZE 100 然后在另一个文件中我使用这个值 fileB java import A class b Object t

随机推荐

  • 神经网络中FLOPs和MACs的计算(基于thop和fvcore.nn)

    以 输入为 1 1 200 3 的张量 卷积取 nn Conv2d 1 64 kernel size 8 1 stride 2 1 padding 0 0 为例 先计算输出的形状 公式为 H上为 200 0 8 2 1 97 W上依然是3
  • Python全栈开发【基础-03】编程语言的分类

    专栏介绍 本专栏为Python全栈开发系列文章 技术包括Python基础 函数 文件 面向对象 网络编程 并发编程 MySQL数据库 HTML JavaScript CSS JQuery bootstrap WSGI Django Flas
  • 阿里巴巴都害怕的区块链电商到底是什么?

    近日 区块链权威机构中国通信工业协会区块链专业委员会 CCIAPCB 发出倡议 联合各界将中共中央政治局10月24日集体学习区块链主席讲话日作为 区块链中国日 此次中央将区块链技术放在了国家战略层面高度上 让区块链一时间成了全民热点 特别是
  • 【python数据挖掘课程】二十七.基于SVM分类器的红酒数据分析

    这是 Python数据挖掘课程 系列文章 前面很多文章都讲解了分类 聚类算法 这篇文章主要讲解SVM分类算法 同时讲解如何读取TXT文件数据并进行数据分析及评价的过程 文章比较基础 希望对你有所帮助 提供些思路 也是自己教学的内容 推荐大家
  • TS装饰器

    一 定义 装饰器本质是一种函数 通过添加标注的方式 对数据 类 方法 属性 参数等 的功能进行增加或者修改 二 使用 准备工作 ts config json文件中 1 基础使用 装饰器名字 例子 function test target a
  • 《塞尔达传说:旷野之息》中设计元素的分析

    塞尔达传说 旷野之息 中设计元素的分析 0 写在前面 关于 塞尔达传说 旷野之息 是否属于中型游戏 检索许多资料后 有一种通识是 塞尔达传说 旷野之息 不属于3A级别游戏 显然也不属于小型游戏 因此我姑且认为它属于中型游戏 这也符合此篇的初
  • crypto-js md5加密和解密

    直接上代码 import CryptoJS from crypto js const encodeFactor zq87dopenf67eg 加密 export function encrypt txt var key CryptoJS e
  • 服务攻防-中间件安全&CVE复现&IIS&Apache&Tomcat&Nginx漏洞复现

    目录 一 导图 二 ISS漏洞 中间件介绍 gt 1 短文件 2 文件解析 3 HTTP SYS 4 cve 2017 7269 三 Nignx漏洞 中间件介绍 gt 1 后缀解析漏洞 2 cve 2013 4547 3 cve 2021
  • openstack平台搭建笔记(容器云)

    openstack平台搭建笔记 容器云 一 根据要求准备好配置环境 节点IP 角色 备注 192 168 100 30 Master Kubernetes 集群 master 节点 Harbor 仓库节点 192 168 100 31 Wo
  • C# 快速写入日志 不卡线程 生产者 消费者模式

    有这样一种场景需求 就是某个方法 对耗时要求很高 但是又要记录日志到数据库便于分析 由于访问数据库基本都要几十毫秒 可在方法里写入BlockingCollection 由另外的线程写入数据库 可以看到 在我的机子上面 1ms写入了43条日志
  • html5 自动化测试工具,五大最佳自动化测试工具

    对更快交付高质量软件 或 快速质量 的需求要求组织以敏捷 持续集成 CI 和DevOps方法论来寻找解决方案 测试自动化是这些方面的重要组成部分 最新的 2018 2019年世界质量报告 表明 测试自动化是实现 快速质量 的最大瓶颈 因为它
  • 四位数显表头设计

    去年帮别人定制了一个四位数显小表头 可以用于测量4 20mA或者0 5V 0 10V输出的的各种传感器 可设置显示范围 上下限报警灯 由于后面更改方案 此方案暂时搁置不用 今天来分享一下软硬件的设计过程 1 硬件设计 1 1电源 电源采用一
  • Flink_06_ProcessAPI(个人总结)

    声明 1 本文为我的个人复习总结 并非那种从零基础开始普及知识 内容详细全面 言辞官方的文章 2 由于是个人总结 所以用最精简的话语来写文章 3 若有错误不当之处 请指出 侧输出流 SideOutput 即分支流 可以用来接收迟到数据 也可
  • SpringBoot实现接口版本控制

    一个系统在上线后会不断迭代更新 需求也会不断变化 有可能接口的参数也会发生变化 如果在原有的参数上直接修改 可能会影响到现有项目的正常运行 这时我们就需要设置不同的版本 这样即使参数发生变化 由于老版本没有变化 因此不会影响上线系统的运行
  • python的UnboundLocalError: local variable 'xxx' referenced before assignment

    From http blog sina com cn s blog 8d3652760101d01p html 一 意思 本地变量xxx引用前没定义 二 错误原因 在于python没有变量的声明 所以它通过一个简单的规则找出变量的范围 如果
  • OPENV接收和发送串口的数据

    import sensor image time from pyb import UART from pyb import Pin Timer LED import re sensor reset sensor set pixformat
  • qt 开发遇到的坑

    1 QString的toString 和toWString 引起的win32位release 下std string的析构崩溃 代码 QString qs std string str qs toStdString const wchar
  • Linux NFS说明,配置及故障分析

    一 NFS服务简介 NFS 是Network File System的缩写 即网络文件系统 一种使用于分散式文件系统的协定 由Sun公司开发 于1984年向外公布 功能是通过网络让不同的机器 不同的操作系统能够彼此分享个别的数据 让应用程序
  • MATLAB:figure的用法

    figure的定义 figure 创建图窗窗口 可以理解为创建一个有画板的窗口 我们在这块画板上绘制 plot 曲线等 figure主要是创建图窗窗口或者切换图窗窗口 figure n 查找到n存在时 将当前窗口切换成n 不存在时创建标识为
  • Java的String类、Object类、包装类

    1 String类 1 1 String类的两种实例化方式 1 直接赋值 String str hello 2 使用构造方法new的形式赋值 String str new String hello 1 2 String类定义的字符串的比较