Java 基础知识易错记录

本文概述了Java编程中的关键概念,如算术运算符、控制结构(continue和break)、成员变量与局部变量、switch语句、StringBuffer和StringBuilder的区别、方法重载与覆盖、throw和throws用法以及数组的创建方法,旨在帮助初学者避免常见错误。
摘要由CSDN通过智能技术生成

Java 基础知识易错记录

1、++运算符
2、continue和break
3、成员变量 局部变量
4、switch case
5、StringBuffer StringBuilder
6、重载 重写
7、throw throws
8、finally catch
9、数组创建
10、抽象类和接口
11、泛型

1、++运算符

public static void main(String[] args) {

        int a = 1;
        System.out.println(a++);
        System.out.println(++a);
    }

在这里插入图片描述
++在后,后加
++在前,先加

2、continue和break

continue:跳过,根据判断条件跳过这一次循环继续下一次
break:终止,根据判断条件达到条件终止当前循环
continue

public static void main(String[] args) {

        int [] numbers = {10, 20, 30, 40, 50};

        for(int x : numbers ) {
            if( x == 30 ) {
                continue;
            }

            System.out.print( x );
            System.out.print("\n");
        }
    }

在这里插入图片描述
break

public static void main(String[] args) {

        int [] numbers = {10, 20, 30, 40, 50};

        for(int x : numbers ) {
            if( x == 30 ) {
                break;
            }

            System.out.print( x );
            System.out.print("\n");
        }
    }

在这里插入图片描述

3、成员变量 局部变量

Java 局部变量 成员变量

4、switch case

Java switch使用

5、StringBuffer StringBuilder

StringBuffer :可变字符序列,线程安全
StringBuilder : 可变字符序列,线程不安全,相对于StringBuffer更快

6、重载 重写

Java 重载和重写

7、throw throws

throw 关键字用于在代码中抛出异常,而 throws 关键字用于在方法声明中指定可能会抛出的异常类型

public void test() throws Exception {
    throw new Exception();
}

一个方法可以声明抛出多个异常,多个异常之间用逗号隔开

import java.io.*;
public class className
{
   public void withdraw(double amount) throws RemoteException,
                              InsufficientFundsException
   {
       // Method implementation
   }
   //Remainder of class definition
}

8、finally catch try

Java finally catch try关键字

9、数组创建

格式:

// 首选的方法
dataType[] arrayRefVar;   

// 效果相同,但不是首选方法
// dataType arrayRefVar[] 风格是来自 C/C++ 语言 ,在Java中采用是为了让 C/C++ 程序员能够快速理解java语言
dataType arrayRefVar[]; 

示例:

// 首选的方法
double[] myList;   
      
// 效果相同,但不是首选方法
double myList[];         

静态创建:

int[] a = {1,2,3};
Object[ ] objects= {new Object(),new Object()};

动态创建:

int[] a = new int[2];
a[0]=1;
a[1]=2;

二维数组创建:

//type 可以为基本数据类型和复合数据类型
//typeLength1 和 typeLength2 必须为正整数
//typeLength1 为行数,typeLength2 为列数
type[][] typeName = new type[typeLength1][typeLength2];

示例:

int a[][] = new int[2][5];

String[][] s = new String[2][];
//为最高维分配引用空间,也就是为最高维限制其能保存数据的最长的长度
s[0] = new String[2];
s[1] = new String[3];
//为其每个数组元素单独分配空间
s[0][0] = new String("Good");
s[0][1] = new String("Luck");
s[1][0] = new String("to");
s[1][1] = new String("you");
s[1][2] = new String("!");

10、抽象类和接口

Java 抽象类和接口

11、泛型

<? extends T>和<? super T>的区别

<? extends T>表示该通配符所代表的类型是T类型的子类 <? super T>表示该通配符所代表的类型是T类型的父类 ```bash 737067250(交友) ```
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值