读String源码所思所得

读String源码所思所得

  • I.String类型最大的特点是String对象在创建之后是不可变的,因此String对象之间是可以共享的,即具有相同字符串的String对象都引用自同一个String。
    原因在于,装载字符的value声明的时候被final修饰,private final char value[]。
    如String str = “abc” 和char data[] = {‘a’, ‘b’, ‘c’};String str = new String(data)是等价的
    		//测试代码:	
    			public static void main(String[] args) {
    				String string="abc";
    				String string2=string;
    				System.out.println(string.equals(string2));
    				System.out.println(string==string2);
    				
    				char [] data= {'a','b','c'};
    				String dataStr=new String(data);
    				
    				System.out.println(string.equals(dataStr));
    				System.out.println(string==dataStr);
    				
    				String string3=new String(string);
    				
    				System.out.println(string.equals(string3));
    				System.out.println(string==string3);
    				
    				/*
    				 * 运行结果:
    					true
    					true
    					true
    					false
    					true
    					false
    					表明:赋值得到的String与被赋值的String指向的是同一个String对象,两者不仅等价且相等
    						   而new出来的String虽然与传入构造器的string等价但不相等,不是同一个对象
    				*/
    		
    				string2=string2+"d";
    				System.out.println(string);
    				
    				dataStr=dataStr+"e";		
    				System.out.println(string);
    				/*
    				 * 运行结果:
    					abc
    					abc
    					表明:不论是赋值得到的String还是new出来的String,其本身的变化都无法影响提供字符串的String
    				*/
    		
    			}
    
    这个特点在String类的原生方法intern()的注释中有解释:String类会维护一个私有的字符串池,而我们在代码中使用的String对象时引用对象,创建一个新的String对象时intern()方法会被调用,字符串池会从池中已有的字符串对象里查找跟新对象等价(即字符串完成匹配)的对象,如果有则该新对象会直接引用匹配的对象,如果没有则会创建新对象需要引用的字符串对象并加入字符串池,最后引用给新对象。注释原文如下:
    				 Returns a canonical representation for the string object.
    			     * <p>
    			     * A pool of strings, initially empty, is maintained privately by the
    			     * class {@code String}.
    			     * <p>
    			     * When the intern method is invoked, if the pool already contains a
    			     * string equal to this {@code String} object as determined by
    			     * the {@link #equals(Object)} method, then the string from the pool is
    			     * returned. Otherwise, this {@code String} object is added to the
    			     * pool and a reference to this {@code String} object is returned.
    			     * <p>
    			     * It follows that for any two strings {@code s} and {@code t},
    			     * {@code s.intern() == t.intern()} is {@code true}
    			     * if and only if {@code s.equals(t)} is {@code true}.
    			     * <p>
    			     * All literal strings and string-valued constant expressions are
    			     * interned. String literals are defined in section 3.10.5 of the
    			     * <cite>The Java&trade; Language Specification</cite>.
    			     *
    			     * @return  a string that has the same contents as this string, but is
    			     *          guaranteed to be from a pool of unique strings.
    			     */
    			     public native String intern();
    
  • II.Strin类中提供了多个构造器,其中最为特别的是为截取扩展字符数值数组并转换成字符的构造器,因为一个扩展字符占用两个char大小,所以需要在数组长度上下功夫。自然,很多方法都需要考虑到扩展字符长度的问题,因此String中一种方法会有两个版本:针对BMP和针对扩展字符
    		public String(int[] codePoints, int offset, int count) {
    	        if (offset < 0) {
    	            throw new StringIndexOutOfBoundsException(offset);
    	        }
    	        if (count <= 0) {
    	            if (count < 0) {
    	                throw new StringIndexOutOfBoundsException(count);
    	            }
    	            if (offset <= codePoints.length) {
    	                this.value = "".value;
    	                return;
    	            }
    	        }
    	        // Note: offset or count might be near -1>>>1.
    	        if (offset > codePoints.length - count) {
    	            throw new StringIndexOutOfBoundsException(offset + count);
    	        }
    	
    	        final int end = offset + count;
    	
    	        // Pass 1: Compute precise size of char[]
    			//计算扩展字符数量,字符长度为count+扩展字符数量	        
    			int n = count;
    	        for (int i = offset; i < end; i++) {
    	            int c = codePoints[i];
    	            if (Character.isBmpCodePoint(c))
    	                continue;
    	            else if (Character.isValidCodePoint(c))
    	                n++;
    	            else throw new IllegalArgumentException(Integer.toString(c));
    	        }
    	
    	        // Pass 2: Allocate and fill in char[]
    	        final char[] v = new char[n];
    	
    	        for (int i = offset, j = 0; i < end; i++, j++) {
    	            int c = codePoints[i];
    	            if (Character.isBmpCodePoint(c))
    	                v[j] = (char)c;
    	            else
    	                Character.toSurrogates(c, v, j++);
    	        }
    	
    	        this.value = v;
    	    }
    
  • III.String提供了查找目标字符或字符串在某个字符串中的索引号,其中比较特别的是LastIndexOf(char[] source, int sourceOffset, int sourceCount,char[] target, int targetOffset, int targetCount,int fromIndex)方法,也就是从尾查找目标字符串在指定字符串中的结束索引号,特别之处在于如果目标字符串为空字符串时返回指定字符串的结束索引号,这里应该使用了集合概念,空集是任意集合的子集。
		//代码清单:
	
			static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
		            char[] target, int targetOffset, int targetCount,
		            int fromIndex) {
		        /*
		         * Check arguments; return immediately where possible. For
		         * consistency, don't check for null str.
		         */
		        int rightIndex = sourceCount - targetCount;
		        if (fromIndex < 0) {
		            return -1;
		        }
		        if (fromIndex > rightIndex) {
		            fromIndex = rightIndex;
		        }
		        /* Empty string always matches. */
		        if (targetCount == 0) {
		            return fromIndex;
		        }
		
		        int strLastIndex = targetOffset + targetCount - 1;
		        char strLastChar = target[strLastIndex];
		        int min = sourceOffset + targetCount - 1;
		        int i = min + fromIndex;
		
		    startSearchForLastChar:
		        while (true) {
		            while (i >= min && source[i] != strLastChar) {
		                i--;
		            }
		            if (i < min) {
		                return -1;
		            }
		            int j = i - 1;
		            int start = j - (targetCount - 1);
		            int k = strLastIndex - 1;
		
		            while (j > start) {
		                if (source[j--] != target[k--]) {
		                    i--;
		                    continue startSearchForLastChar;
		                }
		            }
		            return start - sourceOffset + 1;
		        }
		    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统根据B/S,即所谓的电脑浏览器/网络服务器方式,运用Java技术性,挑选MySQL作为后台系统。系统主要包含对客服聊天管理、字典表管理、公告信息管理、金融工具管理、金融工具收藏管理、金融工具银行卡管理、借款管理、理财产品管理、理财产品收藏管理、理财产品银行卡管理、理财银行卡信息管理、银行卡管理、存款管理、银行卡记录管理、取款管理、转账管理、用户管理、员工管理等功能模块。 文中重点介绍了银行管理的专业技术发展背景和发展状况,随后遵照软件传统式研发流程,最先挑选适用维和语言软件开发平台,依据需求分析报告模块和设计数据库结构,再根据系统功能模块的设计制作系统功能模块图、流程表和E-R图。随后设计架构以及编写代码,并实现系统能模块。最终基本完成系统检测和功能测试。结果显示,该系统能够实现所需要的作用,工作状态没有明显缺陷。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。进入银行卡列表,管理员可以进行查看列表、模糊搜索以及相关维护等操作。用户进入系统可以查看公告和模糊搜索公告信息、也可以进行公告维护操作。理财产品管理页面,管理员可以进行查看列表、模糊搜索以及相关维护等操作。产品类型管理页面,此页面提供给管理员的功能有:新增产品类型,修改产品类型,删除产品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值