Python工程师Java之路(a)入门语法速查

本文详细介绍了Java编程语言的基础知识与高级特性,包括面向对象编程、基本数据类型、运算符、条件与循环结构、异常处理等内容,并通过具体实例进行讲解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

面向对象

  • (class):具有相同特性的事物的抽象描述,是一组相关属性行为的集合
  • 对象(object):类的具体体现

举例

类:猫科动物
属性:姓名、年龄、颜色
行为:猎、吃、喝、跑
对象:美洲豹
对象属性:颜色 -> 黄底+斑点
对象行为:吃 -> 刺杀鳄鱼

成员变量

public class Hello {
    public static void main(String[] args) {
        System.out.println(A.classVariate);
        System.out.println((new A()).variate);
    }
}

class A {
    static String classVariate = "静态成员变量";
    String variate = "成员变量";
}

方法(method)

构造方法(constructor)

public class Cat {
    public Cat() {
        System.out.println("类似Python的__init__");
    }
}

方法重载(overload)

public class Hello {
    void method(String a) {
        System.out.println(a);
    }
    void method(int b) {
        System.out.println(b);
    }
}

继承

class Animal {}
class Cat extends Animal {}

多态

public class Hello {
    public static void main(String[] args) {
        // 本态
        Animal a = new Animal();
        // 多态
        Animal c = new Cat();
        // 编译看左,运行看右
        a.eat(); // 吃
        c.eat(); // 吃鱼
        //c.attack(); // 编译不能通过
        // 类属性没有多态(初学建议:父子类属性不要重名)
        System.out.println(c.name); // 动物
    }
}

class Animal {
    String name = "动物";
    void eat() {System.out.println("吃");}
}

class Cat extends Animal {
    String name = "猫";
    void eat() {System.out.println("吃鱼");}
    void attack() {System.out.println("喵爪");}
}

基础语法

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

代码注释(annotation)

/**
 * 可以用来自动创建文档的注释
 * javadoc -d directory_name A.java
 */
public class A {
    // 单行注释
    public static void main(String[] args) {
        /*
        多行注释
        */
        System.out.println("Hello, world!");
    }
}

关键字(keyword)

Java已定义好的单词,具有特殊含义;
特点:全是小写字母

命名规范

  • 英文 数字 _ $ .
  • 下划线 不独行
  • 数字 不开头
  • 区分大小写
  • 不重名
  • 禁用java关键词
命名规范类&接口方法&变量常量
单个单词全小写首字母大写小写全大写
多个单词全小写,用.分隔大驼峰驼峰全大写,用_分隔
e.g.user.nameUserNameuserNameUSER_NAME

条件(if、switch)

  • ifelse ifelse
int n = 95;
if (n >= 90) {
    System.out.print("优秀");
} else if (n >= 60) {
    System.out.print("及格");
} else {
    System.out.print("挂科");
  • switchcasecase
    在switch语句中,如果case后面不写break,将出现穿透现象
int quarter1 = 3;
switch (quarter1) {
    case 4:
        System.out.print("冬<-");
    case 3:
        System.out.print("秋<-");
    case 2:
        System.out.print("夏<-");
    case 1:
        System.out.println("春");
}
// 打印结果:秋<-夏<-春
int quarter2 = 3;
switch (quarter2) {
    case 4:
        System.out.print("冬");
        break;
    case 3:
        System.out.print("秋");
        break;
    case 2:
        System.out.print("夏");
        break;
    case 1:
        System.out.print("春");
        break;
}
// 打印结果:秋

循环(while、for)

  • while:先判断,后循环
  • do while:先循环,后判断
int sum = 0;
int n = 1;
do {
    sum = sum + n;
    n ++;
} while (n <= 100);
System.out.println(sum);  // 5050
  • forforeach
String[] array = {"a", "d", "c", "a", "d", "c", "a", "d", "c"};
for (String i : array) {
    System.out.print(i);
}
// foreach打印结果:adcadcadc
  • breakcontinue
here:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        System.out.println("" + i + j);
        if (i > 0 & j > 0) {
            break here;
        }
    }
}

break打断外层循环,留意here,打印结果如下:

00
01
02
10
11

here:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (j > 0) {
            continue here;
        }
        System.out.println("" + i + j);
    }
}

