黑马程序员——面试题小总结2

来源:互联网 发布:数据库应用软件开发 编辑:程序博客网 时间:2024/06/05 01:19

------<a href="http://www.itheima.com"target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

面试题小总结2

/*

         逻辑异或 ^ 运算符的特点

        

         需求:向实现两个变量值的互换

                            inta = 5;

                            intb = 3;

                           

         实现方式:

                  方式1:通过第三方变量

                  方式2:通过异或的方式

*/

class OperatorDemo2 {

         publicstatic void main(String[] args) {

                   //方式1:通过第三方变量(开发中)

                   inta = 5;

                   intb = 3;

                  

                   inttemp = a;//temp = 5

                   a= b; //a = 3

                   b= temp;

                  

                   System.out.println("a="+a);

                   System.out.println("b="+b);

                  

                   //方式2:通过第三方变量(面试)

                   intc = 5;

                   intd = 3;

                  

                   c= c ^ d;//c = 5 ^ 3;

                   d= c ^ d;//d = 5 ^ 3 ^ 3 = 5;

                   c= c ^ d;//c = 5 ^ 3 ^ 5 ^ 3 ^ 3 = 5 ^ 3 ^ 5 = 3;

                  

                   System.out.println(5 ^ 3 );//6

                   System.out.println(5 ^ 3 ^ 3 );//5

                   System.out.println(5 ^ 5 ^ 3 );//3

                   System.out.println(5 ^ 3 ^ 5 ^ 3 ^ 3 );//3

                  

                   System.out.println("c="+c);

                   System.out.println("d="+d);

         }

}

 

 

 

package cn.itcast_03_String;

/*

 *String面试题

 */

public class StringDemo4 {

         publicstatic void main(String[] args) {

                   Strings1 = new String("hello");

                   Strings2 = new String("hello");

                   System.out.println(s1==s2);//false

                   System.out.println(s1.equals(s2));//true

 

                   Strings3 = new String("hello");

                   Strings4 = "hello";

                   System.out.println(s3==s4);//false

                   System.out.println(s3.equals(s4));//true

                                    

                   Strings5 = "hello";

                   Strings6 = "hello";

                   System.out.println(s5==s6);//true

                   System.out.println(s5.equals(s6));//true

         }

}

package cn.itcast_03_String;

/*

 * 面试题

 */

public class StringDemo5 {

         publicstatic void main(String[] args) {

                   Strings1 = "hello";

                   Strings2 = "world";

                   Strings3 = "helloworld";

                   System.out.println(s3== (s1+s2)  );//false

                   System.out.println(s3== ("hello" + "world") );//true

                   System.out.println(s3.equals(s1+s2));//true

 

         }

}

/*

         switch语句面试题

*/

class SwitchDemo3 {

         publicstatic void main(String[] args) {

                   /*

                   intx = 2;

                   inty = 3;

                   switch(x){

                            default:

                                     y++;

                                     break;

                            case3:

                                     y++;

                            case4:

                                     y++;

                   }

                   System.out.println("y="+y);//y=4

                   */

                  

                   intx = 2;

                   inty = 3;//4,5,6

                   switch(x){

                            default:

                                     y++;

                                     //break;

                            case3:

                                     y++;

                            case4:

                                     y++;

                   }

                   System.out.println("y="+y);//y=6

                  

         }

}

 

class X {

         Yb = new Y();

         X(){

                   System.out.print("X");

         }

}

class Y {

         Y(){

                   System.out.print("Y");

         }

}

public class Z extends X {

         Yy = new Y();

         Z(){

                   System.out.print("Z");

         }

         publicstatic void main(String[] args) {

                   newZ();

         }

}

 

 

 

 

 

 

 

 

 

/*

         方法重写和方法重载的区别?方法重载能改变返回值类型吗?

                   Overload

                   Override

        

         请解释 Overload(方法重载) Override(方法重写)的区别?

                   Overload(方法重载) :在同一个类中,多个方法同名,参数列表不同

                   Override(方法重写) :子父类中,方法的声明相同,子类将代码进行重写编写

                  

         方法重载能改变返回值类型吗?

                  能,因为方法重载与返回值类型有关

                  

         方法重写能改变返回值类型吗?

                  不能,因为要求方法的声明相同

                           方法的声明  -->  返回值类型方法名(参数列表)

*/

1,String,StringBuffer,StringBuilder的区别

 

         String:字符串

                  长度固定

                  内容不能改变

                  底层实现长度固定的字符数组

         StringBuffer:线程安全的字符串缓冲区

                   jdk1.0

                  长度可变

                  内容可变

                  底层实现长度可变的字符数组

                  线程安全,执行效率低

         StringBuilder:线程不安全的字符串缓冲区

                   jdk1.5

                  长度可变

                  内容可变

                  底层实现长度可变的字符数组

                  线程不安全,执行效率高

        

 

2,StringBuffer和数组的区别

         共同点:

                   StringBuffer与数组都可以存储多个元素内容

                  

         区别:

                  数据类型:

                           数组可以是各种数据类型的数组

                           字符串缓冲区,会把任意的数据类型都转换成字符串类型存储

                  

                  长度:

                           数组长度是固定不变的

                           字符串缓冲区长度可以动态改变的

                           

                           

                           

                           

 

 

0 0
原创粉丝点击