Java开发面试

Session和Cookie的区别
1、cookie数据存放在客户的浏览器上,session数据放在服务器上。
2、cookie不是很安全,别人可以分析存放在本地的cookie并进行cookie欺骗
考虑到安全应当使用session。
3、session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能
考虑到减轻服务器性能方面,应当使用cookie。
4、单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。
简要说明String StringBuffer StringBulider的区别
1.从元素特性讲
String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法。
String类其实是通过char数组来保存字符串的;
对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象 。
2.从线程安全讲
StringBuffer对象的内容可以修改;而String对象一旦产生后就不可以被修改,重新赋值其实是两个对象。
StringBuilder:线程非安全的
StringBuffer:线程安全的
为什么线程安全:因为StringBuffer使用了synchronized
3.适用场景
String:适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况
如何跳出当前的多重循环?
在Java中,要想跳出多重循环,可以在外面的循环语句前定义一个标号,然后在里层循环体的代码中使用带有标号break语句,即可跳出外层循环
在这里插入图片描述
汉字Utf-8和GB2312字符集分别占用多少个字节?**
1.计算机,不能直接存储汉字,而是存储的是编码
UTF-8是国际通用字库,里面涵盖了所有地球上所有人类的语言文字,比如阿拉伯文、汉语……
gb2312 是国标,是中国的字库,里面仅涵盖了汉字和一些常用外文,比如日文片假名,和常见的符号。
字库规模: UTF-8(字全) > gb2312(只有汉字)
2.UTF-8里面存储一个汉字3个字节。而gb2312中存储一个汉字2个字节。
保存大小: UTF-8(更臃肿、加载更慢) > gb2312 (更小巧,加载更快)
写出将GB2312的字符串转换为UTF-8的字符串的代码

// 将 UTF-8 编码的字符串转换为 GB2312 编码格式:
public static String utf8Togb2312(String str){
        StringBuffer sb = new StringBuffer();
        for ( int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            switch (c) {
               case '+' :
                   sb.append( ' ' );
               break ;
               case '%' :
                   try {
                        sb.append(( char )Integer.parseInt (
                        str.substring(i+1,i+3),16));
                   }
                   catch (NumberFormatException e) {
                       throw new IllegalArgumentException();
                  }
                  i += 2;
                  break ;
               default :
                  sb.append(c);
                  break ;
             }
        }
        String result = sb.toString();
        String res= null ;
        try {
             byte [] inputBytes = result.getBytes( "8859_1" );
            res= new String(inputBytes, "UTF-8" );
        }
        catch (Exception e){}
        return res;
  }

写出将UTF-8的字符串转换为GB2312的字符串的代码

