java基础
标识符
package base;
public class Demo01 {
//所有标识符都应该以字母(A-Z或者a-z),美元符号($)、下划线(_)开始
String Aheelo ="dd";
String $heelo ="dd";
String _heelo ="dd";
//首字符之后可以是字母(A-Z或者a-z),美元符($)、下划线(_)或数字的任何字符组成
String _1121312 = "ddd";
//不能使用关键字作为变量名或方法名
//标识符是大小写敏感的
String Man ="钟晚";
String man ="钟晚";
}
数据类型
package base;
public class Demo02 {
public static void main(String[] args) {
//所有变量都必须先定义后使用
//8大基本数据类型
//整型
int num1=10;
byte num2=20;
short num3=30;
long num4 =30L;
//小数: 浮点数
float num5=50.2F;
double num6 =3.14435534543543534534;
//字符
char name ='A';
//字符串
//布尔值
boolean num7 =true;
// boolean num8 =false;
/*
1bit 表示1位
1Byte表示一个字节 1B=8b
*/
}
}
数据类型拓展
package base;
public class Demo04 {
public static void main(String[] args) {
//整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i1 = 10;
int i2 = 010;
int i3 = 0x10; //十六进制0x 0~9 A~F
//-------------------------------
//符点数拓展
// float 有限 离散 舍入误差 大约 接近但不等于
//最好完全使用浮点数进行比较
//BigDecimal
float f = 0.1f; //0.1
double d = 1.0 / 10; //0.1
System.out.println(f == d);
float d1 = 2313232132132132131f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
//==================================
//字符拓展
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);
System.out.println(c1);
System.out.println((int) c2);
//所有的字符本质还是数字
//编码 Unicode 2字节 0-65536 Excel 2的16次方 =65536
//U00000 UFFFF
char c3 = '\u0061';
System.out.println(c3);
//转义字符
// \t
System.out.println("hello \nworld");
System.out.println("==================================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa == sb);
String sc = "hello world";
String sd = "hello world";
System.out.println(sc = sd);
//从对象内存分析
//布尔值扩展
boolean flag = true;
if (true) {
System.out.println("hhhhh");
}
//Less is More!
}
}
数据类型转化
package base;
public class Demo05 {
// byte, short, char -> int -> long -> float -> double
public static void main(String[] args) {
//强制类型转换
int i= 128;
byte b= (byte) i;//内存溢出
//自动转换
double c =i;
System.out.println(c);
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能对布尔值进行转化
2.不能把对象类型转换为不相干的类型
*/
System.out.println("========================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); // -45
System.out.println("========================");
}
}
溢出
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jIcK6KOE-1652761224806)(/Users/tk/Library/Application Support/typora-user-images/image-20220517120225817.png)]
package base;
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//jdk7 的新特性
int money = 10_0000_0000;
int years =20 ;
int total =money*years;
System.out.println(total); //-1474836480 ,计算的时候溢出了
long total2 =money*years;
System.out.println(total2); //默认是int
long total3 =money*(long)years;
System.out.println(total3);
}
}
变量
概念
package base;
public class Demo07 {
//变量:就是可以变化的量
/**
* 注意事项:
* 1.每个变量都有类型,类型可以是基本类型,也可以是引用类型
* 2.变量名必须是合法的表示符
* 3.
*
* @param args
*/
public static void main(String[] args) {
// int a,b,c;
int a = 1;
int b = 2;
int c = 3;
String name = "zhongwan";
char x = 'x';
double pi = 3.14;
}
}
常量、类变量、局部变量
package base;
public class Demo08 {
// 属性
//实例变量:如果不自行初始化,这个类型的默认值为0 0.0
//布尔值:false
//除了基本类型,其余的默认值都是null;
String name;
int age;
static double salary = 2500;
//常量
//修饰符,不存在先后顺序
static final double DCEX_NUM = 3.14;
//mian方法
public static void main(String[] args) {
//局部变量;必须声明和初始化值
int i = 10;
System.out.println(i);
Demo08 demo08 = new Demo08();
System.out.println("==================");
System.out.println(demo08.age);
//类变量
System.out.println(salary);
//常量
System.out.println(DCEX_NUM);
}
//其他方法
public void add() {
}
}
基本用算符
二元用算符
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a=10;
int b=20;
int c=25;
int d=25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 2142132131L;
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
}
}
关系用算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系用算符返回的结果: 正确,错误, 布尔值
/*
>
<
!=
==
*/
//++ -- 一元运算符 自增,自减
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,再值增
//a=a+1;
System.out.println(a);
System.out.println("======================");
int c = ++a; //执行完这行代码前,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
逻辑用算符
package operator;
//逻辑运算符
public class demo04 {
public static void main(String[] args) {
// 与(and) 或(or) 非(取反)
boolean a =true;
boolean b =false;
System.out.println("a && b"+(a&&b)); //两个变量都为真,结果才为true
System.out.println("a || b"+(a||b)); //两个变量有一个为真,则结果为真
//短路运算
int c=5;
boolean d =(c<4)&&(c++<4); // c<4 为false ; 后者不执行
System.out.println(d);
System.out.println(c);
}
}
位运算符
package operator;
public class Demo05 {
public static void main(String[] args) {
/*
A =0011 1100
B =0000 1101
A&B =0000 1100
A|B =0011 1101
A^B =0011 0001
~B =1111 0010
a*8 =16 2*2*2*2
<<左移 *2
>>右移 /2
a<<3
*/
System.out.println(2<<3);
}
}
package operator;
public class Demo06 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;
a-=b;
//字符串连接符 + ,String
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
三元用算符
package operator;
//三元运算符
public class Demo07 {
public static void main(String[] args) {
// x ? y :z
//如果x= true,则结果为y,否则结果为z
int score =50;
String type =score<60?"及格":"不及格";
System.out.println(type);
}
}
jdk 帮助文档
https://www.oracle.com/cn/java/technologies/java-se-api-doc.html
通过命令行
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
本地文件:index.html
java基础
标识符
package base;
public class Demo01 {
//所有标识符都应该以字母(A-Z或者a-z),美元符号($)、下划线(_)开始
String Aheelo ="dd";
String $heelo ="dd";
String _heelo ="dd";
//首字符之后可以是字母(A-Z或者a-z),美元符($)、下划线(_)或数字的任何字符组成
String _1121312 = "ddd";
//不能使用关键字作为变量名或方法名
//标识符是大小写敏感的
String Man ="钟晚";
String man ="钟晚";
}
数据类型
package base;
public class Demo02 {
public static void main(String[] args) {
//所有变量都必须先定义后使用
//8大基本数据类型
//整型
int num1=10;
byte num2=20;
short num3=30;
long num4 =30L;
//小数: 浮点数
float num5=50.2F;
double num6 =3.14435534543543534534;
//字符
char name ='A';
//字符串
//布尔值
boolean num7 =true;
// boolean num8 =false;
/*
1bit 表示1位
1Byte表示一个字节 1B=8b
*/
}
}
数据类型拓展
package base;
public class Demo04 {
public static void main(String[] args) {
//整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i1 = 10;
int i2 = 010;
int i3 = 0x10; //十六进制0x 0~9 A~F
//-------------------------------
//符点数拓展
// float 有限 离散 舍入误差 大约 接近但不等于
//最好完全使用浮点数进行比较
//BigDecimal
float f = 0.1f; //0.1
double d = 1.0 / 10; //0.1
System.out.println(f == d);
float d1 = 2313232132132132131f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
//==================================
//字符拓展
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println((int) c1);
System.out.println(c1);
System.out.println((int) c2);
//所有的字符本质还是数字
//编码 Unicode 2字节 0-65536 Excel 2的16次方 =65536
//U00000 UFFFF
char c3 = '\u0061';
System.out.println(c3);
//转义字符
// \t
System.out.println("hello \nworld");
System.out.println("==================================");
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa == sb);
String sc = "hello world";
String sd = "hello world";
System.out.println(sc = sd);
//从对象内存分析
//布尔值扩展
boolean flag = true;
if (true) {
System.out.println("hhhhh");
}
//Less is More!
}
}
数据类型转化
package base;
public class Demo05 {
// byte, short, char -> int -> long -> float -> double
public static void main(String[] args) {
//强制类型转换
int i= 128;
byte b= (byte) i;//内存溢出
//自动转换
double c =i;
System.out.println(c);
System.out.println(i);
System.out.println(b);
/*
注意点:
1.不能对布尔值进行转化
2.不能把对象类型转换为不相干的类型
*/
System.out.println("========================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); // -45
System.out.println("========================");
}
}
溢出
package base;
public class Demo06 {
public static void main(String[] args) {
//操作比较大的数的时候,注意溢出问题
//jdk7 的新特性
int money = 10_0000_0000;
int years =20 ;
int total =money*years;
System.out.println(total); //-1474836480 ,计算的时候溢出了
long total2 =money*years;
System.out.println(total2); //默认是int
long total3 =money*(long)years;
System.out.println(total3);
}
}
变量
概念
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-90GtCdLm-1652761293461)(/Users/tk/Library/Application Support/typora-user-images/image-20220517120128671.png)]
package base;
public class Demo07 {
//变量:就是可以变化的量
/**
* 注意事项:
* 1.每个变量都有类型,类型可以是基本类型,也可以是引用类型
* 2.变量名必须是合法的表示符
* 3.
*
* @param args
*/
public static void main(String[] args) {
// int a,b,c;
int a = 1;
int b = 2;
int c = 3;
String name = "zhongwan";
char x = 'x';
double pi = 3.14;
}
}
常量、类变量、局部变量
package base;
public class Demo08 {
// 属性
//实例变量:如果不自行初始化,这个类型的默认值为0 0.0
//布尔值:false
//除了基本类型,其余的默认值都是null;
String name;
int age;
static double salary = 2500;
//常量
//修饰符,不存在先后顺序
static final double DCEX_NUM = 3.14;
//mian方法
public static void main(String[] args) {
//局部变量;必须声明和初始化值
int i = 10;
System.out.println(i);
Demo08 demo08 = new Demo08();
System.out.println("==================");
System.out.println(demo08.age);
//类变量
System.out.println(salary);
//常量
System.out.println(DCEX_NUM);
}
//其他方法
public void add() {
}
}
基本用算符
二元用算符
package operator;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
int a=10;
int b=20;
int c=25;
int d=25;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/b);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 2142132131L;
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
}
}
关系用算符
package operator;
public class Demo03 {
public static void main(String[] args) {
//关系用算符返回的结果: 正确,错误, 布尔值
/*
>
<
!=
==
*/
//++ -- 一元运算符 自增,自减
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,再值增
//a=a+1;
System.out.println(a);
System.out.println("======================");
int c = ++a; //执行完这行代码前,先自增,再给c赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
逻辑用算符
package operator;
//逻辑运算符
public class demo04 {
public static void main(String[] args) {
// 与(and) 或(or) 非(取反)
boolean a =true;
boolean b =false;
System.out.println("a && b"+(a&&b)); //两个变量都为真,结果才为true
System.out.println("a || b"+(a||b)); //两个变量有一个为真,则结果为真
//短路运算
int c=5;
boolean d =(c<4)&&(c++<4); // c<4 为false ; 后者不执行
System.out.println(d);
System.out.println(c);
}
}
位运算符
package operator;
public class Demo05 {
public static void main(String[] args) {
/*
A =0011 1100
B =0000 1101
A&B =0000 1100
A|B =0011 1101
A^B =0011 0001
~B =1111 0010
a*8 =16 2*2*2*2
<<左移 *2
>>右移 /2
a<<3
*/
System.out.println(2<<3);
}
}
package operator;
public class Demo06 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;
a-=b;
//字符串连接符 + ,String
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
三元用算符
package operator;
//三元运算符
public class Demo07 {
public static void main(String[] args) {
// x ? y :z
//如果x= true,则结果为y,否则结果为z
int score =50;
String type =score<60?"及格":"不及格";
System.out.println(type);
}
}
jdk 帮助文档
https://www.oracle.com/cn/java/technologies/java-se-api-doc.html
通过命令行
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
本地文件:index.html