Java基础
Java基础
注释
- 单行注释
- 多行注释
- 文档注释
标识符
- 所有标识符都应该以字母(AZ或az),美元符号($),下划线(_)开始
- 首字符后可以是字母(AZ或az),美元符号($),下划线(_)或数字任意字符
- 不能用关键字作为变量名或方法名
- 标识符大小写敏感
- 可以使用中文命名,但不建议
数据类型
- 强语言类型
- 弱语言类型
Java的数据类型分为两大类:

拓展
public class Demo03 {
public static void main(String[] args) {
//整数拓展 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 020; //八进制0
int i3 = 0x2A; //十六进制0x 0~9 A~F或a~f(10~15)
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("------------------------------------");
//=========================================================
//浮点数拓展 银行业务怎么表示?
//BigDecimal 数学工具类
//=========================================================
float f = 0.1f; //表示0.1
double d = 1.0/10; //表示0.1
System.out.println(f==d); //结果为false
System.out.println(f);
System.out.println(d);
float d1 = 123124124124f;
float d2 = d1 + 2;
System.out.println(d1 == d2); //结果为true
//float 有限 离散 舍入误差 大约 接近但不等于
//double
//最好完全避免浮点数进行比较
//最好完全避免浮点数进行比较
//字符拓展
System.out.println("------------------------------------");
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 2字节 0~65536 2的16次方
//U0000 UFFFF
char c3 = '\u0061';
System.out.println(c3); //a
//转义字符
// \t 制表符
// \n 换行
//......
System.out.println("------------------------------------");
System.out.println("Hello\tWorld!");
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); //flase
String sc = "hello world";
String sd = "hello world";
System.out.println(sc == sd); //true
//布尔值拓展
boolean flag = true;
if (flag==true){}
if (flag){}
//两段代码相等 默认表示等于true
}
}
类型转换
Java是强类型语言,所以在进行某些运算时,需要进行类型转换
低------------------------------------------>高
byte,short,char-->int-->long-->float-->double
因为小数的优先级大于整数所以float虽然只有32bit但比64bit的long要高
运算中,不同类型的数据先转化为同一类型,然后运算
public class Demo04 {
public static void main(String[] args) {
int i =128;
//强制转换 (类型)变量名 高-->低
//自动转换 低-->高
byte b = (byte)i; //内存溢出
double c = i;
System.out.println(i);
System.out.println(b);
System.out.println(c);
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.把高容量转换到低容量的时候,强制转换
4.转换的时候可能存在内存溢出,或者精度问题
*/
System.out.println("====================");
System.out.println((int)23.7); //23
System.out.println((int)-45.89f); //-45
System.out.println("====================");
char d = 'a';
int e = d + 1;
System.out.println(e);
System.out.println((char)e);
System.out.println("====================");
//操作比较大的数的时候,注意溢出问题
//JDK7新特性,数字之间可以用下划线分割,便于观察
int money = 10_0000_0000;
int years = 20;
int total = money * years; //-1474836480,计算溢出
long total2 = money * years; //,默认是int,转换之前已经存在问题
System.out.println(total);
System.out.println(total2);
System.out.println("====================");
long total3 = money*((long)years); //先把一个数字转换成long
System.out.println(total3);
}
}
变量
Java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
注意事项
- 每个变量都有类型,类型可以是基本类型,也可以是引用类型
- 变量名必须是合法的标识符
- 变量声明是一条完整的语句,因此每一个声明都必须以分号结束
public class Demo05 {
public static void main(String[] args) {
//int a=1,b=2,c=3; 这种写法没错,但不建议这样写
int a=1;
int b=2;
int c=3;
String name = "kyh";
char x = 'X';
double pi = 3.14;
}
}
变量作用域
- 类变量
- 实例变量
- 局部变量
public class Variable{
static int allClicks = 0; //类变量
String str = "hello, world"; //实例变量
public void method(){
int i = 0; //局部变量
}
}
例:
public class Demo06 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量:从属于对象,如果不自行初始化,则打印这个类型的默认值
//布尔值默认是true
//除了基本类型,其余的默认值都是null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i = 10;
System.out.println(i); //10
//实例变量
//变量类型 变量名字 = new Demo06();
Demo06 demo06 = new Demo06();
System.out.println(demo06.age); //0
System.out.println(demo06.name); //null
//类变量 static
System.out.println(salary); //2500.0
}
//其他方法
public void add(){
System.out.println();
}
}
常量
初始化后不再改变值,可以理解为一种特殊变量,常量名一般用大写字符
final 常量名 = 值;
final double PI = 3.14;
public class Demo07 {
//常量
//修饰符,不存在先后顺序
static final double PI = 3.14;
//final static double PI = 3.14;这种写法也行
public static void main(String[] args) {
System.out.println(PI);
}
}
变量的命名规范
- 所有变量、方法、类名:见名知意
- 类成员变量名:首字母小写和驼峰原则:monthSalary, lastName等,除了第一个单词以外,后面的单词首字母大写
- 局部变量:首字母小写和驼峰原则
- 常量:大写字母和下划线
- 类名:首字母大写和驼峰原则:Man, GoodMan
- 方法名:首字母小写和驼峰原则:run(), runRun()
运算符
Java语言支持的运算符:优先级()
- *算术运算符:+, -, , /, %, ++, --
- 赋值运算符:=
- 关系运算符:>, <, >=, <=, ==, !=, instanceof
- 逻辑运算符:&&, ||, !
- 位运算符:&, |, ^, ~, >>, <<, >>>(了解)
- 条件运算符:?, :
- 扩展赋值运算符:+=, -=, *=, /=
package operatior;
public class Demo01 {
public static void main(String[] args) {
//二元运算符
//Ctrl + D: 复制当前行到下一行
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);
//下面两个结果不同,因为都为int类型所以除出小数会舍去,需要进行强制转换
System.out.println(a/b); //0
System.out.println((double)a/b); //0.5
}
}
package operatior;
public class Demo02 {
public static void main(String[] args) {
long a = 15_6135_1351L;
int b = 123;
short c = 10;
byte d = 8;
double e = 1000;
float f = 900f;
//整数运算中如果有long类型,结果即为long类型;如果没有long即为int类型
//如果运算中有浮点数,有double类则结果为double,只有float则结果为float
System.out.println(a+b+c+d); //Long
System.out.println(b+c+d); //Int
System.out.println(c+d); //Int
System.out.println(d+f); //float
System.out.println(a+e); //double
System.out.println(e+f); //double
}
}
package operatior;
public class Demo03 {
public static void main(String[] args) {
//关系运算符返回的结果:正确,错误 (布尔值)
int a = 10;
int b = 20;
int c = 21;
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(c%a); // c/a的余数 模运算
}
}
package operatior;
public class Demo04 {
public static void main(String[] args) {
// ++ 自增, -- 自减 一元运算符
int a = 3;
int b = a++; //执行完这行代码后,先给b赋值,再自增
//a = a + 1
System.out.println(a); //4
//a = a + 1
int c = ++a; //执行完这行代码前,先自增,再给c赋值
System.out.println(a); //5
System.out.println(b); //3
System.out.println(c); //5
//幂运算 2^3 2*2*2=8 很多运算,我们会使用一些工具类操作
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
package operatior;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
//与 或 非
boolean a = true;
boolean b = false;
System.out.println("a && b:" + (b&&a)); //逻辑与运算:两个变量都为真,结果才为真
System.out.println("a || b:" + (a||b)); //逻辑与运算:两个变量都为假,结果才为假
System.out.println("!(a && b):" + !(a&&b)); //如果是真,则为假;如果是假,则为真
//短路运算 即在判断到第一个已经为假的时候,便不再继续判定第二个,直接给出结果为假
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c); //此时c=5,表示c++并未被执行
}
}
package operatior;
public class Demo06 {
public static void main(String[] args) {
/*
A = 0011 1100
B = 0000 1101
A&B = 0000 1100 (都为1才是1)
A|B = 0011 1101 (都为0才是0)
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(2<<3);
}
}
package operatior;
public class Demo07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a += b; //a = a + b
System.out.println(a);
//字符串连接符
System.out.println(""+a+b); //1020 “ ”让a和b转换成了String类型,再进行的合并
System.out.println(a+b+""); //30 按顺序a+b先运行,才转换为的String类型
}
}
package operatior;
//三元运算符
public class Demo08 {
public static void main(String[] args) {
//x ? y : z
//如果x==true,则结果为y,否则结果为z
int score = 80;
String type = score < 60 ?"不及格":"及格";
System.out.println(type);
}
}
JavaDoc
package com.k.base;
/**
* @author k
* @version 1.0
* @since 1.8
*/
public class Doc {
String name;
/**
*
* @param name
* @return
* @throws Exception
*/
public String test(String name) throws Exception{
return name;
}
//在命令行中生成java文档:javadoc 参数 Java文件
//javadoc encoding UTF-8 -charset UTF-8
}

浙公网安备 33010602011771号