public static String gb2312ToUtf8(String str) {
String urlEncode = “” ;
try {
urlEncode = URLEncoder.encode (str, “UTF-8” );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return urlEncode;
}
将字符串的编码格式转换为utf-8
方式一:

/**
 * 将字符串的编码格式转换为utf-8
 * 
 * @param str
 * @return Name = new
 * String(Name.getBytes("ISO-8859-1"), "utf-8");
 */
public static String toUTF8(String str) {
    if (isEmpty(str)) {
        return "";
    }
    try {
        if (str.equals(new String(str.getBytes("GB2312"), "GB2312"))) {
            str = new String(str.getBytes("GB2312"), "utf-8");
            return str;
        }
    } catch (Exception exception) {
    }
    try {
        if (str.equals(new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"))) {
            str = new String(str.getBytes("ISO-8859-1"), "utf-8");
            return str;
        }
    } catch (Exception exception1) {
    }
    try {
        if (str.equals(new String(str.getBytes("GBK"), "GBK"))) {
            str = new String(str.getBytes("GBK"), "utf-8");
            return str;
        }
    } catch (Exception exception3) {
    }
    return str;
}
/**
 * 判断是否为空
 * 
 * @param str
 * @return
 */
public static boolean isEmpty(String str) {
    // 如果字符串不为null,去除空格后值不与空字符串相等的话,证明字符串有实质性的内容
    if (str != null && !str.trim().isEmpty()) {
        return false;// 不为空
    }
    return true;// 为空
}

方式二:

 - import java.io.UnsupportedEncodingException;  
       /** 
        * 转换字符串的编码 
        */
       public class ChangeCharset { 
        /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */
        public static final String US_ASCII = "US-ASCII"; 
         
        /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */
        public static final String ISO_8859_1 = "ISO-8859-1"; 
         
        /** 8 位 UCS 转换格式 */
        public static final String UTF_8 = "UTF-8"; 
         
        /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */
        public static final String UTF_16BE = "UTF-16BE"; 
         
        /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */
        public static final String UTF_16LE = "UTF-16LE"; 
         
        /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */
        public static final String UTF_16 = "UTF-16";  
        /** 中文超大字符集 */
        public static final String GBK = "GBK"; 
        /** 
        * 将字符编码转换成US-ASCII码 
        */
        public String toASCII(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, US_ASCII); 
        } 
        /** 
        * 将字符编码转换成ISO-8859-1码 
        */
        public String toISO_8859_1(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, ISO_8859_1); 
        } 
        /** 
        * 将字符编码转换成UTF-8码 
        */
        public String toUTF_8(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, UTF_8); 
        } 
        /** 
        * 将字符编码转换成UTF-16BE码 
        */
        public String toUTF_16BE(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, UTF_16BE); 
        } 
        /** 
        * 将字符编码转换成UTF-16LE码 
        */
        public String toUTF_16LE(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, UTF_16LE); 
        } 
        /** 
        * 将字符编码转换成UTF-16码 
        */
        public String toUTF_16(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, UTF_16); 
        } 
        /** 
        * 将字符编码转换成GBK码 
        */
        public String toGBK(String str) throws UnsupportedEncodingException{ 
        return this.changeCharset(str, GBK); 
        }   
        /** 
        * 字符串编码转换的实现方法 
        * @param str 待转换编码的字符串 
        * @param newCharset 目标编码 
        * @return 
        * @throws UnsupportedEncodingException 
        */
        public String changeCharset(String str, String newCharset) 
         throws UnsupportedEncodingException { 
        if (str != null) { 
         //用默认字符编码解码字符串。 
         byte[] bs = str.getBytes(); 
         //用新的字符编码生成字符串 
         return new String(bs, newCharset); 
        } 
        return null; 
        } 
        /** 
        * 字符串编码转换的实现方法 
        * @param str 待转换编码的字符串 
        * @param oldCharset 原编码 
        * @param newCharset 目标编码 
        * @return 
        * @throws UnsupportedEncodingException 
        */
        public String changeCharset(String str, String oldCharset, String newCharset) 
         throws UnsupportedEncodingException { 
        if (str != null) { 
         //用旧的字符编码解码字符串。解码可能会出现异常。 
         byte[] bs = str.getBytes(oldCharset); 
         //用新的字符编码生成字符串 
         return new String(bs, newCharset); 
        } 
        return null; 
        } 
        public static void main(String[] args) throws UnsupportedEncodingException { 
        ChangeCharset test = new ChangeCharset(); 
        String str = "This is a 中文的 String!"; 
        System.out.println("str: " + str); 
        String gbk = test.toGBK(str); 
        System.out.println("转换成GBK码: " + gbk); 
        System.out.println(); 
        String ascii = test.toASCII(str); 
        System.out.println("转换成US-ASCII码: " + ascii); 
        gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK); 
        System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk); 
        System.out.println(); 
        String iso88591 = test.toISO_8859_1(str); 
        System.out.println("转换成ISO-8859-1码: " + iso88591); 
        gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK); 
        System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk); 
        System.out.println(); 
        String utf8 = test.toUTF_8(str); 
        System.out.println("转换成UTF-8码: " + utf8); 
        gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK); 
        System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk); 
        System.out.println(); 
        String utf16be = test.toUTF_16BE(str); 
        System.out.println("转换成UTF-16BE码:" + utf16be); 
        gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK); 
        System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk); 
        System.out.println(); 
        String utf16le = test.toUTF_16LE(str); 
        System.out.println("转换成UTF-16LE码:" + utf16le); 
        gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK); 
        System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk); 
        System.out.println(); 
        String utf16 = test.toUTF_16(str); 
        System.out.println("转换成UTF-16码:" + utf16); 
        gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK); 
        System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk); 
        String s = new String("中文".getBytes("UTF-8"),"UTF-8"); 
        System.out.println(s); 
        } 
       }

Math.round(11.5)等于多少?Math.round(-11.5)等于多少?
12;-11
请列举一些会造成内存溢出的情况说说解决方法
1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据;
2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收;
3.代码中存在死循环或循环产生过多重复的对象实体;
4.使用的第三方软件中的BUG;
5.启动参数内存值设定的过小
解决方案:
第一步,修改JVM启动参数,直接增加内存。(-Xms,-Xmx参数一定不要忘记加。)
第二步,检查错误日志,查看“OutOfMemory”错误前是否有其它异常或错误。
第三步,对代码进行走查和分析,找出可能发生内存溢出的位置。
重点排查以下几点:
1.检查对数据库查询中,是否有一次获得全部数据的查询。一般来说,如果一次取十万条记录到内存,就可能引起内存溢出。这个问题比较隐蔽,在上线前,数据库中数据较少,不容易出问题,上线后,数据库中数据多了,一次查询就有可能引起内存溢出。因此对于数据库查询尽量采用分页的方式查询。
2.检查代码中是否有死循环或递归调用。
3.检查是否有大循环重复产生新对象实体。
4.检查对数据库查询中,是否有一次获得全部数据的查询。一般来说,如果一次取十万条记录到内存,就可能引起内存溢出。这个问题比较隐蔽,在上线前,数据库中数据较少,不容易出问题,上线后,数据库中数据多了,一次查询就有可能引起内存溢出。因此对于数据库查询尽量采用分页的方式查询。
5.检查List、MAP等集合对象是否有使用完后,未清除的问题。List、MAP等集合对象会始终存有对对象的引用,使得这些对象不能被GC回收。
第四步,使用内存查看工具动态查看内存使用情况。
针对百万级的表,可以采取哪些方法优化查询效率?
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。
2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 orderby 涉及的列上建立索引。
3.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:

