String类

上一篇 / 下一篇  2013-12-11 22:57:01 / 个人分类:java

掌握String类的两种实例化方式51Testing软件测试网B L f5{F6z

掌握String的两种比较操作

r)qf5DG6wR3}]R G0

深入分析String类两种实例化方式的区别

7qD*Kt{0

掌握String类的使用特点

A W6EIk&qZ0

 

0Gj+T ^ R*d0P0

 

i#qA%l:dfB9^\0

实例化String对象51Testing软件测试网!K:x+h ?B

A、直接赋值

kY W`:BZ0

public class StringDemo01{51Testing软件测试网lpDq$Jy3nu&N.c

    public static void main(String[] args){51Testing软件测试网 ob K(xe$@a

        String name = "LiXingHua" ; //表示将一个堆内存空间的指向给了栈内在空间

x b4_7Ch7MtZ&Yy0

        System.out.pringln("姓名: " + name) ;

"T:d(`Xm g:N yG(y4O0

    } 51Testing软件测试网Bm5y!N}/`:z_CT _

}

4PC6o?B1C0

 51Testing软件测试网(T,~)e;K/q'`G

B、通过关键字new51Testing软件测试网p2I*W y O!j

 

5l*d1qS0p;q(} Z0

 51Testing软件测试网 ? MuS%F

  public class StringDemo02{51Testing软件测试网B~&mr\c9O5M

    public static void main(String[] args){

X;Z!\^%_~0

        String name = new String("LiXingHua") ;

ZZo!a.U8T&A0

        System.out.pringln("姓名: " + name) ; 51Testing软件测试网O zpf%ho eK5\` C8qm

    }

%E%S;p_B"T/Xz&f5H0

}51Testing软件测试网2{B$B7Vg n-j E Dc$B g

 51Testing软件测试网h!|;N^)u}$V3[s

 51Testing软件测试网5M+fyESA'f-UzLrX

 

$tf8Fj^"Qj%C)C0

两种实例化的区别51Testing软件测试网J Y~)w;A5_#B

使用直接赋值的方式只城西 个实例化对象即可,而使用new String()的方式则意味着要开辟两个内存对象,开发中最好使用直接赋值方式完成;

!^&FH#C{9TiX0

 

I1Gw*i%N\G.BP_0

 

s3QR9DTi;p([0

直接赋值:使用直接赋值的方式,可以有效的节省内存

(~MF&R _/Yrl-d0

 51Testing软件测试网_*GM ytr0q6~3HH,de

    public class StringDemo07{51Testing软件测试网sAK q _i7]s)F

        public static void main(String[] args){

(U%H_lW3`ADJ0

           String str1 = "hello" ;

$F{}m-\ Ba2J0

           String str2 =  "hello" ;51Testing软件测试网-h2U?}weL

           String str3 =  "hello" ;51Testing软件测试网9y-V w&Y zyU

            System.out.println("str1 == str2 --> " + (str1==str2)) ; 51Testing软件测试网X${JvT:JO&I#O"O"T}

            System.out.println("str1 == str3 --> " + (str1==str3)) ;

1`_ r+P9g5W0

            System.out.println("str2 == str3 --> " + (str2==str3)) ;

5U.gF.M*S#u/] D{0

        } 51Testing软件测试网dKE;PU Lw?0`0m

    }

@SPFT0

 51Testing软件测试网3AITk2N~

 51Testing软件测试网6q+]8h|-n n~ n

输出结果51Testing软件测试网z@g8lt)a

str1 == str2 --> true

+xq7Z{)y J8\$o0

str1 == str3 --> true51Testing软件测试网.U6i Y-`,o?*x:s n

str2 == str3 --> true51Testing软件测试网-wfD'd"cJY

 

Fh}*~G[~0

 

:p1X+[s!BRv lr0

String的内容比较

0Dg4`.EU6`'|q0

 

*NB9['F lm/{0

1使用“==”进行比较51Testing软件测试网E{%T3hmr*]

public class StringDemo02{51Testing软件测试网r'`!}2_6HW4e9b8j

        public static void main(String[] args){

r/R5I,{?Q0

           int x = 30 ;

h#M;\mf0

           int y = 30 ;51Testing软件测试网#B|0B'MPX?#uz

            System.out.println("两个数字的比较结果 " + (x==y)) ; 51Testing软件测试网;P;JvbV%Q(~k|

        }

;tN;m ? v*?)C]0

    }51Testing软件测试网NE$v5R5P4y$Cd

2 == 比较的是内在地址的值51Testing软件测试网e0I5Py Jl2]])h

    public class StringDemo04{

&DZ^6g!k7u0

        public static void main(String[] args){

(vpEE Q%|s0

           String str1 = "hello" ;

c+g aXy0

           String str2 = new String("hello") ;

3P_ Njp9y'?N5w Z"mI0

           String str3 = str2 ;51Testing软件测试网W+PI9}H

            System.out.println("str1 == str2 --> " + (str1==str2)) ; 51Testing软件测试网&wc?y:C a3K;A1VQ:u

            System.out.println("str1 == str3 --> " + (str1==str3)) ;51Testing软件测试网hvp7N&R5B

            System.out.println("str2 == str3 --> " + (str2==str3)) ;

^w1} f{ ET0

        }

{[(h jY]Lz4X0

    }

P RjZXzN0

输出结果51Testing软件测试网AFBr_w

str1 == str2 --> false

$m NP {B8a0{0

str1 == str3 --> false51Testing软件测试网l l\Qw)H a

str2 == str3 --> true

;b.iX w$Ag0c:i0

 51Testing软件测试网2d+E(Z,h:bQ

3equals()51Testing软件测试网]'Y3|xG:D$oc

    public class StringDemo05{

._\/kO|v!X7D:O0

        public static void main(String[] args){51Testing软件测试网$\[)PWZ:}

           String str1 = "hello" ;

u\3rdA0

           String str2 = new String("hello") ;

};L5}X}{0

           String str3 = str2 ;51Testing软件测试网`$Bcw*Z Z}6]

            System.out.println("str1 equals str2 --> " + str1.equals(str2)) ; 51Testing软件测试网fB8`X"y!u

            System.out.println("str1 equals str3 --> " + str1.equals(str3)) ;51Testing软件测试网2s:Ji.WqvV$K

            System.out.println("str2 equals str3 --> " + str2.equals(str3)) ;

$fm e6_)t;h{*c+dL0

        }

2?)L7]8|5Z&dC0

    }51Testing软件测试网3d {Z,V T&q

 

M*{}#mtu-w6g&rlJ0

输出结果:

T#UO4q-Q,Ia%_0

str1 equals str2 --> true

+A0G O u`0

str1 equals str3 --> true51Testing软件测试网%Hr]0Bk y/\+?

str2 equals str3 --> true

3vB`Q ? w a8PV0

 

8y#@D Vv3d]u(u5|7S0

TAG: String类

 

评分:0

我来说两句

luoriver

luoriver

北漂一族,80后,计算机专业,从事SIP相关软件测试3年,热爱生活,崇尚运动。 爱看WWE、公开课。爱钻“牛角尖”,这就是我:luorivr!!!!!

日历

« 2024-04-06  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 345295
  • 日志数: 96
  • 图片数: 1
  • 建立时间: 2012-12-27
  • 更新时间: 2014-05-03

RSS订阅

Open Toolbar