java常用函数

分类: 外勤365在线登录 时间: 2026-01-18 22:28:47 作者: admin 阅读: 5274
java常用函数

object类

java中所有的类共同的父类

构造方法

object()无参构造方法

成员方法

public int hashCode() //返回对象的哈希值

public final Class getClass() //获取一个类对应的class对象

public String toString() //输出一个对象 实际上输出的是对象调用的toString() 方法

public Boolean equals(Object obj)

protected void finalize() //用于垃圾回收

protected Object clone() //拷贝为浅拷贝

一个标准类的最终写法

成员变量的私有化

构造方法(无参/所有参数)

get 和set 方法

toString()方法

Scanner 类

其中 有一个作用就是获取键盘上的符号

构造方法

Scanneer(InputStream source) 构造一个新的Scanner 产生从指定输入流扫描的值

next() //获取字符串

public String nextLine()//获取字符串 可以接受换行符

nextInt() // 接受整数

public boolean hashNextInt() //判断管道中的下一个数据是否为整数

String

字符串 由若干个字符组成的字符序列 整体叫做字符串

特点

一旦被创建,就不能被改变

java 程序中所有字符串都被实现为此类的实例。

因为String对象是不可变的,他们可以被共享(常量池)

字符串可以被看做一个字符数组

string中的构造方法

public String() //创建空的字符串

public String(byte[] bytes)//

public String(byte[] bytes,int offset,int length)

public String(char[] value)

public String(char[] value,int offset,int count)

public String(String original)

string中判断函数

boolean equals(Object obj) // 判断两个字符串内容是相同

boolean equalsIgnoreCase(String str)//判断两个字符串内容是否相同,忽略大小写

boolean contains(String str)//判断一个字符串是否包含另一个字符串

boolean startsWith(String str)//判断字符串是否以某个字符串开头

boolean endsWith(String str)//判断字符串是否以某个字符串结束

boolean isEmpty()//判断字符串是否为空字符串

String类中的获取功能的函数

int length() //获取字符串中字符的个数

char charAt(int index)//根据索引获取索引对应的字符

int indexOf(int ch)//判断字符在字符串中的第一次出现的索引

int indexOf(String str)//获取小字符串在大字符中第一次出现的位置 返回小字符第一个字符的位置索引

int indexOf(int ch,int fromIndex)////从fromIndex位置开始找,如果找到返回该字符在大字符串中的位置索引

int indexOf(String str,int fromIndex)

String substring(int start) //字符串截取 从start索引位置截取 一直到末尾

String substring(int start,int end)//子串开始于指定beginIndex并延伸到字符索引endIndex-1

字符串的转化功能

byte[] getBytes() //将字符串转化为字节数组

char[] toCharArray()//将字符串转化为字符数组

static String valueOf(char[] chs)// 将字符数组转化为字符串

static String valueOf(int i)//将数值转化为字符串

String toLowerCase()//转小写

String toUpperCase()//转大写

String concat(String str)//进行字符串的拼接

String其他函数

//替换功能

String replace(char old, char new)

String replace(String old,String new)

//去除字符串两端的空格

String trim()

//按照字典顺序比较两个字符串

int comperTo(String str)

int cpmperTOIgnoreCase(String str)

StringBuffer类

可变的字符序列,我们可以理解为一个可变的字符串,线程安全

构造方法

public StringBuffer()//创建一个空的StringBuffer容器

public StringBuffer(int capacity)//创建指定大小容量的容器

public StringBuffer(String str)//创建StrinBuffer的同时, 内部包含一个初始字符串

//添加功能

public StringBuffer append(String str)

//删除功能

public StringBuffer deleteCharAt(int index)

public StringBuffer delete(int start, int end)

//替换功能

public StringBuffer replace(int start, int end, String str)

//反转功能

public StringBuffer reverse()

//截取功能

public String subString(int start)

public String subString(int start, int end)

StringBUffer 和stirng 的相互转换

可以需要用到另一种类中的方法,来方便我们的需求

方法传参

//Strng --> StringBuffer

StringBuffer sb= new StringBuffer(s1); // 使用StringBuffer的构造方法

//StringBuffer --> String

sb.toString() //使用toString()方法

参数传递问题: StringBuffer作为参数传递; 哪一个StringBuffer 对象调用方法,哪一个就会改变。

Arrays类

该类中没有构造方法 但有很多静态方法,这种类我们称为工具类

//静态成员方法

public static Strign toString(int[] a) //将数组以字符串的形式返回

public static void sort(int [] a) //快速排序,结果是升序

public static int binarySearch(int [] a, int key)//二分查找 返回查找的索引

包装类

# java中为了扩展每一个 基本数据类型的功能,针对每一个基本数据类型都提供一个对应的类,这些类统称为把包装类

byte Byte

short Short

int Integer

long Long

float Float

double Double

boolean Boolean

char Character

//Integer 中的构造方法

public Integer(int value)

public Integer(String s)

Integer i=100;//自动装箱

Integer i= new Integer("100")//自动装箱

System.out.print(i+1)//自动拆箱

//Integer 中的方法

public int intValue()

public static int parseInt(String s)

public static String toString(int i)

public static Integer valueOf(int i)

public static Integer valueOf(String s)

String s = Integer.toBinaryString(20); //将十进制数值转为二进制

//String -> int

int i=Integer.parseInt(String s)

//int -> String

String s1=Integer.toString(200)

//int -> Integer

Integer i1=Integer.valueOf(int i)

//String -> Integer

Integer i2=Integer.valuOf(String s)

//Integer -> Int

Integer 14=100;

int i5 = i4.intValue();

//Character 中的方法

public static boolean isUpperCase(char ch)

public static boolean isLowerCase(char ch)

public static boolean isDigit(char ch) //判读一个字符是否为数字

public static char toUpperCase(char ch) //转大写

public static char toLowerCase(char ch) //转小写

Random类

Math.random() [0.0,1.0]

//产生随机整数

Random random= new Random()

random.next() //产生随机整数

Date类

// Date类 和日期有关的内容

//SimpleDateFormat 和日期格式化相关的函数

构造方法

public Date() //将成程序运行到此刻,此刻的时间

public Data(long data) //将指定的时间戳转成对应的时间

构造方法

public SimpleDateFormat(String pattern) yyyy年MM月dd日 HH时mm分ss秒

//将一个时间戳转成指定日期格式

SimpleDateFormat sdf= new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒")

Date date = new Date(1709535166924L)

String res1=sdf.format(date); //将Date类型的值格式化为我们目标的形式

相关文章

cheat的中文翻译
拍摄服装时最常用的三种方法
共享单车收费标准