continue继续外层循环,留意here,打印结果如下:

00
10
20
30
40

异常

  • 捕获异常:trycatchfinally
  • 抛出异常:throw
  • https://yellow520.blog.youkuaiyun.com/article/details/114661078
try {
    int i = 9 / 0;
} catch (ArithmeticException e) {
    System.out.println(e);
} finally {
    System.out.println("finally");
}

数据相关

基本数据类型

名称二进制位数包装类MIN_VALUEMAX_VALUE默认值
(5系下下都有)
byte8java.lang.Byte-1281270
short16java.lang.Short-32768327670
int32java.lang.Integer − 2 31 -2^{31} 231 2 31 2^{31} 2310
long64java.lang.Long − 2 63 -2^{63} 263 − 2 63 -2^{63} 2630L
float32java.lang.Float1.4E-453.4028235E380.0f
double64java.lang.Double4.9E-3241.7976931348623157E3080.0d
char16java.lang.Character065535'u0000'
booleanfalse

1.4E-45表示: 1.4 × 1 0 − 45 1.4 \times 10^{-45} 1.4×1045

基本数据类型转换

  • 自动类型转换(隐式类型转换)

自动类型转换图

// 自动升级为int
char c1 = '0';
char c2 = 'A';
System.out.println(c1 + c2); // 113
  • 强制类型转换(显示类型转换)
//损失精度
int i = (int)3.14;
System.out.println(i); // 3
// 溢出
byte b = (byte)128;
System.out.println(b); // -128
  • String相关转换
System.out.println("" + 1 + 23); // 123

运算符(Operator)优先级

优先级类别操作符关联性
后缀() [] .左到右
2一元expr++ expr--从左到右
3一元++expr --expr + - ~ !从右到左
4乘性* / %左到右
5加性+ -左到右
6移位>> >>> <<左到右
7关系> >= < <=左到右
8相等== !=左到右
9按位与&左到右
10按位异或^左到右
11按位或|左到右
12逻辑与&&左到右
13逻辑或||左到右
14条件? :从右到左
15赋值= += -= *= /= %= >>= <<= &= ^= |=从右到左
逗号,左到右

+ + 和 - -

double x = 1.5;
double y = ++x;
System.out.printf("%.2f", x); // 2.50
System.out.printf("%.2f", y); // 2.50
double x = 1.5;
double y = x--;
System.out.printf("%.2f", x); // 0.50
System.out.printf("%.2f", y); // 1.50

数组

public class a {
    public static void main(String[] args) {
        int[][] arr = {
                {0, 1, 2, 3},
                {4, 5, 6, 7},
                {8, 9},
        };
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
            }
        }
    }
}

0123456789

泛型

https://yellow520.blog.youkuaiyun.com/article/details/114996580

ArrayList(类似Python的list)

import java.util.ArrayList;

// 创建ArrayList对象
ArrayList<String> a = new ArrayList<>();
// 添加元素
a.add("2");
a.add("3");
System.out.println(a); // [2, 3]
// 查看
System.out.println(a.get(1)); // 3
// 大小(长度)
System.out.println(a.size()); // 2
// 按索引移除元素
a.remove(1);
// 遍历
for (String s : a) { System.out.println(s); } // 2

HashMap(类似Python的dict)

import java.util.HashMap;

// 创建
HashMap<String, Integer> map = new HashMap<>();
// 添加
map.put("剑圣", 8);
map.put("先知", 6);
map.put("巫妖", 5);
System.out.println(map); // {巫妖=5, 先知=6, 剑圣=8}
// 获取
System.out.println(map.get("剑圣")); // 8
System.out.println(map.get("大法师")); // null
// 移除
System.out.println(map.remove("先知")); // 6
System.out.println(map); // {巫妖=5, 剑圣=8}
// 获取全部键或值
System.out.println(map.keySet()); // [巫妖, 剑圣]
System.out.println(map.values()); // [5, 8]
// 遍历键值对
for (Map.Entry<String, Integer> stringIntegerEntry : map.entrySet()) {
    System.out.println(stringIntegerEntry);
} // 巫妖=5 剑圣=8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小基基o_O

您的鼓励是我创作的巨大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值