Java基础语法

本文详细介绍了Java编程的基础语法,包括注释的使用(单行和多行注释、JavaDoc)、标识符和关键字的规则,数据类型(基本类型和引用类型),以及算术、关系、逻辑和位运算符。还涵盖了变量、作用域、常量、包机制和JavaDoc的使用。
摘要由CSDN通过智能技术生成

Java基础语法

注释、标识符、关键字

单行注释

//

多行注释

/* */

JavaDoc:文档注释

/**
*
*/

书写注释是一个非常好的习惯 BAT

平时写代码一定要注意规范

标识符和关键字

关键字

在这里插入图片描述

Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。

  • 所有标识符都应该以字母(A-Z或者a-z),美元符($)、或者下划线(_)开始
  • 首字母之后可以是字母A-Z或者a-z),美元符($)、或者下划线(_)或数字的任何字符组合
  • 不能使用关键字作为变量名或方法名
  • 标识符是大小写敏感
  • 可以使用中文名来命名,但是一般不建议这样去使用,也不建议使用拼音,很Low

数据类型

  • 强类型语言

    • 要求变量的使用严格符合规定,所有变量都必须先定义后才能使用
  • 弱类型语言

  • Java的数据类型分为两大类

    • 基本类型
    • 引用类型

    在这里插入图片描述

//八大基本数据类型
        //整数类型 byte,short,int,long
        int num1 = 1;
        byte  num2 = 2;
        short num3 = 30;
        long num4 = 40L;

        //浮点数类型 float,double
        float num5 = 50.5f;
        double num6 = 60.6;

        //字符类型 char
        char num7 = 'A';

        //字符串,不是关键词,是一个类
        //String str = "Hello World";

        //布尔类型 boolean
        //只有两个值 true false
        boolean bool = true;
        boolean bool2 = false;

什么是字节

在这里插入图片描述

进制

//整数扩展: 进制 二进制0b 十进制 八进制0 十六进制0x

int a = 10;
        int b = 0b10;//二进制0b
        int c = 010;//八进制0
        int d = 0x10;//十六进制0x
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);

//浮点数扩展
//BigDecimal
///float 有限 离散 舍入误差 大约 接近但不等于
//double 有限 离散 舍入误差 大约 接近但不等于
//最好完全避免使用浮点数进行比较

float f1 = 0.1f;//0.1
double f2 = 1.0/10;//0,1

System.out.println(f1==f2);
System.out.println(f1);
System.out.println(f2);

float  d1 = 1234567893242342f;
double d2 = d1+1;

System.out.println(d2==d1);
System.out.println(d1);
System.out.println(d2);

//字符扩展

char c1 = 'a';
char c2 = '中';

System.out.println(c1);

System.out.println((int)c1);

System.out.println(c2);

System.out.println((int)c2);

//所有的字符本质都是数字
//编码 Unicode 表:(97 = a 65 = A) 2字节 0 - 65536 Excel 2 16 = 65536
//编码 Unicode 扩展 表:(97 = a 65 = A) 4字节 0 - 111411
//编码 ASCII 1字节
//编码 ISO8859-1 1-2字节
//编码 GB2312 2字节
//编码 Unicode 4字节
//编码 UTF-8 1-4字节

char c3 = '\u0061';
char c4 = '\u4e2d';

System.out.println(c3);
System.out.println(c4);

转义字符:
\t 制表符
\n 换行符
\ 斜杠
" 双引号
’ 单引号

String s1 = "abc";
String s2 = "def";
String s3 = "abcdef";

String s4 = "abc\ndef";
String s5 = "abc\\def";
String s6 = "abc\"def\"";
String s7 = "abc\'def\'";

System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
System.out.println(s7);

//布尔值扩展

boolean b1 = true;
boolean b2 = false;

if (b1 ) {
    System.out.println(b1);
}

if (!b2) {
    System.out.println(b2);
}

类型转换

//类型转换
//类型转换优先级
//强制类型转换:代码合法,但是有损失,需要强制转换
//自动类型转换:代码合法,自动完成,没有损失

