java笔试题(1)基础知识

这篇博客主要讨论了Java中的基础知识,包括static修饰符的使用限制,substring()方法的用法,以及Java与JavaScript中字符串截取的区别。还提供了一道关于浮点数赋值的笔试题,指出在Java中不加f的浮点数会被默认解释为double类型导致编译错误。

1.

public static int function(){
         static int i=0;
         i++;
         return i;
     }

编译通不过,因为static只能修饰成员变量或成员函数,不能修饰非成员变量和函数。

2. 

public static void main(String[] args) {
static int  a[]=new int[15];
    System.out.print(a[10]);
}

编译通不过,因为static只能修饰成员变量或成员函数,不能修饰非成员变量和函数。改成如下两种都可以:

(1)  把static int a[]=new int[15];放到main外面定义成成员变量。

static int  a[]=new int[15];
publicstatic void main(String[] args) {
    System.out.print(a[10]);
}

输出结果为0

(2)  把static变成final

publicstatic void main(String[] args) {
final int  a[]=new int[15];
    System.out.print(a[10]);
}

输出结果为0

3.

<span style="font-size:14px;">String a=new String("123");
        String b=new String("123");
        String p=b;
        long c=10L;
        int d=10;
        System.out.print(a==b);
        System.out.print(p==b);
        System.out.print(c==d);</span>

结果为false,true,true

4. substring(startindex) startIndex从0开始

substring(startindex,endindex)表示截取startindex开始到endindex结束的字符串,不包括endindex字符

另,js中的substr(startindex,length)表示截取startindex开始,长度为length的字符串

String A=newString("ABCDE");
 String C=A.substring(3);//A=”ABCDE”,C=”DE”
System.out.println(A);
A.concat("XYZ");
System.out.println(A); //A=”ABCDE”
       
StringBufferB=new StringBuffer("ABCDE");
B.substring(3);//B=”ABCDE”
B.append("XYZ");
System.out.print(B);//B=”ABCDEXYZ”
5.

public static void fun(StringBuffer  a ,StringBuffer  b){
         a=a.append(y);
         b=a;
     }
     
    public static void main(String[] args) {
        StringBuffer  x=new StringBuffer  ("A");
        StringBuffer  y=new StringBuffer  ("B");
        fun(x,y);
        System.out.println(x+","+y);
}
答案:AB,B

x      “A”  经过append之后

y     “B”

a   

b

 

x   “AB” 

y   “B”

a   

b

 6.下列编译不通过的是:

A float a=5+5.5   

B String b="123"+1;

C String c="121"+"234";

D int d=1+2;

E float e=2.2f;

F double a=5+5.5

答案A

float定义时必须要以f结尾,如携程float a=5+5.5f则通过,如果不加f则默认为是double类型

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值