select id from t where num isnull
    可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id from t where num=0

4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:

select id from t where num=10or num=20
    可以这样查询:
select id from t where num=10
unionall
select id from t where num=20

5.下面的查询也将导致全表扫描:(不能前置百分号)

select id from t where name like ‘%abc%’

若要提高效率,可以考虑全文检索。
6.in 和 notin 也要慎用,否则会导致全表扫描,如:

select id from t where num in(1,2,3)
    对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between1and3
select xx,phone FROM send  a JOIN (
select'13891030091' phone  unionselect'13992085916' …………  UNIONSELECT'13619100234' ) b 
on  a.Phone=b.phone
--替代下面  很多数据隔开的时候
in('13891030091','13992085916','13619100234'…………)

7.如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然 而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num 可以改为强制查询使用索引:
select id from t with(index(索引名)) wherenum=@num
8.应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:

select id from t where num/2=100
    应改为:
select id from t where num=100*2

9.应尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:

select id from t wheresubstring(name,1,3)=’abc’–name以abc开头的id
select id from t wheredatediff(day,createdate,’2005-11-30′)=0–’2005-11-30′生成的id
    应改为:
select id from t where name like ‘abc%’
select id from t where createdate>=’2005-11-30′ and createdate<’2005-12-1′

10.不要在 where 子句中的“=”左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。
11.在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,否则该索引将不会被使 用,并且应尽可能的让字段顺序与索引顺序相一致。
12.不要写一些没有意义的查询,如需要生成一个空表结构:

select col1,col2 into #t from t where1=0
    这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样:
createtable #t(…)

13.很多时候用 exists 代替 in 是一个好的选择:

select num from a where num in(select num from b)
    用下面的语句替换:
select num from a whereexists(select1from b where num=a.num)

14.并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段 sex,male、female几乎各一半,那么即使在sex上建了索引也对查询效率起不了作用。
15.索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有 必要。
16.应尽可能的避免更新 clustered 索引数据列,因为 clustered 索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列,那么需要考虑是否应将该索引建为 clustered 索引。
17.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会 逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。
18.尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。
19.任何地方都不要使用 selectfrom t ,用具体的字段列表代替“”,不要返回用不到的任何字段。
20.尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。
21.避免频繁创建和删除临时表,以减少系统表资源的消耗。
22.临时表并不是不可使用,适当地使用它们可以使某些例程更有效,例如,当需要重复引用大型表或常用表中的某个数据集时。但是,对于一次性事件,最好使 用导出表。
23.在新建临时表时,如果一次性插入数据量很大,那么可以使用 selectinto 代替 createtable,避免造成大量 log ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先create table,然后insert。
24.如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,先 truncatetable ,然后 droptable ,这样可以避免系统表的较长时间锁定。
25.尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。
26.使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效。
27.与临时表一样,游标并不是不可使用。对小型数据集使用 FAST_FORWARD 游标通常要优于其他逐行处理方法,尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括“合计”的例程通常要比使用游标执行的速度快。如果开发时 间允许,基于游标的方法和基于集的方法都可以尝试一下,看哪一种方法的效果更好。
28.在所有的存储过程和触发器的开始处设置 SET NOCOUNT ON ,在结束时设置 SET NOCOUNT OFF 。无需在执行存储过程和触发器的每个语句后向客户端发送 DONE_IN_PROC 消息。
29.尽量避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理。
30.尽量避免大事务操作,提高系统并发能力。
请用JAVA写出整数组中找出无重复的元素。
倒序遍历数组,放入map
如果map存在,value为出现次数,加1
如果map不存在,index=i,记录当前元素位置。
最后index的值就是第一个不重复的元素位置

package com.puhui.goosecard.web;
import java.util.HashMap;
import java.util.Map;
public class Test {
    public static void main(String[] args) {
        char[] array = {'d', 'a', 'b', 'c', 'c', 'b'};
        Map<Object, Object> map = new HashMap<>();
        int index = -1;
        for (int i = array.length - 1; i >= 0; i--) {
            if (map.containsKey(array[i])) {
                int count = (Integer) map.get(array[i]);
                map.put(array[i], count + 1);
                index = i;
            } else {
                map.put(array[i], 1);
            }
        }
        System.out.println(index);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值