Java核心类库篇2——lang

Java核心类库篇2——lang

1、Object

该类是所有类的父类,每个类都使用它作为超类,没有任何属性

方法声明功能介绍
Object()使用无参方式构造对象
boolean equals(Object obj)用于判断调用对象是否与参数对象相等。 该方法默认比较两个对象的地址是否相等
int hashCode()用于获取调用对象的哈希码值(内存地址的编号)
String toString()用于获取调用对象的字符串形式 该方法默认返回的字符串为:包名.类名@哈希码值的十六进制
Class getClass()用于返回调用对象执行时的Class实例,反射机制使用
  • equals:与 == 运算符的结果一致,若希望比较两个对象的内容,则需要重写该方法。 若该方法被重写后,则应该重写hashCode方法来保证结果的一致性
  • hashCode:若两个对象调用equals方法相等,则各自调用该方法的结果必须相同,若两个调用对象equals方法不相等,则各自调用该方法的结果应该不相同,为了使得该方法与equals方法保持一致,需要重写该方法
  • toString:为了返回更有意义的数据,需要重写该方法 使用print或println打印引用或字符串拼接引用都会自动调用该方法

2、包装类

基本类型并不具有对象的性质,为了与其他对象“接轨”就出现了包装类型(如我们在使用集合类型Collection时就一定要使用包装类型而非基本类型),它相当于将基本类型“包装起来”,使得它具有了对象的性质,并且为其添加了属性和方法,丰富了基本类型的操作。

基本类型包装类
booleanjava.lang.Boolean
charjava.lang.Character
bytejava.lang.Byte
shortjava.lang.Short
intjava.lang.Integer
longjava.lang.Long
floatjava.lang.Float
doublejava.lang.Double
  • 基本类型的优势:数据存储相对简单,运算效率比较高
  • 包装类的优势:有的容易,比如集合的元素必须是对象类型,满足了java一切皆是对象的思想
  • 声明方式不同,基本类型不适用new关键字,而包装类型需要使用new关键字来在堆中分配存储空间;
  • 存储方式及位置不同,基本类型是直接将变量值存储在堆栈中,而包装类型是将对象放在堆中,然后通过引用来使用;
  • 初始值不同,基本类型的初始值如int为0,boolean为false,而包装类型的初始值为null
  • 使用方式不同,基本类型直接赋值直接使用就好,而包装类型在集合如Collection、Map时会使用到

2.1、包装类的装箱拆箱

public class Test {public static void main(String[] args) {//自动装箱拆箱int a=10;Integer integer=a;int b=integer;System.out.println(a);System.out.println(integer);System.out.println(b);//手动装箱拆箱int a1=10;Integer integer1=new Integer(a1);int b1=integer1.intValue();System.out.println(a);System.out.println(integer);System.out.println(b);}
}

2.2、包装类与字符串的转换

public class Test {public static void main(String[] args) {//解析字符串String a="100";//Java为了提高拆装箱效率,在执行过程中提供了一个缓存区(对象池)【类似于常量数组】//如果传入的参数是,-128<参数<127会直接去缓存查找数据,如果有久直接产生,如果没有就隐式调用new方法产生System.out.println(Integer.parseInt(a));String b="100.2";System.out.println(Float.parseFloat(b));String c="true";System.out.println(Boolean.parseBoolean(c));//转为字符串Integer d=100;System.out.println(d.toString());}
}

3、Math

3.1、属性

属性介绍
Math.PIπ
Math.Ee
public class Test {public static void main(String[] args) {System.out.println("math属性================");System.out.println(Math.PI);System.out.println(Math.E);}
}
math属性================
3.141592653589793
2.718281828459045

3.2、三角函数方法

方法声明功能介绍
public static double sin(double radians)正弦函数
public static double cos(double radians)余弦函数
public static double tan(double radians)正切函数
public static double toRadians(double degree)度转换成弧度
public static double toDegree(double radians)弧度转换成度
public static double asin(double a)反正弦
public static double acos(double a)反余弦

3.2、指数函数方法

方法声明功能介绍
public static double exp(double x)e^x
public static double log(double x)ln(x)
public static double log10(double x)log 10(x)
public static double pow(double a,double b)a^b
public static double sqrt(double x)√x

3.3、取整方法

方法声明功能介绍
public static double ceil(double x)天花板的意思,就是逢余进一
public static double floor(double x)地板的意思,就是逢余舍一
public class Test {public static void main(String[] args) {double a=2.5;System.out.println(Math.ceil(a));System.out.println(Math.floor(a));}
}
3.0
2.0

3.4、min、max、abs方法

方法声明功能介绍
public static int abs(int a)返回一个数的绝对值
public static int min(int a, int b)返回两个数的最小值
public static int max(int a, int b)返回两个数的最大值
public static double random()生成大于等于0.0且小于1.0的double型随机数
public class Test {public static void main(String[] args) {int a=5;int b=9;System.out.println(Math.max(a,b));System.out.println(Math.min(a,b));int c=-9;System.out.println(Math.abs(c));System.out.println(Math.random());}
}
9
5
9
0.30741550461739375

4、String

