Java基础

Java基础

注释

  • 平时我们编写代码,在代码量比较少的时候,我们还可以看懂自己写的,但是当项目结构一旦复杂起来,我们就需要用到注释
  • 注释并不会被执行,是给我们写代码的人看的
  • 书写注释是一个非常好的习惯
  • 平时写代码一定要注意规范

Java注释有三种

  • 单行注释:样式修改 File -> settings -> Editor -> Java -> Comments ->Line comment
//单行注释只能注释一行
  • 多行注释:
/*
这是多行注释
可以注释多行
*/
  • 文档注释
/**
这是文档注释
@Description  Hello World
@Author 李光宇
*/

标识符

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

标识符注意点

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

关键字

abstract assert boolean break byte
case **catch ** char class const
**continue ** default do double eles
**enum ** extends final finally float
for goto **if ** implements import
**instanceof ** int interface long native
new package private protected public
return strictfp short static super
switch **synchronized ** this throw throws
transient try void **volatile ** while

数据类型(两大类)

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

基本类型(primitive type)

字节

  • 位(bit): 是计算机内部数据存储的最小单位,11001100是一个八位二进制数
  • 字节(byte): 是计算机中数据处理的基本单位,习惯上用大写B来表示
  • 1B(byte,字节) = 8bit(位)
  • 字符:是指计算机中使用的字母、数字、字和符号
  • 1bit 表示1位
  • 1Byte表示一个字节 1B=8b
  • 1024B=1KB
  • 1024KB=1M
  • 1024M=1G

数值类型

整数类型

JDK7新特性;数字之间可以用下划线分割

int money = 10_0000_0000;
System.out.println(money); // 1000000000;
  • byte 1字节 -128~127
  • short 2字节 -32768~32767
  • int 4字节 -2147483648~2147483647
  • long 8字节 -9223372036854775808~9223372036854775807
 long num=30L;
浮点类型
  • float 4字节
float num = 50.1F;
  • double 8字节
字符类型
  • char 2字节

布尔类型

  • boolean true 和 false

引用类型 (reference type)

  • 接口
  • 数组

拓展

整数拓展

  • 二进制 0b
int bit = 0b101;
  • 八进制 0
int bit = 0175;
  • 十进制
int bit = 125;
  • 十六进制 0x
int bit = 0xad123;

浮点数拓展

  • 不要用浮点数比较 有限 离散 舍入误差 大约 接近但不等于
//案例一
float f = 0.1f;   //f:0.1
double d = 1.0/10;//d:0.1
System.out.println(f==d);//false
//案例二
float f1 = 2313131313131313f;
float f2 = f1 + 1;
System.out.println(d1==d2); //true
  • BigDecimal :可以使用

字符拓展

  • 所有的字符本质还是数字
char c1= 'a';
char c2='中';
System.out.println(c1);       //a
System.out.println((int)c1);  //97
System.out.println(c2);       //中
System.out.println(int(c2));  //20013
  • Unicode 2 字节 0 ~65534
char c3= '\u0061';  \u是转意字符  \t制表符  \n换行 ....
System.out.println(c3)//a
  • 地址对象的不同
String sa = new String("helloworld");
String sb = new String("helloworld");
System.out.println(sa==sb); // false
String sc = "helloworld";
String sd = "helloworld";
System.out.println(sc==sd);  //true

布尔拓展

  • 功能相同,代码要精简易读(Less Is More)
boolean flag = true;
if(flag){}		 //老手
if(flag==true){} //新手

类型转换

  • 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换
// 低 ---------------------------->  高
byteshortchar -> int -> long ->float ->double
  • 运算中,不同类型的数据先转化为同一类型,然后进行计算

强制类型转换 (类型)变量名

高到低需要强制转换

  • 内存溢出(避免)
int i = 128;
byte b= (byte)i;
System.out.println(i); //128
System.out.println(b); //-128
int money = 10_0000_0000;
int years = 20;
int total = money * years; 
System.out.println(total); //-1474836480;
long total2 = money * years; //-1474836480;默认是int,转换之前问题已经存在
long total3 = money * (long)years; 
System.out.println(total3);//20000000000 
  • 失去精度
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); //-45

自动类型转换

低到高不需要手动转换

byte i = 127;
int b= i;
System.out.println(i); //128
System.out.println(b); //128
char c = 'a';
int d = c + 1;
System.out.println(d); //98
System.out.println((char)d); //b

注意点

  • 不能对布尔类型转换
  • 不能把对象类型转换为不相关的类型

变量

  • 变量是可以变化的量
  • Java是一种强类型语言,每个变量都必须声明其类型
  • Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
	type varName [=value] [{,varName[=value]}];
// 数据类型       变量名 = 值; 可以使用逗号隔开来声明多个同类型变量

变量作用域

  • 类变量
  • 实例变量
  • 局部变量
public class Variable{
	
    static int allClicks = 0;  //类变量 ,static
    
    String str = "hello world";//实例变量,从属于对象,不初始化就是默认值,	   				          boolean 默认 false;除了基本类型,其余的默认值都是null;
    