//强制类型转换:double–float–long–int–char–short–byte
//强制类型转换:高类型–>低类型 (类型)变量名

int a = 128;
byte b = (byte) a;
System.out.println(b);

//自动类型转换:byte,short,char–int–long–float–double
//自动类型转换:低类型–>高类型

int c = 123;
double d = c;
System.out.println(d);
  • 注意事项:
  • 1.强制类型转换通常是不安全的,可能会导致数据丢失
  • 2.强制类型转换通常需要进行类型检查,以确保转换是合法的
  • 3.强制类型转换通常需要进行类型转换,以确保转换是安全的
  • 4.不能对布尔类型进行转换
System.out.println((int) 3.14);
System.out.println((int) -45.98);

//自动类型转换

char f ='a';
int e = f + 1;
System.out.println(e);
System.out.println((char) e);

//操作比较大的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割

int money = 10_0000_0000;
int years = 20;
int total = money * years;//-1474836480,计算的时候溢出了
System.out.println(total);
long total2 = money * years;//默认是int,转换之前就已将出现问题了?
System.out.println(total2);
long total3 = money * (long)years;//先把一个数转换为long类型,再进行运算
System.out.println(total3);

变量

在这里插入图片描述

变量的作用域

在这里插入图片描述

常量

在这里插入图片描述

//常量
//常量的命名规范和变量一样遵循驼峰命名法
//常量的名称一般是大写
//常量在定义的时候必须赋值
//常量在赋值的时候,变量的值不允许被修改
//修饰符,不存在先后顺序
static final double PI = 3.14;

变量的命名规范

在这里插入图片描述

Java运算符

在这里插入图片描述

算数运算符

//二元运算符
int a = 10;
int b = 20;
int c = 30;
int d = 50;



System.out.println("a和b的和:" + (a + b));
System.out.println("a和b的差:" + (a - b));
System.out.println("a和b的积:" + (a * b));
System.out.println("a和b的商:" + (a / (double)b));
System.out.println("a和b的余数:" + (a % b));

//如果数据运算中有long,float,double类型,最终结果为long,float,double
//如果数据运算中没有long类型,最终结果为int

long a = 123123123123123L;
int b = 123;
short c = 10;
byte d = 8;

//如果数据运算中有long,float,double类型,最终结果为long,float,double
//如果数据运算中没有long类型,最终结果为int
System.out.println(a + b + c + d);//long
System.out.println(b + c + d);//int
System.out.println( c + d);//int
//++ --  自增,自减  一元运算符
int a = 10;
a++;  //a = a + 1;
System.out.println(a); //11
--a;  //a = a - 1;
System.out.println(a); //10
//a++ 先使用后++   b = a++;
int b = a++ + 1;
System.out.println(b); //11
System.out.println(a); //12
//++a 先++后使用   c = ++a;
int c = ++a + 1;
System.out.println(c); //14
System.out.println(a); //14
//--a 先--后使用   d = --a;
int d = --a + 1;
System.out.println(d); //12
System.out.println(a); //12
//++ -- 只能用于变量前面  单独使用会报错
//int e = ++;  //错误写法
//int f = --;  //错误写法
//int g = +1;  //错误写法
//int h = -1;  //错误写法

//幂运算2^3  2*2*2 =8
double pow = Math.pow(2, 3);
System.out.println(pow);

Java中的Math类提供了一系列静态方法,用于执行基本数学运算。以下是一些常用的Math方法:

1. Math.abs(x):返回x的绝对值。
2. Math.max(x, y):返回x和y中的最大值。
3. Math.min(x, y):返回x和y中的最小值。
4. Math.sqrt(x):返回x的平方根。
5. Math.pow(x, y):返回x的y次方。
6. Math.random():返回一个01之间的随机浮点数。
7. Math.round(x):返回x四舍五入后的值。
8. Math.ceil(x):返回大于或等于x的最小整数。
9. Math.floor(x):返回小于或等于x的最大整数。
10. Math.sin(x):返回x的正弦值。
11. Math.cos(x):返回x的余弦值。
12. Math.tan(x):返回x的正切值。
13. Math.asin(x):返回x的反正弦值。
14. Math.acos(x):返回x的反余弦值。
15. Math.atan(x):返回x的反正切值。
16. Math.toDegrees(x):将x从弧度转换为角度。
17. Math.toRadians(x):将x从角度转换为弧度。
18. Math.exp(x):返回e的x次方。
19. Math.log(x):返回x的自然对数。
20. Math.log10(x):返回x的以10为底的对数。