  • String类被final关键字修饰,意味着String类不能被继承,并且它的成员方法都默认为final方法
  • 字符串一旦创建就不能再修改
  • String类实现了Serializable、CharSequence、 Comparable接口
  • String实例的值是通过字符数组实现字符串存储的

4.1、构造方法

方法声明功能介绍
String str=“hello world!”字面量创建
public String()无参构造方法
public String(String original)用已知的字符串value创建一个String对象
public String(char value[])用字符数组value创建一个String对象
public String(char value[], int offset, int count)用字符数组chars的offset开始的count个字符创建一个String对象
public String(byte bytes[])用byte数组values创建一个String对象

常用的为字面量创建

4.2、字符串长度

方法声明功能介绍
public int length()字符串的长度
public class Test {public static void main(String[] args) {String str="hello world!";System.out.println(str.length());}
}
12

4.3、字符串中指定位置的字符

方法声明功能介绍
public char charAt(int index)字符串中指定位置的字符
public class Test {public static void main(String[] args) {String str="hello world!";System.out.println(str.charAt(1));}
}
e

4.4、提取子串

方法声明功能介绍
public String substring(int beginIndex)从beginIndex位置起,从当前字符串中取出剩余的字符
public String substring(int beginIndex, int endIndex)从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符
public class Test {public static void main(String[] args) {String str="hello world!";System.out.println(str.substring(2));System.out.println(str.substring(2,4));}
}
llo world!
ll

4.5、字符串比较

方法声明功能介绍
public int compareTo(String anotherString)对字符串内容按字典顺序进行大小比较
public int compareToIgnore(String anotherString)同上,忽略大小写
public boolean equals(Object anotherObject)比较当前字符串和参数字符串
public boolean equalsIgnoreCase(String anotherString)比较字符串,忽略大小写
public class Test {public static void main(String[] args) {String str="a";String str1="A";System.out.println(str.compareTo(str1));System.out.println(str.compareToIgnoreCase(str1));System.out.println("===========================");System.out.println(str.equals(str1));System.out.println(str.equalsIgnoreCase(str1));}
}
32
0
===========================
false
true

4.6、字符串连接

方法声明功能介绍
public String concat(String str)将字符串str连接到当前字符串的后面,效果等价于"+"

与直接+一致

4.7、字符串中单个字符查找

方法声明功能介绍
public int indexOf(int ch/String str)查找当前字符串中字符或子串,返回第一次出现的位置
public int indexOf(int ch/String str, int fromIndex)从fromIndex开始查找当前字符串中字符或子串,返回第一次出现的位置
public int lastIndexOf(int ch/String str)倒着查找当前字符串中字符或子串,返回第一次出现的位置
public int lastIndexOf(int ch/String str, int fromIndex)倒着从fromIndex开始查找当前字符串中字符或子串,返回第一次出现的位置
public class Test {public static void main(String[] args) {String str="hello world!";System.out.println(str.indexOf('o'));System.out.println(str.lastIndexOf('o'));}
}
4
7

4.8、字符串大小写转换

方法声明功能介绍
public String toLowerCase()所有字符转换成小写
public String toUpperCase()所有字符转换成大写
public class Test {public static void main(String[] args) {String str="Hello World!";System.out.println(str.toUpperCase());System.out.println(str.toLowerCase());}
}
HELLO WORLD!
hello world!

4.9、字符串中字符的替换

方法声明功能介绍
public String replace(char oldChar, char newChar)用字符newChar替换当前字符串中所有的oldChar字符
public String replaceFirst(String regex, String replacement)用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串
public String replaceAll(String regex, String replacement)用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串
public class Test {public static void main(String[] args) {String str="Hello World!";System.out.println(str.replace('o','v'));//正则表达式会独立开新篇}
}
Hellv Wvrld!

4.10、其他类方法

方法声明功能介绍
public String trim()截去字符串两端的空格,但对于中间的空格不处理
public boolean startsWith(String prefix)判断当前字符串是否以指定字符串开始
public boolean endWith(String suffix)判断当前字符串是否以指定字符串结束
public contains(String str)判断参数s是否被包含在字符串中
public String[] split(String str)将str作为分隔符进行字符串分解
public class Test {public static void main(String[] args) {String str=" Hello World!    ";System.out.println(str.trim());System.out.println(str.contains("ello"));System.out.println(str.startsWith(" Hello"));System.out.println(str.endsWith("World!    "));}
}
Hello World!
true
true
true

4.11、字符串转为基本类型

方法声明功能介绍
public static byte parseByte(String s)String转为byte
public static short parseShort(String s)String转为short
public static short parseInt(String s)String转为int
public static long parseLong(String s)String转为long
public static float parseFloat(String s)String转为float
public static double parseDouble(String s)String转为double

4.12、基本类型转为字符串

方法声明功能介绍
public static String valueOf(char data[])byte转String
public static String valueOf(char data[], int offset, int count)char[]转String
public static String valueOf(boolean b)boolean转String
public static String valueOf(char c)char转String
public static String valueOf(int i)int转String
public static String valueOf(long l)long转String
public static String valueOf(float f)float转String
public static String valueOf(double d)double转String

4.13、StringBuffer

