Object类的概述:
* A:Object类概述
    * 类层次结构的根类
    * 所有类都直接或者间接的继承自该类
* B:构造方法
    * public Object()
    * 子类的构造方法默认访问的是父类的无参构造方法

Object类的hashCode()方法

* public int hashCode()
    * a:返回该对象的哈希码值。默认情况下,该方法会根据对象的地址来计算。
    * b:不同对象的,hashCode()一般来说不会相同。但是,同一个对象的hashCode()值肯定相同

public class Demo_HashCode {
public static void main(String[] args) {
Object object1 = new Object();
int hashCode = object1.hashCode(); System.out.println(hashCode);
}
}

Object类的getClass()方法
    * public final Class getClass()
    * a:返回此 Object 的运行时类。
    * b:可以通过Class类中的一个方法,获取对象的真实类的全名称。    
        * public String getName()

public class Demo_GetClass {

    public static void main(String[] args) {
Person person = new Person(); Class class1 = person.getClass(); // 获取该对象的字节码文件
String name = class1.getName(); // 获取名称
System.out.println(name); // com.fly.bean.Person } }

Object类的toString()方法
    * public String toString()
    * a:返回该对象的字符串表示。
*
        
        public Stirng toString() {
            return name + "," + age;
        }
    * b:它的值等于:
        * getClass().getName() + "@" + Integer.toHexString(hashCode())
    * c:由于默认情况下的数据对我们来说没有意义,一般建议重写该方法。

public class Demo_ToString {
public static void main(String[] args) {
/**
* public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode ());
}
*/
Person person = new Person("张三",18);
// String string = person.toString();
// System.out.println(string); // com.fly.bean.Person@15db9742 System.out.println(person.toString());
System.out.println(person); // 如果直接打印对象的引用,会默认调用toString方 法
}
}

Object类的equals()方法
    * a:指示其他某个对象是否与此对象“相等”。
    * b:默认情况下比较的是对象的引用是否相同。
    * c:由于比较对象的引用没有意义,一般建议重写该方法。

public class Demo_Equals {
public static void main(String[] args) {
/*
public boolean equals(Object obj) {
return (this == obj);
}
Object中的equals方法是比较对象的地址值
开发中通常比较的是对象的属性值,相同属性是同一个对象
* */
Person person1 = new Person("张三",10);
Person person2 = new Person("李四",11);
Person person3 = new Person("李四",11); boolean b = person1.equals(person2); // 比较两个对象是否相等
System.out.println(b); //false System.out.println(person2.equals(person3)); //true // 重写之后比较的是对象 的属性值
}
}

==号和equals方法的区别
    ==是一个比较运算符号,既可以比较基本数据类型,也可以比较引用数据类型,基本数据类型比较的是

值,引用数据类型比较的是地址值
    equals方法是一个方法,只能比较引用数据类型,所有的对象都会继承Object类中的方法,如果没有重

写Object类中的equals方法,equals方法和==号比较引用数据类型无区别,重写后的equals方法比较的是

对象中的属性
Scanner:

import java.util.Scanner;

public class Demo1_Scanner {

    public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// int i = scanner.nextInt();
// System.out.println(i); //输入不是int类型 会抛出 InputMismatchException
if (scanner.hasNextInt()) {
int i = scanner.nextInt();
System.out.println(i);
}else {
System.out.println("你输入的类型有误");
}
} }
import java.util.Scanner;

public class Demo2_Scanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
System.out.println(string); // 以\r\n作为结束标志
}
}
public class Demo1_String {