以下是一个使用Math方法的示例:

public class MathExample {
    public static void main(String[] args) {
        double x = 5.0;
        double y = 3.0;

        System.out.println("绝对值: " + Math.abs(x));
        System.out.println("最大值: " + Math.max(x, y));
        System.out.println("最小值: " + Math.min(x, y));
        System.out.println("平方根: " + Math.sqrt(x));
        System.out.println("幂运算: " + Math.pow(x, y));
        System.out.println("随机数: " + Math.random());
        System.out.println("四舍五入: " + Math.round(x));
        System.out.println("向上取整: " + Math.ceil(x));
        System.out.println("向下取整: " + Math.floor(x));
        System.out.println("正弦值: " + Math.sin(Math.toRadians(30)));
        System.out.println("余弦值: " + Math.cos(Math.toRadians(60)));
        System.out.println("正切值: " + Math.tan(Math.toRadians(45)));
    }
}

关系运算符

//关系运算符符返回的结果: 正确,错误 布尔值
//关系运算符:> < >= <= == !=
//关系运算符的结果都是 布尔值

int a = 10;
int b = 20;
int c = 12;

System.out.println(c%a);

System.out.println(a > b);//false
System.out.println(a < b);//true
System.out.println(a == b);//false
System.out.println(a >= b);//false
System.out.println(a <= b);//true
System.out.println(a != b);//true

逻辑运算符

//逻辑运算符
public class Demo05 {
    public static void main(String[] args) {
        // 与(and)  或(or)  非(取反)
        boolean a = true;
        boolean b = false;

        System.out.println(a && b);  //false
        System.out.println(a || b);  //true
        System.out.println(!a);  //false
        System.out.println(!b);  //true
        System.out.println("--------------------");

        //短路与  短路或
        int x = 10;
        boolean d = (x<4)&&(x++<4);
        System.out.println(d);
        System.out.println(x);
    }
}

位运算符

/*
* A = 0011 1100
* B = 0000 1101
*
* A&B 0000 1100
* A|B 0011 1101
* A^B 0011 0001 相同为0 不同为1
* ~B  1111 0010 取反
*
* 2*8 = 16  2*2*2*2
* <<    *2
* >>    /2
*
* 0000 0000     0
* 0000 0001     1
* 0000 0010     2
* 0000 0011     3
* 0000 0100     4
* 0000 1000     8
* 0001 0000     16
* */
System.out.println(6<<3);// 6*2*2*2 = 48

扩展赋值运算符

int a = 10;
int b = 20;

a += b; // a = a + b
System.out.println("a = " + a); // 输出:a = 30

b -= a; // b = b - a
System.out.println("b = " + b); // 输出:b = 10

连接符

//字符串连接符    + , String
String str1 = "Hello";
String str2 = "World";
String str3 = str1 + " " + str2;
System.out.println("str3 = " + str3); // 输出:str3 = Hello World

System.out.println(""+a+b);
System.out.println(a+b+"");

三元运算符(偷懒运算符)

// x ? y : z
// x为true,则整个表达式的值为y,否则为z

int a = 10;
int b = 20;
int max = (a > b) ? a : b;
System.out.println("最大值是:" + max);

//必须掌握
int score = 80;
String  grade = (score >= 90) ? "优秀" : ((score >= 80) ? "良好" : ((score >= 70) ? "中等" : ((score >= 60) ? "及格" : "不及格")));
System.out.println("成绩等级是:" + grade);

包机制

在这里插入图片描述

JavaDoc

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值