码迷,mamicode.com
首页 > 编程语言 > 详细

java22:API-Object-String

时间:2016-01-07 20:23:39      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:object   字符串   equals   java   

Object 类 顶级


    toString()方法

        一般都要重写

        返回对象文本描述

        被很多API调用

    

    ==

        比较变量的值,引用变量的值是地址值,引用变量==比较的是地址值

        不能用于比较逻辑上对象是否相等

    equals 方法

        一般要重写(重写时也要重写 hashCode方法)

        Object的equals 方法用于对象的相等 逻辑

        

    instanceof  java的运算符

           测试一个对象是否是一个类的实例

    A instanceof B 

            对象A是否是类型B的实例  返回boolean

    

String 类 重要类

     final类

    有很多的优化

    底层就是:字符数组

    字符串是有字符数组组成的。

    字符串对象不可改变

    字符串引用是可以重新赋值

    字符串的连接底层是字符数组复制实现的

    

    String API 

       API方法不会改变原字符串对象

        返回值与原有字符串不同时候是新对象

           返回值与原有字符串一样的时候还是引用之前的对象

             为了性能


     常量池(提高性能)

    静态字符串(字面量/常量/常量连接的结果)在静态池中创建,尽量使用同一个对象

    重用静态字符串


    String  toLowerCase()            返回字符串小写形式

          toUpperCase()           返回字符串大写形式

         trim()              返回字符串为原始字符串去掉前后的空白

                        (空格 \t \n \r)

        endsWith("str")         字符串结尾是否有str

        startsWith("str")        字符串开头是否有str

        length()            返回字符串的长度

        indexOf()  有重载      字符串中查找别一个字符串

        substring()          截取字符串中的一部分 

        indexOf()和substring()     配合使用可以截取特定的字符串   




package day022;

public class Demo01 {
	public static void main(String[] args){
		String s1 = "Hello";
		String s2 = s1;
		s1 = s1 + "World";
		System.out.println(s1);
		System.out.println(s2);
		System.out.println(s1.toLowerCase());	
		
		
		
		
		
	}	
}


package day22;大写转小写,小写转大写

import java.util.Arrays;

public class Demo02 {
	public static void main(String[] args) {
		char[] chs = { ‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘,
				‘!‘ };
		char[] chs1 = Arrays.copyOf(chs, chs.length);
		for (char c : chs) {
			if (c > 64 && c < 91) {
				c = (char) (c + 32);
			}
			System.out.print(c);
		}
		for (char c : chs) {
			if (c > ‘a‘ && c < ‘z‘) {
				c = (char) (c - 32);
			}
			System.out.print(c);
		}
	}
}
package day22;// 大写转小写同时小写转大写

import java.util.Arrays;

public class Demo03 {
	public static void main(String[] args){
		char[] chs = {‘H‘,‘e‘,‘l‘,‘l‘,‘o‘,‘ ‘,‘W‘,‘o‘,‘r‘,‘l‘,‘d‘,‘!‘};
		char[] chs1 = Arrays.copyOf(chs, chs.length);
		for(char c:chs){
			if(c>64 && c<91){
				c = (char) (c + 32);
				System.out.print(c);
				continue;
			}else if(c>‘a‘ && c<‘z‘){
				c = (char) (c - 32);
			}
			System.out.print(c);
		}
	}	
}
package day22;//字符串去掉前后的空白

public class Demo04 {
	public static void main(String[] args){
		String name = " \n lmd tx \t \r  ";
		System.out.println(name.trim());
	}
}
package day22;

public class Demo05 {
	public static void main(String[] args){
		String name = " \n lmd tx \t \r  ";
		System.out.println(name.trim());
		
		String file = "p.png";
		boolean isPng = file.endsWith(".png");//字符串结尾是否有.png
		System.out.println(isPng);
		boolean isP = file.startsWith("p");//字符串开头是否有p
		System.out.println(isP);
		
		
	}
}
package day22;

public class Demo06 {
	public static void main(String[] args){
		String str = "asdfzcvqewrfgHHajlsdjf";
		int index = str.indexOf("HH");//找到返回序号
		System.out.println(index);
		index = str.indexOf("GG");//找不到返回-1
		System.out.println(index);
		index = str.indexOf(‘a‘);//从头开始
		System.out.println(index);
		index = str.indexOf(‘a‘, 13);//从指定位置开始找
		System.out.println(index);
		System.out.println(str.substring(4,6));// 截取字符串中的一部分
		
		 
	}
}
package day22;

public class Demo07 {//特殊截取
	public static void main(String[] args){
		String email ="81240805@qq.com";
		String QQ = email.substring(0,email.indexOf("@"));//从邮箱中截取QQ号
		System.out.println(QQ);
		String domain = email.substring(email.indexOf("@")+1);//从邮箱中截取域名
		System.out.println(domain);
		
		
	}
}
package day22;

public class Demo08 {//条码应用 EAN-13
	public static void main(String[] args){
		String str = "6922711076348";
		System.out.println(str);
		char c1;
		char c2;
		int n=0; 
		int m=0;
		for(int i = 0;i <str.length()-1;i+=2){
			c1 = str.charAt(i); 
			n = n +c1-‘0‘;	
			System.out.print(c1);
		}
		System.out.println();
		for(int i = 1;i <str.length()-1;i+=2){
			c2 = str.charAt(i);
			System.out.print(c2);
			m = m +c2-‘0‘;
		}
		System.out.println();
		System.out.println(n);
		System.out.println(m);
		int sum = n + m*3;
		System.out.println(sum);
		int q =(10-sum%10)%10;
		if(q == 0){
			System.out.println(0);
		}else{
		System.out.println(q);
		}	
	}
}

  

本文出自 “浪漫的偷笑” 博客,转载请与作者联系!

java22:API-Object-String

标签:object   字符串   equals   java   

原文地址:http://lmdtx.blog.51cto.com/6942028/1732652

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!