    public void method(){
        int i = 0//局部变量,方法里,必须声明和初始化值
    }
}

注意事项

  • 每个变量都有类型,类型可以是基本类型,也可以是引用类型

  • 变量名必须是合法的标识符

  • 变量声明是一条完整的语句,因此每一个声明都必须以分号结束

常量

  • 常量(Constant): 初始化(initialize)后不能再改变值,不会变动值
  • 常量可以理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变
final 常量名 = 值;
static final 常量名 = 值;
final static 常量名 = 值;//修饰符不存在先后顺序
final double PI = 3.14;
  • 常量名一般使用大写字母

命名规范

  • 所有变量、方法、类名;见明知意
  • 类成员变量;首字母小写和驼峰原则;monthSalary 除了第一个单词以外,后面的单词首字母大写 lastName
  • 常量:大写字母和下划线;MAX_VALUE
  • 类名:首字母大写和驼峰原则;Man,GoodMan;
  • 方法名:首字母小写和驼峰原则;run(),runRun()

运算符

Java语言支持如下运算符:

算术运算符

  • +、-、*、/、%、++、--

赋值运算符

  • =

关系运算符

  • < 、>、>=、<=、==、!= 、instanceof

逻辑运算符

  • && 、|| 、!

位运算符

  • &、|、^、~、>>、<<、>>>(了解!!!)

条件运算符

  • ? :

扩展赋值运算符

  • +=、-=、*=、/=

运算

  • 整数除以整数,运算结果有小数会舍弃,需转为(cast)浮点型
int a=10;
int b=20;
System.out.println(a/b);//0
System.out.println(a/(double)b);//0.5
System.out.println(a%b);//1  取余,模运算
  • 与long 运算的转为long,其余的整数类型运算会转(cast)为int类型,若是有double则转为double类型
long a=121232131212L;
        int b  = 123;
        short c= 10;
        byte d =  8;
        System.out.println(a+b+c+d);//long
        System.out.println(b+c+d);//int
        System.out.println(c+d);//int
  • 关系运算返回Boolean值
int a = 10;
int b = 20;
System.out.println(a>b);//false
System.out.println(a<b);//true
System.out.println(a!=b);//true
  • 自增自减 ++ -- 一元运算符
int a= 3;
int b= a++; //执行完这行代码后,先给b赋值,在自增
System.out.println(a);//4
int c=++a;  ////执行完这行代码前,先自增,在给c赋值
System.out.println(a);//5
System.out.println(b);//3
System.out.println(c);//5
  • 很多运算,我们会使用一些工具类来操作
//幂运算
double pow=Math.pow(3,2);
System.out.println(pow);//9
  • 逻辑运算符(&& 与 and)(|| 或 or)(! 非 取反)
boolean a = true;
boolean b = false;
System.out.println(a && b);//false  两个变量都为真,结果为真
System.out.println(a || b);//true   两个多为真才为真
System.out.println(!(a && b);//true 真变假,假变真     
//德模佛定理 !(condition1 && condition2) <===> !condition1 || !condition2)
//         !(condition1 || condition2) <===> !condition1 && !condition2)                   
  • 短路
int c = 5 ;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
  • 位运算
int a = 0b0011_1100;
int b = 0b0000_1101;
System.out.println(a&b); //0b0000_1100 全一为一
System.out.println(a|b); //0b0011_1101 有一为一
System.out.println(a^b); //0b0011_0001 不同为1,相同为0
System.out.println(~b);  //0b1111_0010 取反
int c = 0b10;
System.out.println(c<<3);//16 2*2*2*2  
System.out.println(c>>3);//2 16/2/2/2  
  • 扩展赋值运算符
int a = 10;
int b = 10;
a+=b; //-->a=a+b
System.out.println(a); //20
a-=b; //--->a=a-b
System.out.println(a);//10
  • 字符串连接
int a = 10;
int b = 10;
System.out.println(a+b); //20
System.out.println(""+a+b); //1010
System.out.println(a+b+""); //20
  • 三元运算符
// x ? y : z;   x==true 用y值, x==false 用z值
int score = 80 ;
score < 60 ? 1 : 0; //0
score > 60 ? 1 : 0; //1 
  • 优先级

比较运算符优先级高于逻辑运算符,除了赋值运算符,其余都是左结合运算

包机制

  • 本质就是文件夹

  • 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间

  • 包的语法格式为

package pkg1[.pkg2[.pkg3..]]
  • 一般用公司域名倒置作为包名 www.baidu.com ---> com.baidu.www
  • 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包,使用'import'语句即可
import package1[.package2...].(className | * );

JavaDoc

  • javaDoc命令是用来生成自己 API 文档的
  • 参数信息
    1. @author 作者名
    2. @version 版本号
    3. @since 指明需要最早使用的jdk版本
    4. @param 参数名
    5. @return 返回值情况
    6. @throws 异常抛出情况
  • 命令: javadoc -encoding utf-8 charset utf-8 路径\文件.java
posted @   李光宇  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示