  • StringBuffer类的对象能够被多次的修改,并且不产生新的未使用对象

  • 属于线程安全的类,效率比较低

4.13.1、构造函数

方法声明功能介绍
public StringBuffer()构造一个字符串缓冲区,其中没有字符,初始容量为16个字符
public StringBuffer(CharSequence seq)构造一个包含与指定字符相同的字符串缓冲区
public StringBuffer(int capacity)构造一个字符串缓冲区,其中没有字符,但是包含指定的初始容量capacity
public StringBuffer(String str)构造一个指定字符串内容的字符串缓冲区

4.13.2、方法

方法声明功能介绍
public StringBuffer append(String str)将指定的字符串追加到此字符序列
public StringBuffer append(StringBuffer sb)将指定的内容附加StringBuffer到此序列
public int capacity()返回当前容量
public char charAt(int index)返回char指定索引处的此序列中的值
public StringBuffer delete(int start, int end)删除此序列的子字符串中的字符
public StringBuffer deleteCharAt(int index)删除指定位置字符
public int indexOf(String str)指定子字符串第一次出现的字符串中的索引
public StringBuffer insert(int offset, String str)将字符串插入此字符序列
public int length()返回该字符串的长度
public StringBuffer replace(int start, int end, String str)用指定的字符替换此序列的子字符串中的字符String
public StringBuffer reverse()此字符序列的反向替换
public void setCharAt(int index, char ch)指定索引处的字符设置为ch
public void setLength(int newLength)设置字符序列的长度
public String toString()此序列中数据的字符串

4.14、StringBuilder

