java基础

时间:2020-10-14 20:07:36   收藏:0   阅读:87

java基础

jdk,jre,jvm的关系

JDK包含JRE,JRE包含JVM.

技术图片

常用类

String

String的特性

String的定义方式

String s1 = "abc";//字面量的定义方式,数据存放在方法区字符常量池

//本质上this.value = new char[0];
String s1 = new String(); 
//this.value = original.value;
String s2 = new String(String original); 
//this.value = Arrays.copyOf(value, value.length);
String s3 = new String(char[] a);

//Tom存放于字符常量池中
Person p1 = new Person("Tom",12);
Person p2 = new Person("Tom",12);
System.out.println(p1.name == p2.name);//true

技术图片

String常见笔试题

String s1 = "hello";
String s2 ="world";

String s3 = "hello" + "world";
String s4 = s1 + "world";
String s5 = "hello" + s2;
String s6 = (s1 + s2).intern();

System.out.println(s3 == s4);//false
System.out.println(s3 == s5);//false
System.out.println(s4 == s5);//false
System.out.println(s3 == s6);//true
public class StringTest {
    
    //str存放在了堆中
    String str = new String("good");
    //ch存放在了堆中
    char[] ch = { ‘t‘, ‘e‘, ‘s‘, ‘t‘ };
    
    //值传递,地址不可改变
    public void change(String str, char ch[]) {
        //str存放在了常量池,局部变量
        str = "test ok";
        //ch堆中的值变了
        ch[0] = ‘b‘; 
    }
    
    public static void main(String[] args) {
        StringTest ex = new StringTest();
        ex.change(ex.str, ex.ch);
        System.out.print(ex.str);//good
        System.out.println(ex.ch);//best
	} 
}

String的常用方法


############################################


#########################################

String 常见算法题

//模拟一个trim方法,去除字符串两端的空格。
public String myTrim(String str) {

   if (str != null) {
        int start = 0;// 用于记录从前往后首次索引位置不是空格的位置的索引
        int end = str.length() - 1;// 用于记录从后往前首次索引位置不是空格的位置的索引

        while (start < end && str.charAt(start) ==‘ ‘{
                    start++;
        	}

        while (start < end && str.charAt(end)== ‘ ‘) {
                    end--;
             }
        if (str.charAt(start) == ‘ ‘){
                    return "";
          }

         return str.substring(start, end + 1);
        }
    
	return null;
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!