奋斗的Fly

Java面试题总结

1.静态变量在方法中是否可以编译了?不行,静态变量是类变量,放在方法中会报编译错误

2.GBK—utf-8的字节流的转换

操作步骤就是先解码再编码
用new String(src,"GBK")解码得到字符串
用getBytes("UTF-8")得到UTF8编码字节数组
byte[] src = null,dst;
        try {
            dst = new String(src,"GBK").getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

3.拼接的字符串与本身一直都是字符串是否指向一致。

public static void main(String[] args){

String a = "java";
String b ="ja";
String c ="va";
String d = b+c;
System.out.println(d);
System.out.println(a==d);
}

拼接后,产生了新实例

4.递归求阶乘

 /**
     * 求5的阶乘? 5*4*3*2*1 = ?
     */
   public static int test(int number){

  //如果输入的是1,就直接返回1,否则就返回自己乘以一个减去一个1的数
       if(number == 1){
           return 1;
       }else{
           return number * test(number - 1);
       }

   }

5.jsp脚本<%=AB%>由于不是变量也不是其他,不能通过编译

6.String 类型的使用

subString(开始的索引,结束的索引),索引从0开始左闭右开区间

subString只有一个参数的是,从索引开始包含后面的所有,一样是左闭的区别

7.对于字符串的切割split的用法

String str="Java string split test";
String[] strarray=str.split(" ",3);//第一个参数表示以什么进行分割,第二个参数表示分割成几部分
for (int i = 0; i < strarray.length; i++)
System.out.print(strarray[i]);

输出结果是:

Javastringsplit test

8.将一个字符串以某种格式输出到文件中

/**
 * 用IO流的形式,将字符串以某种格式进行保存到文件中
 * @author Administrator
 *
 */
public class Test {
    
    public static void main(String[] args) throws IOException{

    File file = new File("D:/Hello");
    FileOutputStream fos = null;
    try {
         fos = new FileOutputStream(file);
        Scanner sc = new Scanner(System.in);
        String tel = sc.nextLine();
        String[] a = tel.split(" ", 3);
        String separator ="-";
        String telephone = "";
        
        for(int i = 0; i<a.length;i++){
            if(i != (a.length-1)){
            telephone +=(a[i]+separator);
            }else{
                telephone +=a[i];
            }
        }
        byte[] b = telephone.getBytes();
        fos.write(b);
        fos.flush();
        System.out.println(telephone);
    } catch ( IOException e) {
        // TODO Auto-generated catch block
        System.out.println("加载文件失败");
    }finally{
        if(fos != null){
            fos.close();
        }
    }
    
    }
    

}

 

posted on 2017-09-19 16:35  奋斗的Fly  阅读(159)  评论(0编辑  收藏  举报

导航