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();
        }
    }
    
    }
    

}

 

转载于:https://www.cnblogs.com/fly-boy/p/7552565.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值