
1.// 我是单行注释
2.
4.搜索有趣的神级注释:
二、标识符
关键字:
1.强类型语言
要求变量使用严格符合规定,所有变量都必须先定义后才能使用
2.弱类型语言
public class Demo01 {
public static void main(String[] args) {
String a="Hello";
int number = 10;
System.out.println(a);
System.out.println(number);
//八大基本数据类型
//Integer
//整数
int num1 = 10;//最常用
byte num2 =20;
short num3 = 30;
long num4 = 30L;//Long类型要在数字后面加L
//小数、浮点数
float num5 =50.1F;//float类型要在数字后面加个F
double num6 = 3.14159265358979323846243;
//字符
char name = 'A';
//字符串,String不是关键字,类
String namea ="你好";
//布尔值:代表是非
boolean flag = ture;
//boolean flag = false;
}
}
import sun.plugin.liveconnect.OriginNotAllowedException;
public class Demo02 {
public static void main(String[] args) {
// 整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制0
int i3 = 0x10;//十六进制0x 0~9 A~F
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("===============================================================");
//=======================================================================
//浮点数拓展: 银行业务怎么表示?钱
//BigDecimal 数学工具类
//=======================================================================
//float 有限 离散 舍入误差 大约 接近但不等于
//double
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 = 3276576215657657f;
float d2 = d1 + 1;
System.out.println(d1 == d2);//true
System.out.println("===============================================================");
//=======================================================================
//字符拓展
//=======================================================================
//所有的字符本质还是数字
//编码 Unicode 表:97=a 65=A 占了2字节 写的长度0-65536 ;Excel 2的16次方 =65536
//u0000 UFFFF
char c1 = 'a';
char c2 = '中';
char c3 = 'u0061';
System.out.println(c1);
System.out.println((int) c1);//强制转换
System.out.println(c2);
System.out.println((int) c2);//强制转换
System.out.println(c3);//a
//转义字符
//t 制表符
//n 换行
//还有更多转义字符
System.out.println("HellotWord");
System.out.println("HellonWord");
System.out.println("===============================================================");
//对象 从内存分析
String sa = new String("hello word");
String sb = new String("hello word");
System.out.println(sa == sb);
String sc = ("hello word");
String sd = ("hello word");
System.out.println(sc == sd);
System.out.println("===============================================================");
//布尔值扩展
boolean flag = true;
if (flag == true) {//新手
}
if (flag) {//老手
}
//Less is More! 代码要精简易读
}
}
四、类型转换
运算中不同类型的数据先转化为同一类型,然后进行运算
public class Demo03 {
public static void main(String[] args) {
//byte,short,char -> int -> long -> float ->double
int i =128;
byte b =(byte) i;//内存溢出
//强制转换 高->低 (类型)变量名
System.out.println(i);
System.out.println(b);//由于byte最大127
//自动类型转换 低->高
int i1 =128;
double b1 = i1;
System.out.println(i1);
System.out.println(b1);
System.out.println("=====================================");
System.out.println((int)23.7);//23 内存溢出
System.out.println((int)-45.87f);//45 精度问题
System.out.println("=====================================");
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char)d);
}
}
import java.time.YearMonth;
public class Demo04 {
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);//-1474836480 默认是int,转换之前已经存在问题了
long total3 = money*((long)years);
System.out.println(total3);//20000000000 先把一个数转化为Long
}
}
五、变量
变量就是可以变化的量
java是一种强类型语言,每个变量都必须声明其类型
java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域
注意事项:
public class Demo05 {
public static void main(String[] args) {
// int a,b,c
//int a = 1,b =1,c =1; 程序可读性
String name = "nihao";
int a = 1;
double c = 2;
}
}
变量作用域
public class Demo06 {
//类变量 static
static double salary=2500;
//属性:变量
//实例变量:从属于对象;如果不自行初始化,就会变成这个类型的默认值, 0 0.0
// 布尔值默认是false
//除了基本类型,其余的默认值都是null
String name;
int age;
// main 方法
public static void main(String[] args) {
//局部变量:使用之前必须声明和初始化值
int i = 10;
System.out.println(i);
//变量类型:变量名字 = new Demo06()
Demo06 demo06 = new Demo06();
System.out.println(demo06.age);//
System.out.println(demo06.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
六、常量
初始化后不能再改变的值
所谓常量可以理解成特殊的变量,它的值设定后,在程序运行过程中不允许被改变
常量名一般使用大写字符
public class Demo07 {
static final double PI =3.14;
//final static double PI =3.14; final 是修饰符 不存在先后顺序
public static void main(String[] args) {
System.out.println(PI);
}
}
变量的命名规范
七、运算符
package operator;
import sun.nio.cs.ext.DoubleByteEncoder;
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);
System.out.println(a/(double)b);
System.out.println("==========================================");
long e = 867728362876872678L;
int f = 123;
short g = 10;
byte h = 8;
System.out.println(e+f+g+h);//Long 有一个是Long 结果就是Long
System.out.println(f+g+h);//Int 结果没有Long 就都是Int
System.out.println(g+h);//Int
System.out.println("==========================================");
//关系运算符返回的结果 :正确,错误 布尔值
int a1 = 10;
int b1 = 20;
int c1 = 21;
System.out.println(a1>b1);//false
System.out.println(a1
package operator;
import sun.net.www.protocol.http.HttpURLConnection;
import java.lang.reflect.GenericSignatureFormatError;
//逻辑运算符
public class Demo02 {
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));//逻辑或运算:两个变量有一个为真,结果才为true
System.out.println("!(a&&b):"+!(a&&b));//如果是真,则变成假,如果是假则变为真
//短路运算
int c = 5;
boolean d = (c<4)&&(c++<4);//因为c<4 为假,但是&&是要求都为真,所以不继续执行了,短路运算
System.out.println(c);//如果没短路 c就是6
System.out.println(d);
}
}
package operator;
public class Demo03 {
public static void main(String[] args) {
System.out.println(2<<3);//16
}
}
package operator;
public class Demo04 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b;//a=a+b
System.out.println(a);
a-=b;//a=a-b
System.out.println(a);
//字符串连接符 +会把输出转换成String类型
System.out.println(a+b);
System.out.println(""+a+b);
System.out.println(a+b+"");//放在字符串前面不会改变类型
}
}
package operator;
//三元运算符
public class Demo05 {
public static void main(String[] args) {
// x ? y : z
// 如果X=true 则结果为y 否则结果为z
int score =80;
String type=score<60?"不及格":"及格";
System.out.println(type);
}
}
八、包机制
包的本质就是文件夹
包名一般利用公司域名倒置作为包名:
例如:www.baidu.com就会变为com.baidu.www
import com.kuang.base.* ; 就会导入base这个包下的所有的类
网上查阿里巴巴开发手册认真看一遍
九、JavaDOC
jdk帮助文档
package com.sun.operator;
public class Demo06 {
String name;
public String test(String name) throws Exception{
return name;
}
//命令行生成的
//作业:学会查找使用IDEA生产JavaDoc文档 面向百度编程
//基础很重要
}