栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java基础回顾

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java基础回顾

☔内容介绍☔

基础回顾01


                本文目录

01 "&" 和 "&&" 有什么区别

02  switch是否能作用在byte上,是否能作用在String上

03 short s1  = 1;s1 = s1 + 1;有什么错?short s1 = 1;s1 += 1;有什么错

04 char型变量中能不能存储一个中文汉字?,为什么?

05 使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变


01 "&" 和 "&&" 有什么区别

例子

if (test1() && test2()) {
    System.out.println("打印...");
}

private static boolean test2() {
    System.out.println("test2...");
    return false;
}

private static boolean test1() {
    System.out.println("test1...");
    return false;
}

使用 "&" 会打印

test1...
test2...

Process finished with exit code 0

使用 "&&" 打印

test1...

Process finished with exit code 0

注:因此得知 "&" 第一个条件不成立 仍然会执行后面的条件,"&&" 第一个条件不成立,第二个条件不执行


02  switch是否能作用在byte上,是否能作用在String上
String str = "a";
    switch (str) {
        case "a":
            System.out.println("a...");
            break;
        case "b":
            System.out.println("b...");
            break;
        default:
            break;
}


byte key = 0;
int i = key;
switch (key) { // 数据类型自动提升为int
    case 0:
        System.out.println("0...");
        break;
    case 1:
        System.out.println("1...");
        break;
    default:
        break;
}

打印

a...
0...

Process finished with exit code 0

注:switch可以作用在byte上(数据类型自动提升),也可以作用在String上


03 short s1  = 1;s1 = s1 + 1;有什么错?short s1 = 1;s1 += 1;有什么错

先看这句 short s1  = 1;s1 = s1 + 1 

short s1 = 1;
s1 += 1;
System.out.println(s1);

输出

2

Process finished with exit code 0

注:+= 是Java语言规定的运算符,编译器会对他进行特殊处理所以不会报错

而 short s1 = 1;s1 += 1;

注:则会提示类型转换错误,自动提升右边的数据类型为int,然后赋值给short因为int不能直接赋值给short所以就会报类型转换错误


04 char型变量中能不能存储一个中文汉字?,为什么?
char c1 = '字';
System.out.println(c1);

输出

字

Process finished with exit code 0

注:因为char占用两个字节,而一个汉字就是2个字节所以可以存储


05 使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变
final StringBuffer sb1 = new StringBuffer();
sb1 = new StringBuffer();

则会提示

使用它的.append() 方法

则不会报错

因此:引用的指向不能发生改变,引用对象的内容可以变

另外用final修饰类时

修饰方法时

注:因此可以得知

1 被final所修饰的类,不能被继承

2 被final所修饰的方法,不能被重写

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939549.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号