您的位置:首页 > 职场人生

常见笔试面试的一些原理

2010-04-21 15:06 597 查看
1.字符串相关

package com.test;

public class StringTest

{

public static void main(String[] args)

{

String s = new String("abc");//生成两个对象,String pool中一个,堆中一个

String s1 = "abc";//没有生成对象

String s2 = new String("abc");//堆中生成一个对象

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

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

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

System.out.println(s == s.intern());//false

System.out.println(s1 == s1.intern());//true

System.out.println(s.intern() == s2.intern());//true

String hello = "hello";

String hel = "hel";

String lo = "lo";

System.out.println(hello == "hel" + "lo");//true

System.out.println(hello == "hel" + lo);//false

}

}

输出:

false

false

false

false

true

true

true

false

2.参数传递

package com.test;

public class ParamTest

{

public void changeInt(int a)

{

a = 3;

}

public void changePoint(Point point)

{

point.x = 5;

point.y = 6;

}

public void changeString(String str)

{

str = "abc";

}

public static void main(String[] args)

{

String str = "xyz";

ParamTest pt = new ParamTest();

pt.changeString(str);

System.out.println(str);

int a = 1;

ParamTest pt1 = new ParamTest();

pt1.changeInt(a);

System.out.println(a);

Point point = new Point(1,2);

ParamTest pt2 = new ParamTest();

pt2.changePoint(point);

System.out.println(point.x);

System.out.println(point.y);

}

}

class Point

{

int x;

int y;

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

}

输出:

xyz

1

5

6

在java中,不管是原生数据类型,还是引用类型,都是值传递

3.静态变量

package com.test;

public class StaticTest

{

private static StaticTest st = new StaticTest();

public static int count1;

public static int count2 = 0;

private StaticTest()

{

count1++;

count2++;

}

public static StaticTest getInstance()

{

return st;

}

public static void main(String[] args)

{

StaticTest st = StaticTest.getInstance();

System.out.println("count1: " + st.count1);

System.out.println("count2: " + st.count2);

}

}

输出:

count1: 1

count2: 0

静态变量从上往下依次执行,从main函数开始,执行getInstance方法时,先执行private static StaticTest st = new StaticTest(); ,new StaticTest();给count1,count2赋值都为1,然后执行

public static int count1;,只是定义一个变量,跳过,执行 public static int count2 = 0;给count2赋值为0

4.父子类的静态块和构造方法的执行顺序

package com.test;

public class OrderTest

{

public static void main(String[] args)

{

new Child("abc");

}

}

class Parent

{

static String name = "hello";

static

{

System.out.println("parent static block");

}

public Parent(String name)

{

System.out.println("parent constructor");

}

}

class Child extends Parent

{

static String childName = "world";

static

{

System.out.println("child static block");

}

public Child(String str)

{

super(str);

System.out.println("child constructor");

}

}

输出:

parent static block

child static block

parent constructor

child constructor

生成子类对象是,父子类的静态块和构造方法的执行顺序是先父类的静态块,再子类的静态块,再父类的构造方法,再子类的构造方法,

子类的构造方法,不管带不带参数,总是先调用父类的不带参数的构造方法,如果父类没有不带参数的构造方法,子类应显示的调用父类的构造方法

5.关于java中的方法重写(override)

1>.子类中的方法与父类中的方法有相同的返回类型,相同的方法名称,相同的参数类表

2>.子类中的方法的访问级别不能低于父类中该方法的访问级别

3>.子类中方法抛出的异常范围不能大于父类中方法抛出的异常范围

6.能否将name属性改成world

package com.test;

public class PrivateTest

{

private String name = "hello";

public String getName()

{

return name;

}

}

修改代码(java 反射 reflect)

package com.test;

import java.lang.reflect.Field;

public class ReflectionTest

{

public static void main(String[] args) throws Exception

{

PrivateTest pt = new PrivateTest();

Class<?> clazz = PrivateTest.class;

Field field = clazz.getDeclaredField("name");

field.setAccessible(true);

field.set(pt,"world");

System.out.println(pt.getName());

}

}

获得某个类class对象的方法

1.使用类的.class语法

2.通过类的对象的getClass()方法

3.通过Class对象的forName方法

4.对于包装类,可以通过.TYPE语法方式

7.public class Test{

private final int a;

private String name;

public Test(){

a = 3;

}

public Test(String name){

//a = 3;

this.name = name;

}

}

编译能否通过,为什么

对于final类型的成员变量的初始化方式

1> 声明变量是直接赋值

2> 在构造方法中完成赋值,如果一个类有多个构造方法,就要保证在每个构造方法中对final类型的

成员变量的赋值

解决方法,1 private final int a=0;

2 a = 3;

this.name = name;

public class Test{

private static final int a=3;

private String name;

public Test(){

//a = 3;

}

public Test(String name){

//a = 3;

this.name = name;

}

}

对于static final 类型的变量只能在声明的时候给其赋值

public class Test{

public static final StringBuffer s = new StringBuffer();

public static void main(String[] args){

s.append("hello");

}

编译可以通过,对于final类型引用本身不能变,不能指向其他对象,所指向的对象可以变

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: