黑马程序员--Java常见对象
2015-08-10 18:15
543 查看
——- android培训、java培训、期待与您交流! ———-
Object类的构造方法有一个,并且是无参构造
Object类需要掌握的方法:
hashCode()方法
public int hashCode():返回该对象的哈希码值
哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值。可以理解为地址值。
getClass()方法
public final Class getClass():返回此 Object 的运行时类
配合使用Class类方法:
public String getName():以 字符串的形式返回此 Class 对象所表示的实体
需要注意调用这个方法的返回值是一个Class类,再从Class类中调用getName()方法得到此类的名称。
从以上代码可以看出Object中的toString()方法返回对象的字符串表示,默认是由类的全路径+’@’+哈希值的十六进制表示,但是这个信息没有任何意义。所以API所有的类中都重写了该方法。利用eclipse工具直接可以生成。
上面的代码中可以看出==和equals()方法效果是一样,比较的都是地址值,而比较地址值是没有意义的,一般子类也会重写该方法。eclipse工具中也会自动重写该方法。比较的是两个对象属性是否相同。
构造方法:
public Scanner(InputStream source)
InputStream is = System.in;
System.in是标准的输入流,对应于键盘录入,IO流中会讲解。
常用格式:
Scanner sc = new Scanner(System.in);
hasNextXxx()和nextXxx()方法:
public boolean hasNextXxx():判断是否是某种类型元素
public Xxx nextXxx():获取该元素
public int nextInt():获取一个int类型的值
public String nextLine():获取一个String类型的值
出现问题:
先获取一个数值,在获取一个字符串,会出现问题。
主要原因:就是那个换行符号的问题。
解决方案:
方案1:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
方案2:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
字符串:由多个字符组成的一串数据,也可以看成是一个字符数组。
字符串是常量,一旦赋值就不能改变。指的是字符串的内容不能改变,而不是引用不能改变。
构造方法:
String s = new String(“hello”);和String s = “hello”;的区别
String s = “hello”;字符串直接赋值方式是先到字符串常量池里面找,如果有就直接返回,没有就创建并返回。
String s = new String(“hello”);会在堆中开辟空间,然后再去常量池中找有没有这个字符,如果有堆中的空间就指向此字符串,没有就创建。
所以它们两个的区别是前者会创建两个对象,后者只创建一个。
判断功能
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空。
需要注意的是字符串内容为空和字符串对象为空是不同的。
String s = “”;是内容为空
String s = null;是对象为空
获取功能
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):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
String substring(int start):从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
转换功能
byte[] getBytes():把字符串转换为字节数组。
char[] toCharArray():把字符串转换为字符数组。
static String valueOf(char[] chs):把字符数组转成字符串。
static String valueOf(int i):把int类型的数据转成字符串。
String toLowerCase():把字符串转成小写。
String toUpperCase():把字符串转成大写。
String concat(String str):把字符串拼接。
其他功能
替换功能
String replace(char old,char new)
String replace(String old,String new)
去空格功能
String trim()
按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)
String作为参数传递,效果和基本类型作为传递效果是一样的
String,StringBuffer,StringBuilder的区别
String的内容是不可变的。
StringBuffer和StringBuilder都是内容可变的。
StringBuilder是同步的,数据安全,效率低。
StringBuilder是不同步的,数据不安全,效率高。
StringBuffer和数组的区别
两者都可看成是一个存储数据的容器。
StringBuffer里面的数据最终是一个字符串数据
数组可以存储多种类型数据,但必须存储同一类型的数据
byte —– Byte
short —– Short
int —– Integer
long —– Long
float —– Float
double —– Double
char —– Character
boolean —– Boolean
构造方法
Integer i = new Integer(100);
进制转换
Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据
API
API是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件的以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节,简单来说就是JDK提供给我们的一些提高编程效率的java类。object类
Object是类层次结构的根类,所有的类都直接或者间接的继承自Object类。Object类的构造方法有一个,并且是无参构造
Object类需要掌握的方法:
hashCode()方法
public int hashCode():返回该对象的哈希码值
public class PersonDemo { public static void main(String[] args) { Person p1 = new Person(); //获取对象p1的哈希值 System.out.println(p1.hashCode()); } } class Person extends Object{ }
哈希值是根据哈希算法计算出来的一个值,这个值和地址值有关,但是不是实际地址值。可以理解为地址值。
getClass()方法
public final Class getClass():返回此 Object 的运行时类
配合使用Class类方法:
public String getName():以 字符串的形式返回此 Class 对象所表示的实体
public class StudentTest { public static void main(String[] args) { Student s = new Student(); Class c = s.getClass(); String str = c.getName(); System.out.println(str); //链式编程 String str2 = s.getClass().getName(); System.out.println(str2); } } class Student extends Object { }
需要注意调用这个方法的返回值是一个Class类,再从Class类中调用getName()方法得到此类的名称。
toString()方法
public String toString():返回该对象的字符串表示public class StudentDemo { public static void main(String[] args) { Student s = new Student(); System.out.println(s.getClass().getName() + '@' +Integer.toHexString(s.hashCode())); System.out.println(s.toString()); System.out.println(s); } }
从以上代码可以看出Object中的toString()方法返回对象的字符串表示,默认是由类的全路径+’@’+哈希值的十六进制表示,但是这个信息没有任何意义。所以API所有的类中都重写了该方法。利用eclipse工具直接可以生成。
equals()方法
public boolean equals(Object obj):比较两个对象是否相同。默认情况下,比较的是地址值是否相同。public class StudentDemo { public static void main(String[] args) { Student s1 = new Student("周星驰", 30); Student s2 = new Student("周星驰", 30); System.out.println(s1 == s2); Student s3 = s1; System.out.println(s1 == s3); System.out.println("---------------"); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); } }
上面的代码中可以看出==和equals()方法效果是一样,比较的都是地址值,而比较地址值是没有意义的,一般子类也会重写该方法。eclipse工具中也会自动重写该方法。比较的是两个对象属性是否相同。
Scanner类
在JDK5以后出现的用于键盘录入数据的类。构造方法:
public Scanner(InputStream source)
InputStream is = System.in;
System.in是标准的输入流,对应于键盘录入,IO流中会讲解。
常用格式:
Scanner sc = new Scanner(System.in);
hasNextXxx()和nextXxx()方法:
public boolean hasNextXxx():判断是否是某种类型元素
public Xxx nextXxx():获取该元素
public int nextInt():获取一个int类型的值
public String nextLine():获取一个String类型的值
public class ScannerDemo { public static void main(String[] args) { // 创建对象 Scanner sc = new Scanner(System.in); // 获取数据 if (sc.hasNextInt()) { int x = sc.nextInt(); System.out.println("x:" + x); } else { System.out.println("你输入的数据有误"); } } }
出现问题:
先获取一个数值,在获取一个字符串,会出现问题。
主要原因:就是那个换行符号的问题。
解决方案:
方案1:先获取一个数值后,在创建一个新的键盘录入对象获取字符串。
方案2:把所有的数据都先按照字符串获取,然后要什么,你就对应的转换为什么。
String类
String类是多个字符组成的一串数据。字符串:由多个字符组成的一串数据,也可以看成是一个字符数组。
字符串是常量,一旦赋值就不能改变。指的是字符串的内容不能改变,而不是引用不能改变。
构造方法:
public class StringDemo { public static void main(String[] args) { // public String():空构造 String s1 = new String(); System.out.println("s1:" + s1); System.out.println("s1.length():" + s1.length()); System.out.println("--------------------------"); // public String(byte[] bytes):把字节数组转成字符串 byte[] bys = { 97, 98, 99, 100, 101 }; String s2 = new String(bys); System.out.println("s2:" + s2); System.out.println("s2.length():" + s2.length()); System.out.println("--------------------------"); // public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串 // 我想得到字符串"bcd" String s3 = new String(bys, 1, 3); System.out.println("s3:" + s3); System.out.println("s3.length():" + s3.length()); System.out.println("--------------------------"); // public String(char[] value):把字符数组转成字符串 char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' }; String s4 = new String(chs); System.out.println("s4:" + s4); System.out.println("s4.length():" + s4.length()); System.out.println("--------------------------"); // public String(char[] value,int index,int count):把字符数组的一部分转成字符串 String s5 = new String(chs, 2, 4); System.out.println("s5:" + s5); System.out.println("s5.length():" + s5.length()); System.out.println("--------------------------"); //public String(String original):把字符串常量值转成字符串 String s6 = new String("abcde"); System.out.println("s6:" + s6); System.out.println("s6.length():" + s6.length()); System.out.println("--------------------------"); //字符串字面值"abc"也可以看成是一个字符串对象。 String s7 = "abcde"; System.out.println("s7:"+s7); System.out.println("s7.length():"+s7.length()); } }
String s = new String(“hello”);和String s = “hello”;的区别
String s = “hello”;字符串直接赋值方式是先到字符串常量池里面找,如果有就直接返回,没有就创建并返回。
String s = new String(“hello”);会在堆中开辟空间,然后再去常量池中找有没有这个字符,如果有堆中的空间就指向此字符串,没有就创建。
所以它们两个的区别是前者会创建两个对象,后者只创建一个。
判断功能
boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断大字符串中是否包含小字符串
boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty():判断字符串是否为空。
public class StringDemo { public static void main(String[] args) { // 创建字符串对象 String s1 = "helloworld"; String s2 = "helloworld"; String s3 = "HelloWorld"; // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写 System.out.println("equals:" + s1.equals(s2)); System.out.println("equals:" + s1.equals(s3)); System.out.println("-----------------------"); // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 System.out.println("equals:" + s1.equalsIgnoreCase(s2)); System.out.println("equals:" + s1.equalsIgnoreCase(s3)); System.out.println("-----------------------"); // boolean contains(String str):判断大字符串中是否包含小字符串 System.out.println("contains:" + s1.contains("hello")); System.out.println("contains:" + s1.contains("hw")); System.out.println("-----------------------"); // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头 System.out.println("startsWith:" + s1.startsWith("h")); System.out.println("startsWith:" + s1.startsWith("hello")); System.out.println("startsWith:" + s1.startsWith("world")); System.out.println("-----------------------"); // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩 // boolean isEmpty():判断字符串是否为空。 System.out.println("isEmpty:" + s1.isEmpty()); String s4 = ""; String s5 = null; System.out.println("isEmpty:" + s4.isEmpty()); // NullPointerException // s5对象都不存在,所以不能调用方法,空指针异常 System.out.println("isEmpty:" + s5.isEmpty()); } }
需要注意的是字符串内容为空和字符串对象为空是不同的。
String s = “”;是内容为空
String s = null;是对象为空
获取功能
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):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
String substring(int start):从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
public class StringDemo { public static void main(String[] args) { // 定义一个字符串对象 String s = "helloworld"; // int length():获取字符串的长度。 System.out.println("s.length:" + s.length()); System.out.println("----------------------"); // char charAt(int index):获取指定索引位置的字符 System.out.println("charAt:" + s.charAt(7)); System.out.println("----------------------"); // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf('l')); System.out.println("----------------------"); // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf("owo")); System.out.println("----------------------"); // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 System.out.println("indexOf:" + s.indexOf('l', 4)); System.out.println("indexOf:" + s.indexOf('k', 4)); // -1 System.out.println("indexOf:" + s.indexOf('l', 40)); // -1 System.out.println("----------------------"); // 自己练习:int indexOf(String str,int // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引 System.out.println("substring:" + s.substring(5)); System.out.println("substring:" + s.substring(0)); System.out.println("----------------------"); // String substring(int start,int // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引 System.out.println("substring:" + s.substring(3, 8)); System.out.println("substring:" + s.substring(0, s.length())); } }
转换功能
byte[] getBytes():把字符串转换为字节数组。
char[] toCharArray():把字符串转换为字符数组。
static String valueOf(char[] chs):把字符数组转成字符串。
static String valueOf(int i):把int类型的数据转成字符串。
String toLowerCase():把字符串转成小写。
String toUpperCase():把字符串转成大写。
String concat(String str):把字符串拼接。
public class StringDemo { public static void main(String[] args) { // 定义一个字符串对象 String s = "JavaSE"; // byte[] getBytes():把字符串转换为字节数组。 byte[] bys = s.getBytes(); for (int x = 0; x < bys.length; x++) { System.out.println(bys[x]); } System.out.println("----------------"); // char[] toCharArray():把字符串转换为字符数组。 char[] chs = s.toCharArray(); for (int x = 0; x < chs.length; x++) { System.out.println(chs[x]); } System.out.println("----------------"); // static String valueOf(char[] chs):把字符数组转成字符串。 String ss = String.valueOf(chs); System.out.println(ss); System.out.println("----------------"); // static String valueOf(int i):把int类型的数据转成字符串。 int i = 100; String sss = String.valueOf(i); System.out.println(sss); System.out.println("----------------"); // String toLowerCase():把字符串转成小写。 System.out.println("toLowerCase:" + s.toLowerCase()); System.out.println("s:" + s); // System.out.println("----------------"); // String toUpperCase():把字符串转成大写。 System.out.println("toUpperCase:" + s.toUpperCase()); System.out.println("----------------"); // String concat(String str):把字符串拼接。 String s1 = "hello"; String s2 = "world"; String s3 = s1 + s2; String s4 = s1.concat(s2); System.out.println("s3:"+s3); System.out.println("s4:"+s4); } }
其他功能
替换功能
String replace(char old,char new)
String replace(String old,String new)
去空格功能
String trim()
按字典比较功能
int compareTo(String str)
int compareToIgnoreCase(String str)
public class StringDemo { public static void main(String[] args) { // 替换功能 String s1 = "helloworld"; String s2 = s1.replace('l', 'k'); String s3 = s1.replace("owo", "ak47"); System.out.println("s1:" + s1); System.out.println("s2:" + s2); System.out.println("s3:" + s3); System.out.println("---------------"); // 去除字符串两空格 String s4 = " hello world "; String s5 = s4.trim(); System.out.println("s4:" + s4 + "---"); System.out.println("s5:" + s5 + "---"); // 按字典顺序比较两个字符串 String s6 = "hello"; String s7 = "hello"; String s8 = "abc"; String s9 = "xyz"; System.out.println(s6.compareTo(s7));// 0 System.out.println(s6.compareTo(s8));// 7 System.out.println(s6.compareTo(s9));// -16 } }
String作为参数传递,效果和基本类型作为传递效果是一样的
public class StringBufferDemo { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; System.out.println(s1 + "---" + s2);// hello---world change(s1, s2); System.out.println(s1 + "---" + s2);// hello---world StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("world"); System.out.println(sb1 + "---" + sb2);// hello---world change(sb1, sb2); System.out.println(sb1 + "---" + sb2);// hello---worldworld } public static void change(StringBuffer sb1, StringBuffer sb2) { sb1 = sb2; sb2.append(sb1); } public static void change(String s1, String s2) { s1 = s2; s2 = s1 + s2; } }
String,StringBuffer,StringBuilder的区别
String的内容是不可变的。
StringBuffer和StringBuilder都是内容可变的。
StringBuilder是同步的,数据安全,效率低。
StringBuilder是不同步的,数据不安全,效率高。
StringBuffer和数组的区别
两者都可看成是一个存储数据的容器。
StringBuffer里面的数据最终是一个字符串数据
数组可以存储多种类型数据,但必须存储同一类型的数据
Arrays工具类
针对数组进行操作的工具类。包括排序和查找等功能。public class ArraysDemo { public static void main(String[] args) { // 定义一个数组 int[] arr = { 24, 69, 80, 57, 13 }; // public static String toString(int[] a) 把数组转成字符串 System.out.println("排序前:" + Arrays.toString(arr)); // public static void sort(int[] a) 对数组进行排序 Arrays.sort(arr); System.out.println("排序后:" + Arrays.toString(arr)); // [13, 24, 57, 69, 80] // public static int binarySearch(int[] a,int key) 二分查找 System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57)); System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577)); } }
Integer类
为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型。byte —– Byte
short —– Short
int —– Integer
long —– Long
float —– Float
double —– Double
char —– Character
boolean —– Boolean
构造方法
Integer i = new Integer(100);
进制转换
public class IntegerDemo { public static void main(String[] args) { // 十进制到二进制,八进制,十六进制 System.out.println(Integer.toBinaryString(100)); System.out.println(Integer.toOctalString(100)); System.out.println(Integer.toHexString(100)); System.out.println("-------------------------"); // 十进制到其他进制 System.out.println(Integer.toString(100, 10)); System.out.println(Integer.toString(100, 2)); System.out.println(Integer.toString(100, 8)); System.out.println(Integer.toString(100, 16)); System.out.println(Integer.toString(100, 5)); System.out.println(Integer.toString(100, 7)); System.out.println(Integer.toString(100, -7)); System.out.println(Integer.toString(100, 70)); System.out.println(Integer.toString(100, 1)); System.out.println(Integer.toString(100, 17)); System.out.println(Integer.toString(100, 32)); System.out.println(Integer.toString(100, 37)); System.out.println(Integer.toString(100, 36)); System.out.println("-------------------------"); //其他进制到十进制 System.out.println(Integer.parseInt("100", 10)); System.out.println(Integer.parseInt("100", 2)); System.out.println(Integer.parseInt("100", 8)); System.out.println(Integer.parseInt("100", 16)); System.out.println(Integer.parseInt("100", 23)); //NumberFormatException //System.out.println(Integer.parseInt("123", 2)); } }
Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据
public class IntegerDemo { public static void main(String[] args) { Integer i1 = new Integer(127); Integer i2 = new Integer(127); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = new Integer(128); Integer i4 = new Integer(128); System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6); System.out.println(i5.equals(i6)); System.out.println("-----------"); Integer i7 = 127; Integer i8 = 127; System.out.println(i7 == i8); System.out.println(i7.equals(i8)); } }
总结
这里只是列举出来比较常见的一部分,学习常见对象,重点在于清楚每个类的作用,以及每个类中的常见方法,没有什么难点知识。平时勤加练习,多敲一下代码就能全部记住。相关文章推荐
- 黑马程序员——C语言之 指针
- android面试
- 年度催泪之作:2015中国程序员生存报告
- 百度测试开发面试题
- 面试题收集
- 面试题集锦---BY算法导论小组
- 黑马程序员--IO流之字符流
- PHP的文件操作与算法实现的面试题示例
- 黑马程序员——面向对象概述+类与对象的关系-第8天
- Mac Pro 提高效率 快捷键 程序员
- 关于游戏程序猿的职业生涯规划
- 题解分析及总结:经典逻辑面试题“假设有一个池塘,里面有无穷多的水,现有2个空水壶,如何使用少于15L的水,用2个水壶从池塘里取得3升水”
- 99%的海量数据处理面试题
- Android程序员必读电子书推荐
- 程序员学炒股(7) 股指期货收盘价对第二天开盘价有影响吗?
- 为什么想得慢的人能赢,程序员又如何
- 程序员常去的14个顶级开发社区
- 为什么程序员的业余项目大多都死了?
- 黑马程序员——代理
- 漫谈程序员(五)20年资深程序员编程经验分享