    public static void main(String[] args) {
String string = "abc"; // 字符串字面值"abc"也可以看成是一个字符串对象
string = "def"; // 字符串是常量,一旦被赋值,就不能被改变
// 此时的"abc" 已经变为垃圾值
System.out.println(string); // 常见的构造方法
String string2 = new String(); // 空构造
System.out.println(string2); byte[] bytes = {97,98,99};
String string3 = new String(bytes); // 把字节数组转成字符串
System.out.println(string3); // abc String string4 = new String(bytes, 1, 2); // 把字节数组的一部分转成字符串, // String(byte[] bytes,int index,int length)
System.out.println(string4); // bc char[] value = {'a','b','c'};
String string5 = new String(value); // 把字符数组转成字符串
System.out.println(string5); // abc } }

String类的常见面试题:

* 1.判断定义为String类型的s1和s2是否相等
* String s1 = "abc";
* String s2 = "abc";
* System.out.println(s1 == s2); // true
* System.out.println(s1.equals(s2)); // true
* 2.下面这句话在内存中创建了几个对象? 2个,一个在常量池,一个在堆内存中
* String s1 = new String("abc");
* 3.判断定义为String类型的s1和s2是否相等
* String s1 = new String("abc");
* String s2 = "abc";
* System.out.println(s1 == s2); //false ,地址不相同
* System.out.println(s1.equals(s2)); //true
* 4.判断定义为String类型的s1和s2是否相等
* String s1 = "a" + "b" + "c";
* String s2 = "abc";
* System.out.println(s1 == s2); // true,java的常量优化机制
* System.out.println(s1.equals(s2));// true
* 5.判断定义为String类型的s1和s2是否相等
* String s1 = "ab";
* String s2 = "abc"; // 常量池
* String s3 = s1 + "c"; // 指向堆内存的toString中
* System.out.println(s3 == s2); //false
* System.out.println(s3.equals(s2)); // true

String类的判断功能:

    * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。

"" 与null:

        /*
* ""是字符串常量,同时时一个String类的对象
* null是空常量,不能调用任何方法,否则或出现空指针异常,可以给任意的引用 数据类型赋值
* */
String string = "";
String string2 = null; System.out.println(string.isEmpty()); // true
// System.out.println(string2.isEmpty()); // NullPointerException

String类的获取功能:

/*String类的获取功能
* int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现 处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一 次出现处的索引。
* lastIndexOf
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。顾头 不顾尾*/ public class Demo2_StringMethod { public static void main(String[] args) {
// 获取字符串的长度
String string = "fly";
System.out.println(string.length()); //
String string2 = "飞?";
System.out.println(string2.length()); // 2
// 获取指定索引位置的字符
char c = string2.charAt(0);
System.out.println(c); //飞
//返回指定字符在此字符串中第一次出现处的索引,找不到返回-1
int i = string.indexOf('l');
System.out.println(i); // } }

String的转换功能:
    * byte[] getBytes():把字符串转换为字节数组。
    * char[] toCharArray():把字符串转换为字符数组。
    * static String valueOf(char[] chs):把字符数组转成字符串。
    * static String valueOf(int i):把int类型的数据转成字符串。
        * 注意:String类的valueOf方法可以把任意类型的数据转成字符串

* String toLowerCase():把字符串转成小写。(了解)
    * String toUpperCase():把字符串转成大写。
    * String concat(String str):把字符串拼接。
    // gbk码表,一个中文代表两个字节,第一个字节肯定是负数

String类的其他功能
* A:String的替换功能   找不到不替换
    * String replace(char old,char new)
    * String replace(String old,String new)
* B:String的去除字符串两空格及案例演示
    * String trim()
* C:String的按字典顺序比较两个字符串
    * int compareTo(String str) 比较的是Unicode码表
    * int compareToIgnoreCase(String str) 不区分大小写

import java.util.Scanner;

public class Test2 {

    public static void main(String[] args) {
/*
* 输入的字符串反转
* */
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串");
String string = scanner.nextLine(); String s = "";
char[] c = string.toCharArray(); // 将字符串转换为字符数组
for (int i = c.length-1; i >= 0; i--) {
s =s + c[i];
}
System.out.println(s);
} }
public class Demo1_StringBuffer {

    public static void main(String[] args) {
/*
* StringBuffer的构造方法的使用
* */
StringBuffer sb = new StringBuffer();
System.err.println(sb.length()); //0 容器的字符个数,实际值
System.out.println(sb.capacity()); //16 容器的初始容量,理论值 StringBuffer sb2 = new StringBuffer(10);
System.out.println(sb2.length()); //
System.out.println(sb2.capacity()); // StringBuffer sb3 = new StringBuffer("fly");
System.out.println(sb3.length()); //
System.out.println(sb3.capacity()); //19 16+3
}
public class Demo2_StringBuffer {

    public static void main(String[] args) {
StringBuffer sBuffer = new StringBuffer();// StringBuffer是字符串缓冲区, new在堆内存创建一个对象
StringBuffer sb = sBuffer.append(true); // 不会创建对象,在不断向原缓冲区 添加字符
StringBuffer sb2 = sBuffer.append("fly");
StringBuffer sb3 = sBuffer.append(123); System.out.println(sb); // truefly123 StringBuffer类重写了toString方法
System.out.println(sb2); // truefly123
System.out.println(sb3); // truefly123
} }
public class Demo3_StringBuffer {
public static void main(String[] args) {
// sbDelete(); // 删除功能
/* StringBuffer sbBuffer = new StringBuffer("hello");
sbBuffer.replace(0, sbBuffer.length(), "hi");
System.out.println(sbBuffer); //hi sbBuffer.reverse(); // 反转
System.out.println(sbBuffer); //ih
*/
StringBuffer sBuffer2 = new StringBuffer("hello world");
// String str = sBuffer2.substring(6); // 截取
// System.out.println(str); // world String str2 = sBuffer2.substring(0, 5);
System.out.println(str2); // hello } private static void sbDelete() {
StringBuffer sBuffer = new StringBuffer();
// sBuffer.deleteCharAt(5);
// System.out.println(sBuffer); // StringIndexOutOfBoundsException 删除不存在 的
sBuffer.append("fly");
// sBuffer.deleteCharAt(2);
// System.out.println(sBuffer); // fl sBuffer.delete(0, sBuffer.length()); // 清空缓冲区
System.out.println(sBuffer);
}
}
public class Demo4_StringBuffer {

    public static void main(String[] args) {
// String转为StringBuffer
StringBuffer sBuffer = new StringBuffer("fly");
System.out.println(sBuffer); StringBuffer sBuffer2 = new StringBuffer();
sBuffer2.append("fly");
System.out.println(sBuffer2);
// StringBuffer转为String
StringBuffer builder = new StringBuffer("fly");
String string = new String(builder);
System.out.println(string); String string2 = builder.toString();
System.out.println(string2); String string3 = builder.substring(0, builder.length());
System.out.println(string3);
} }

StringBuffer与StringBuilder
每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无

需分配新的内部缓冲区数组。如果内部缓冲区溢出,则此容量自动增大。从 JDK 5 开始,为该类补充了

一个单个线程使用的等价类,即 StringBuilder。与该类相比,通常应该优先使用 StringBuilder 类,

因为它支持所有相同的操作,但由于它不执行同步,所以速度更快。

public class Demo5_StringBuffer {

    public static void main(String[] args) {
String string = "hello";
System.out.println(string); // hello
change(string);
System.out.println(string); // hello StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("hello");
System.out.println(stringBuffer); //hello
change(stringBuffer);
System.out.println(stringBuffer); //helloworld
} private static void change(StringBuffer stringBuffer) {
stringBuffer.append("world");
} private static void change(String string) {
string += "world"; // String类虽然是引用数据类型,但当作参数传递时和基本数 据类型是一样的
} }

排序:

public class Demo1_Array {

    public static void main(String[] args) {
int[] arr = {23,69,70,57,15}; // bubbleSort(arr);
selectSort(arr);
stringPrint(arr);
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for(int i = 0;i < arr.length - 1;i++) {
for (int j = 0; j < arr.length -1 -i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
//选择排序
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
} public static void stringPrint(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
} }

二分查找:

public class Demo2_Array {

    public static void main(String[] args) {
// 二分查找
// 前提必须是有序的
int[] arr = {11,22,33,44,55,66,77};
System.out.println(getIndex(arr, 55)); //
System.out.println(getIndex(arr, 88)); //-1
}
public static int getIndex(int[] arr,int value) {
int min = 0;
int max = arr.length - 1;
int mid = (min + max) / 2; while(arr[mid] != value) {
if(arr[mid] > value) {
max = mid - 1;
}else if(arr[mid] < value) {
min = mid + 1;
}
mid = (min + max) / 2;
if(min > max) {
return -1;
}
}
return mid;
} }

Arrays类的使用:

import java.util.Arrays;

public class Demo3_Arrays {
// Arrays类的使用
public static void main(String[] args) {
int[] arr = {33,22,55,11,44};
// 数组转字符串
System.out.println(Arrays.toString(arr)); //[33, 22, 55, 11, 44] // 排序
// 底层用的快速排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [11, 22, 33, 44, 55] int[] arr2 = {11,22,33,44,55,66,77};
// 二分查找
// 有序的,不能有相同的
System.out.println(Arrays.binarySearch(arr2, 22)); //
System.out.println(Arrays.binarySearch(arr2, 77)); //
System.out.println(Arrays.binarySearch(arr2, 12)); //-2
System.out.println(Arrays.binarySearch(arr2, 99)); //-8 找不到返回 应在的 位置负数后-1
}
}
/*基本类型和包装类的对应:
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean*/ public class Demo1_Integer { public static void main(String[] args) {
// 进制
System.out.println(Integer.toBinaryString(60)); //
System.out.println(Integer.toOctalString(60)); //
System.out.println(Integer.toHexString(60)); //3c
//int 类型能够表示的最大值,最小值
System.out.println(Integer.MAX_VALUE); //
System.out.println(Integer.MIN_VALUE); // -2147483648
//数字
Integer integer = new Integer(100);
System.out.println(integer); //100
//数字字符串
Integer integer2 = new Integer("100");
System.out.println(integer2); //100 // int--String
int i = 100;
String string = i + ""; // 方式1 //推荐
String string2 = String.valueOf(i); // 方式2 //推荐
Integer integer3 = new Integer(i); // 方式3
String string3 = integer3.toString();
String string4 = Integer.toString(i); // 方式4 // String--int
String string5 = "200";
Integer integer4 = Integer.parseInt(string5); // 方式1 //推荐
Integer integer5 = new Integer(string5); // 方式2
int i5 = integer5.intValue(); // parseXX方法
// Character没有,字符串到字符的转换通过toCharArray()就可以把字符串转换为字 符数组 } }
public class Demo4_JDK5 {

    /**
* * A:JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
* B:案例演示
* JDK5的新特性自动装箱和拆箱 * Integer ii = 100;
* ii += 200;
* C:注意事项
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。
*/
public static void main(String[] args) {
// int x = 100;
// Integer i1 = new Integer(x); //将基本数据类型包装成对象, 装箱
//
// int y = i1.intValue(); //将对象转换为基本 数据类型,拆箱 Integer i2 = 100; //自动装箱, 把基本数据类型转换成对象
int z = i2 + 200; //自动拆箱, 把对象转换为基本数据类型
System.out.println(z); // Integer i3 = null;
// int a = i3 + 100; //底层用i3 调用intValue,但是i3是null,null调用方法就会出现
// System.out.println(a); //空指针异常 java.lang.NullPointerException
} }
public class Demo5_Integer {

    public static void main(String[] args) {
Integer i1 = new Integer(97);
Integer i2 = new Integer(97);
System.out.println(i1 == i2); //false
System.out.println(i1.equals(i2)); //true
System.out.println("-----------"); Integer i3 = new Integer(197);
Integer i4 = new Integer(197);
System.out.println(i3 == i4); //false
System.out.println(i3.equals(i4)); //true
System.out.println("-----------"); Integer i5 = 127;
Integer i6 = 127;
System.out.println(i5 == i6); //true
System.out.println(i5.equals(i6)); //true
System.out.println("-----------"); Integer i7 = 128;
Integer i8 = 128;
System.out.println(i7 == i8); // false
System.out.println(i7.equals(i8)); //true /*
* -128到127是byte的取值范围,如果在这个取值范围内,自动装箱就不会新创建对象, 而是从常量池中获取
* 如果超过了byte取值范围就会再新创建对象
*
* public static Integer valueOf(int i) {
assert IntegerCache.high >= 127; // 断言
if (i >= IntegerCache.low && i <= IntegerCache.high) //i>= -128 && i <= 127
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
*/
} }

07 Object类,Scanner,Arrays类,String类,StringBuffer类,包装类的更多相关文章

  1. Java 中 常用API概述之 Math, Object, String,StringBuffer类,Arrays,Integer类

    Math Math类包含执行基本数字运算的方法,如基本指数,对数,平方根和三角函数. 与StrictMath类的一些数字方法不同,Math类的StrictMath所有Math都没有定义为返回比特位相同 ...

  2. Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。

         在java中有三个类负责对字符的操作:Character.String.StringBuffer.其中,Character类是对单个字符进行操作,String是对一个字符序列的操作,Stri ...

  3. Java从零开始学三十(String和StringBuffer类)

    一.StringBuffer连接字符操作 当一个字符串的内容需要被经常改变时就要使用StringBuffer 在StringBuffer中使用append()方法,完成字符串的连接操作   二.Str ...

  4. Java基础(三十四)String、StringBuffer类和数据缓冲区Buffer类

    一.String类 1.创建字符串对象 创建字符串对象有两种方法:直接用“=”或者使用“new String(...)” String aStr = "TMZ"; String b ...

  5. 57. Collections(list的工具类)、Arrays(数组的工具类)

    List集合的工具类(Collections): 注意:Collection是单列集合的根接口  Collections是操作集合对象的工具类 1.对list集合排序: sort(List) 根据自然 ...

  6. Java中的常用类:包装类、String、StringBuffer、StringBuilder、Math、System、Arrays、BigInteger、BigDecimal、Data、Calendar

    一.包装类 √ 二.String类 ★ 三.StringBuffer和StringBuilder类 ★ 四.Math类 五.System类 六.Arrays类 七.BigInteger类和BigDec ...

  7. 【JAVA中String、StringBuffer、StringBuilder类的使用】

    一.String类概述 1.String对象一旦创建就不能改变. 2.字符串常量池. 字符串常量池的特点:池中有则直接使用,池中没有则创建新的字符串常量. 例1: public class Strin ...

  8. 解析Java中的String、StringBuilder、StringBuffer类(一)

    引言 String 类及其相关的StringBuilder.StringBuffer 类在 Java 中的使用相当的多,在各个公司的面试中也是必不可少的.因此,在本周,我打算花费一些时间来认真的研读一 ...

  9. [Java] String类, StringBuffer类

    1. String 类 1. 创建: String s1 = new String; s1 = "abc"; String s2 = new String("abc&qu ...

  10. Java笔记——String、StringBuffer和StringBuilder类

    String类是不可变类,即一旦一个String对象被创建以后,包含在这个对象中的字符串序列是不可改变的,直至这个对象被销毁.   StringBuffer对象则代表一个字符序列可变的字符串,当一个S ...

随机推荐

  1. linux用户管理 用户和用户组信息

    用户管理配置文件 用户信息文件  /etc/passwd 密码文件 /etc/shadow 用户配置文件 /etc/login.defs /etc/default/useradd 新用户信息文件 /e ...

  2. 【资料收集】Converting Between cv::Mat and QImage or QPixmap

    参考: 方法一 Convert between cv::Mat and QImage 两种图片类转换 - Grandyang - 博客园 http://www.cnblogs.com/grandyan ...

  3. VSTO - 使用Excel加载项生成表和图表

    此示例显示如何创建Excel的加载项,使用户可以在其工作表中选择库存符号,然后生成一个新工作表,显示库存的历史性能. 工作表包含数据表和图表. 介绍Excel加载项通常不知道工作表包含什么.典型的加载 ...

  4. 进程中的Manager(),实现多进程的数据共享与传递

    __author__ = "Alex Li" from multiprocessing import Process, Managerimport osdef f(d, l): d ...

  5. C++基础知识:类的静态成员

    1.普通成员变量通过对象名能够访问public成员变量每个对象都可以有只属于自己的成员变量成员变量不能在对象之间共享 2.从命名空间的角度:类的静态成员只是类这个命名空间中的全局变量和全局函数不同之处 ...

  6. SQL-33 创建一个actor表,包含如下列信息

    题目描述   创建一个actor表,包含如下列信息 列表 类型 是否为NULL 含义 actor_id smallint(5) not null 主键id first_name varchar(45) ...

  7. 开发框架DevExtreme全新发布v18.2.6|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  8. DevExpress v18.1新版亮点——Data Access篇

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Data Access v18.1 的新功能,快来下载试用新版本 ...

  9. L308 New brain cells made throughout life

    People keep making new brain cells throughout their lives (well at least until the age of 97), accor ...

  10. libusb示例

    #include <stdio.h> #include <libusb-1.0/libusb.h> #include <stdint.h> #include < ...