  • StringBuilder类的对象能够被多次的修改,并且不产生新的未使用对象
  • 属于非线程安全的类,效率比较高

4.14.1、构造方法

方法声明功能介绍
public StringBuilder()构造一个没有字符的字符串构建器,初始容量为16个字符
public StringBuilder(CharSequence seq)构造一个包含与指定的相同字符的字符串构建器
public StringBuilder(int capacity)造一个没有字符的字符串构建器,由构capacity参数指定的初始容量
public StringBuilder(String str)构造一个初始化为指定字符串内容的字符串构建器

4.14.2、方法

方法声明功能介绍
public StringBuilder append(String str)将指定的字符串附加到此字符序列
public StringBuilder append(StringBuffer sb)将指定 StringBuffer追加到这个序列
public int capacity()当前容量
public char charAt(int index)获取指定索引位的字符
public StringBuilder delete(int start, int end)删除指定索引间的字符串
public StringBuilde deleteCharAt(int index)删除指定位置的字符
public int indexOf(String str)指定子字符串第一次出现的字符串内的索引
public StringBuilder insert(int offset, String str)将字符串插入到此字符序列中
public int length()返回长度
public StringBuilder replace(int start, int end, String str)用指定的String中的字符替换此序列的子字符串中的String
public StringBuilder reverse()字符串顺序颠倒
public void setCharAt(int index, char ch)指定索引处的字符设置为ch
public void setLength(int newLength)设置字符序列的长度
public String substring(int start)返回指定位置后的字符串
public String toString()此顺序中的数据的字符串

5、System

主要用于获取系统的属性数据

5.1、属性

属性介绍
PrintStreamerr标准错误输出流
InputStreamin标准输入流
PrintStreamout标准输出流

5.2、方法

方法声明功能介绍
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)将指定源数组从指定位置复制到目标数组的指定位置
public static long currentTimeMillis()返回当前时间(以毫秒为单位)
static void exit(int status)终止当前运行的Java虚拟机
public static void gc()提醒运行垃圾回收器
public static String getenv(String name)获取指定环境变量的值
static Properties getProperties()获取当前的系统属性
static String getProperty(String key)获取指定键指示的系统属性
static String lineSeparator()系统相关的行分隔符字符串
static void setProperties(Properties props)将系统属性设置为Properties参数
static void setSecurityManager(SecurityManager s)设置系统安全性
public class Test {public static void main(String[] args) {System.out.println(System.currentTimeMillis());System.out.println(System.getenv());}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/557519.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java核心类库篇3——util

Java核心类库篇3——util 1、Date 用于描述特定的瞬间&#xff0c;也就是年月日时分秒&#xff0c;可以精确到毫秒 1.1、构造方法 方法声明功能介绍public Date()获取当前时间表示的date对象public Date(long date)根据给定的毫秒值创建date对象 public class Test {public…

linux怎么重装ssh服务器,Linux平台下安装SSH

什么是SSH&#xff1f;Secure Shell(缩写为SSH)&#xff0c;由IETF的网络工作小组(Network Working Group)所制定&#xff1b;SSH为一项创建在应用层和传输层基础上的安全协议&#xff0c;为计算机上的Shell(壳层)提供安全的传输和使用环境。传统的网络服务程序&#xff0c;如r…

Java核心类库篇4——集合

Java核心类库篇4——集合 1、集合的意义 记录单个数据内容时&#xff0c;则声明一个变量记录多个类型相同的数据内容时&#xff0c;声明一个一维数组记录多个类型不同的数据内容时&#xff0c;则创建一个对象记录多个类型相同的对象数据时&#xff0c;创建一个对象数组记录多…

计划任务文件 linux,Linux之任务计划

一、单次任务计划二、周期性任务计划一、单次任务计划命令&#xff1a;batch&#xff1a;系统空闲时自动执行&#xff0c;不常用at&#xff1a;可以使用相对时间、绝对时间或模糊时间&#xff0c;例如相对时间&#xff1a;at now3min&#xff0c;表示3分钟后执行绝对时间&#…

Java核心类库篇5——异常

Java核心类库篇5——异常 java.lang.Throwable类是Java语言中错误(Error)和异常(Exception)的超类其中Error类主要用于描述Java虚拟机无法解决的严重错误&#xff0c;通常无法编码解决&#xff0c;如&#xff1a;JVM挂掉了 等其中Exception类主要用于描述因编程错误或偶然外在…

linux2019内核版本发布,求问Linux最新内核版本以及发布日期。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼具有时效性的东西&#xff0c;百度能查处什么鬼来。mainline:4.3-rc32015-09-27[tar.xz][pgp][patch][view diff][browse]stable:4.2.22015-09-29[tar.xz][pgp][patch][inc. patch][view diff][browse][changelog]longterm:4.1.920…

Java核心类库篇6——IO

Java核心类库篇6——IO 1、File 1.1、构造方法 方法声明功能介绍public File(File parent, String child)从父抽象路径名和子路径名字符串创建新的 File实例public File(String pathname)通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例public File(String pa…

linux搭建web服务器原理,【LINUX】linux搭建web服务器

linux httpd假设服务器地址为192.168.80.20/241.将准备安装的httpd软件包共享给everyone&#xff0c;(1)在linux上mount.cifs //真机IP地址/共享文件夹名/media /ls /meidia/查看tar xjvf httpd-2.4.10.tar.bz2 -C /usr/src解压至/usr/src下下面两个插件是httpd2.4以…

Java核心类库篇7——多线程

Java核心类库篇7——多线程 1、程序、进程和线程 程序 - 数据结构 算法&#xff0c;主要指存放在硬盘上的可执行文件进程 - 主要指运行在内存中的可执行文件线程就是进程内部的程序流 操作系统内部支持多 进程的&#xff0c;而每个进程的内部又是支持多线程的 2、线程的创…

Java核心类库篇8——网络编程

Java核心类库篇8——网络编程 1、七层网络模型 OSI&#xff08;Open System Interconnect&#xff09;&#xff0c;即开放式系统互联&#xff0c;是ISO&#xff08;国际标准化组织&#xff09;组织在1985 年研究的网络互连模型。 当发送数据时&#xff0c;需要对发送的内容按…

linux网站如何添加swf支持,linux下安装swftools和openOffice

1.openOffice下载地址&#xff1a;http://download.openoffice.org/all_rc.html#untested-full 下载 Linux 32-bit Intel rpm 包 版本自选 2.安装openOffice 1】 tar -zxvf OOo_3.2.1_Linux_x86_install-rpm-wJRE_zh-CN.tar.gz 2】 cd OOO320_m18_native_packed-1_zh-CN1.openO…

Java番外篇1——正则表达式

Java番外篇1——正则表达式 1、什么是正则表达式 正则表达式定义了字符串的模式正则表达式可以用来搜索、编辑或处理文本正则表达式并不仅限于某一种语言&#xff0c;但是在每种语言中有细微的差别 2、正则表达式规则 2.1、普通字符 普通字符包括没有显式指定为元字符的所…

linux 1号硬盘不能用,linux 挂载硬盘的疑问 : IDE1 上的接口无法使用.

状况说明:我将在linux系统上挂载多块硬盘(目前是redhat9).我通过3块硬盘试验.问题出现:无论如何链接 IDE1 上的硬盘, /dev/hdc 都无法 mount.数据:1. 使用 fdisk -l : 不会显示接到 IDE1 上的硬盘(目前只试验了在 IDE1 上接 1个硬盘,用 master 端口).2. 使用 fdisk /dev/hdc : …

Java番外篇2——jdk8新特性

Java番外篇2——jdk8新特性 1、Lambda 1.1、无参无返回值 public class Test {interface Print{void print();}public static void main(String[] args) { // Print printnew Print() { // Override // public void print() { // …

linux同花顺乱码,打开同花顺软件全是问号

官方答案&#xff1a;字体库字体乱码【原因分析】&#xff1a;系统字体缺失&#xff0c;损坏。【解决方案】方案一&#xff1a;使用360电脑门诊进行修复1.打开【360安全卫士】—【电脑专家】搜索乱码&#xff0c;然后会弹出如下六个解决方案&#xff0c;根据当前计算机的故障现…

Java番外篇3——线程池

Java番外篇3——线程池 1、多线程产生的问题 多次创建并销毁线程。而创建并销毁线程的过程势必会消耗内存 2、线程池 降低系统资源消耗&#xff0c;通过重用已存在的线程&#xff0c;降低线程创建和销毁造成的消耗提高系统响应速度&#xff0c;当有任务到达时&#xff0c;通…

嵌入式linux组件,嵌入式Linux系统的几大组件!

原标题&#xff1a;嵌入式Linux系统的几大组件&#xff01;本文概述了Linux系统的几大组件&#xff0c;描述了这些组件之间的关系。文章解释了术语&#xff0c;并描述看似很基础的细节。每个Linux系统都有许多主要组件。其中一个组件(引导加载程序)从技术上讲是Linux之外的&…

linux iptables找不到,centos /etc/sysconfig/下找不到iptables文件解决方法

本想做些防火墙策略。防火墙策略都是写在/etc/sysconfig/iptables文件里面的。可我发现我也没有这个文件。[rootxiaohuai /]# cd /etc/sysconfig/[rootxiaohuai sysconfig]# lsatd firstboot irqbalance network-scripts rhn sysstatauditd grub kdump ntpd rngd sysstat.iocon…

Java番外篇4——BigInteger与BigDecimal

Java番外篇4——BigInteger与BigDecimal 为了解决大数运算的问题 操作整型&#xff1a;BigInteger操作小数&#xff1a;BigDecimal 1、BigInteger 方法声明功能介绍public BigInteger abs()返回大整数的绝对值public BigInteger add(BigInteger val)返回两个大整数的和publ…

linux cd 命令案例,15个关于Linux的‘cd’命令的练习例子

命令名称&#xff1a;cd代表&#xff1a;切换目录使用平台&#xff1a;所有Linux发行版本执行方式&#xff1a;命令行权限&#xff1a;访问自己的目录或者其余指定目录级别&#xff1a;基础/初学者1.从当前目录切换到/usr/local avitecmint:~$ cd /usr/local avitecmint:/usr/l…