1 根底数据结构
在JAVA程序运行过程中始终不会扭转的量称为常量。
有的数据值是不固定的,总在变,咱们还须要记录这些值,咱们能够把这些值了解为变量。
在java中数据类型分为两类:根本数据类型和援用类型。
1.1 根本数据类型
根本数据类型总共有8种:
根本数据类型的取值范畴:
/* 整数型:byte 1字节
* short 2
* int 4
* long 8
*浮点型:float 4
* double 8
*字符: char 2
*布尔值:boolean 1
*/
//整数型测试 byte short int long
byte max = Byte.MAX_VALUE;
byte min = Byte.MIN_VALUE;
System.out.println(max);//127 2e7
System.out.println(min);//-128
short smax = Short.MAX_VALUE;
short smin = Short.MIN_VALUE;
System.out.println(smax);//32767 2e15
System.out.println(smin);//-32768
//int--integer
int imax = Integer.MAX_VALUE;
int imin = Integer.MIN_VALUE;
System.out.println(imax);//2147483647 2e31
System.out.println(imin);//-2147483648
long lmax = Long.MAX_VALUE;
long lmin = Long.MIN_VALUE;
System.out.println(lmax);//9223372036854775807 2e63
System.out.println(lmin);//-9223372036854775808
//浮点型测试 float double
float fmax = Float.MAX_VALUE;
float fmin = Float.MIN_NORMAL;
System.out.println(fmax);//3.4028235E38 8位有效数字
System.out.println(fmin);//1.17549435E-38
double dmax = Double.MAX_VALUE;
double dmin = Double.MIN_NORMAL;
System.out.println(dmax);//1.7976931348623157E308 16位有效数字
System.out.println(dmin);//2.2250738585072014E-308
//char测试
char c = 'a';//能够存一个字符''
char c1 = '1';
char c2 = '在';//能够存一个汉字
char c3 = 18;//能够存一个数字,会查看ASCII表(0-127有对应)进行打印,区间0-65535
System.out.println(c);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
1.2 数据的字面值
整数类型的字面值:int类型
//byte,short,char三种比int小的整数能够用范畴内的值间接赋值
byte b=120;//对
int a = 999999999;//错,右侧是int类型,然而超出范围
long a = 999999999l;//对~~~~
所有的整数类型的数据都是整型:例如 20 05 0x12 0X12 0b10 0B10
八进制以0结尾:例如 05
十六进制以0x或0X结尾:例如 0x12 0X12
二进制应用0b或0B结尾:例如 0b10 0B10
浮点数的字面值是:double类型
double a=3.14;//对
float a=3.14;//错,右面是double,float是四字节double是八字节存不下
布尔值的字面值是:true或false
1.3 根本类型的类型转化
1.3.1 隐式转化(小转大)
沿着箭头所指的方向能够间接转化
1.3.2 显示转化(大转小)
如果大类型的值转为小类型的值时须要强制类型转换。
int xx = 356;
byte y=(byte) xx;
如果大类型的值在小类型的取值范畴内能够间接转换。
留神:小数转成整数,小数间接舍弃
1.4 运算规定
运算时会先转化为最大类型进行运算
byte a=3;
byte b=4;
byte c=a+b;//错,运行时,byte会先主动转成int再运算,int+int还是int
int c=a+b;//对
运算后果输入也为最大类型
System.out.println(15/7);//2,后果是int类型
System.out.println(15d/7);//2.142857142857143,相当于double/int,后果是double类型~~~~
整数运算溢出
整数运算,如果运算后果超出数据类型取值范畴,则会溢出,从最小范畴再开始统计。
System.out.println(300000000*60*60*24*365);//659193856
System.out.println(300000000l*60*60*24*365);//9460800000000000
浮点数运算不准确
可应用BigDecimal工具类:用来解决准确的浮点数运算。
System.out.println(3-1.2);//1.8
System.out.println(3*1.1);//3.3000000000000003
浮点数的非凡值
Infinity 无穷大 3.14/0
Nan not a number 0/0.
2 运算符
自增自减的练习
int a = 1 ;
//符号在后,先应用后变动
System.out.println(a++);//1,a=2
int b = 1 ;
//符号在前,先变动后应用
System.out.println(++b);//2
int c = 1;
//符号在前,先变动后应用
System.out.println(--c);//0
int d = 1;
//符号在后,先应用后变动
System.out.println(d--);//1
//TODO
System.out.println(++a+b+a++);//3+2+3=8
System.out.println(--d-d-c--);//-1-(-1)-0=0
判断平平年的分割
public static void main(String[] args) {
System.out.println("请输出年份进行测试");
int year = new Scanner(System.in).nextInt();
/*
* 平年和平年的三种判断方bai法:
*①、一般年都能被4整除且不能被100整除的为平年。
*②、世纪年能被400整除的是平年。否则为平年。(如2000年是平年,1900年不是平年)
*③、对于数值很大的年份,这年如果能整除3200,并且能整除172800则是平年。否则为平年。
*/
String a = "平年";
if((year%4==0 && year%100!=0) || year%400==0 ) {
a="平年";
}
System.out.println(year+"是"+a);
}
3 分支和循环构造
3.1 分支构造
对于要先做判断再抉择的问题就要应用分支构造。
3.1.1 分支构造(if)
单分支:
if(判断条件){
满足条件的代码
}
多分支:
if(判断条件){
满足条件的代码
}else{
不满足条件的代码
}
嵌套分支:
if(判断条件1){
满足条件1的代码1
}else if(判断条件2){
满足条件2的代码2
}else if(判断条件3){
满足条件3的代码3
}else{
以上谁都不满足来这儿
}
3.1.2 分支构造(switch)
概述:当一个case成立,从这个case向后穿透所有case,包含default,直到程序完结或者遇到break程序才完结。
public static void main(String[] args) {
int a = 3;
//a能够是int根本类型或Integer包装类型,可反对5中类型,byte,short,int,char,JDK1.5后的string;
switch (a) {
case 1 :System.out.println(2);
case 2 :System.out.println(3);
case 3 :System.out.println(4);break;
case 4 :System.out.println(5);
default:System.out.println(6);
}
//1.当胜利匹配case,会执行前面代码,并会穿透后续所有case和default;
//2.当遇到break,终止switch分支;
//3.没有遇到工作满足条件的case,只会执行default;
}
3.2 循环构造
循环构造是指在程序中须要重复执行某个性能而设置的一种程序结构。
它由循环体中的条件,判断继续执行某个性能还是退出循环。
依据判断条件,循环构造又可细分为先判断后执行的循环构造和先执行后判断的循环构造。
3.2.1 循环构造(for)
循环次数已知的状况下可使可优先思考应用for循环
fori循环:
for(开始条件;循环条件;更改条件){
循环体代码…
}
foreach循环(加强for循环):
for(元素类型 元素名称:遍历数组(汇合)){
循环体代码
}
循环的程序:
3.2.1 循环构造(while和do…while)
当循环次数不确定时应用:
while:先判断,不合乎规定,不执行代码
do while:代码起码被执行一次,再去判断,合乎规定,再次执行代码
先判断后执行:
while(循环的判断条件){
循环体
}
先执行后判断:
do{
循环体
}while(循环的判断条件);
4 数组
数组Array是用于贮存多个雷同类型数据的汇合。
想要获取数组中的元素值,能够通过元素的下标来获取,下标是从0开始的。
4.1 数组的创立
数组一旦创立,长度不可变,容许创立长度为0的数组。
动态创立数组
//动态存储
char[] a =new char[] {'h','e','l','l','o'};
char[] a1={'h','e','l','l','o'};//简写模式
动态创建数组
//动静存储
char[] a2=new char[5];
a2[0]='h';
a2[1]='e';
a2[2]='l';
a2[3]='l';
a2[4]='o';
4.2 数组的工具类
Arrays.copy0f(复制的数组名,长度值)
Arrays.sort(数组名) 排序办法
Arrays.toString(数组名) 数组的展现
public static void main(String[] args) {
//创立一个10位无序数组;
int a[]=new int[10];
for(int i=0;i<a.length;i++) {
a[i]=new Random().nextInt(100);
}
//Arrays.copy0f(复制的数组名,长度值),长度数大于10扩容,小于10缩容
int b[]=Arrays.copyOf(a, 11);
System.out.println(Arrays.toString(b));
//Arrays.sort排序办法
Arrays.sort(a);
//Arrays.toString(a),数组的展现
System.out.println(Arrays.toString(a));
}
4.3 简略的算法
冒泡排序:
public static void method(int[] a) {
//进行冒泡排序
for(int i =0;i<a.length-1;i++) {//外循环,比拟大小,管制比拟轮数,n轮,比拟n-1次
for(int j =0;j<a.length-(1+i);j++) {//内循环,比拟大小
if(a[j]>a[j+1]) {//相邻比拟
//替换数值
int c =a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
}
发表回复