HelloWorld
学习的第一步当然是写这个啦,仪式感一定要有
本篇为个人课堂笔记,从HelloWorld写起。
我学习的Java零基础免费课堂https://www.bilibili.com/video/BV12J41137hu?p=33&share_source=copy_web
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,World!");
}
}
01-注释
- 注释分为三种:单行注释、多行注释、文档注释
- 这里在代码中展示一下,文档注释暂且不做过多了解
public class HelloWorld {
public static void main(String[] args) {
//单行注释
System.out.println("Hello,World!");
/*
多行注释
*/
//JavaDoc 文档注释 /** */
/**
*
*/
}
}
02-标识符和关键字
一.标识符
Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。
二.关键字
abstract | assert | boolean | break | byte | case | catch | char | calss | const |
---|---|---|---|---|---|---|---|---|---|
continue | default | do | double | else | enum | extends | fina | finally | float |
for | goto | if | implements | import | instance of | int | interface | long | native |
new | package | private | protected | public | return | strictfp | short | static | super |
switch | synchronized | this | throw | throws | transient | try | void | volatile | while |
标识符的命名规范:
-
见名知意
-
遵循驼峰命名方式。(就是一高一低,一高一低…)。驼峰有利于单词与单词会之间很好地进行分隔。
-
类名、接口名有特殊的要求,类名、接口名首字母大写。后面每个单词首字母大写。
-
变量名、方法名首字母小写,后面每个单词首字母大写。
-
所有常量名全部大写,并且单词和单词之间采用下划线衔接。
public class Demon01 { public static void main(String[] args) { String Ahello = "1"; String hello = "1"; String $hello = "1"; String _hello = "1"; String Man = "ah"; String man = "ah"; String 王者荣耀 = "倔强青铜"; } }
03-数据类型
Java八大数据类型:
(1)整数类型:byte、short、int、long
(2)小数类型:float、double
(3)字符类型:char
(4)布尔类型:boolean
1、 整数数据类型
- byte:1个字节,8位,256种状态,取值范围为【-128,127】
- short:2个字节,16位,65536种状态,取值范围为【-32768,32767】
- int:4个字节,32位,整数类型默认是int类型,取值范围约21亿
- long:8个字节,64位,long类型表示long类型常量,要加L或者l,建议加L
2、 小数数据类型
- float:4个字节,32位,单精度,能精确到6~7位,声明一个小数类型,要加F或者f,建议加F
- double:8个字节,64位,双精度,能精确到15~16位,小数类型默认是double类型
3、 字符数据类型
- char:2个字节,16位,字符表示Unicode(万国码)编码表中的每一个符号,每个符号使用单引号引起来,其中前128个符号和ASCII表相同
4、 布尔数据类型
- boolean:占1位,有true和false 2个值,一个表示真,一个表示假,一般用于表示逻辑运算
04-数据类型拓展
public class Demon02 {
public static void main(String[] args) {
//八大基本数据类型
//整数
int num1 = 10;//最常用
byte num2 = 20;
short num3 =30;
long num4 = 40L;//long类型要在数字后面加l
//小数:浮点数
float num5 =50.1F;
double num6 = 3.1415926;
//字符
char name = '国';
//字符串,String不是关键字,类
//String namea = "名字";
//布尔值:是非
boolean flag = true;
//boolean flag = false;
}
public class Demon03 {
public static void main(String[] args) {
//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i = 10;
int i2 = 010;//八进制
int i3 = 0x10;//十六进制
System.out.println(i);
System.out.println(i2);
System.out.println(i3);
System.out.println("=========================================");
//=====================================================================================
//浮点数拓展? 银行业务怎么表示? 钱
//BigDecimal 数学工具类
//=====================================================================================
//float 有限 离散 舍入误差 大约 接近但不等于
//double
float f = 0.1f;
double d = 1.0/10;
System.out.println(f==d);
System.out.println(f);
System.out.println(d);
float d1 = 123445556676543f;
float d2 = d1+1;
System.out.println(d1==d2);//true
//=====================================================================================
//字符拓展?
//=====================================================================================
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 65 = A) 占了2字节 最多可以表示0 - 65536字符 早的Excel 有2^16 = 65536
//区间U0000 - UFFFF
char c3 = '\u0061';
System.out.println(c3);//a
//转义字符
//\t 制表符
//\n 换行
}
}
public class Demon04 {
public static void main(String[] args) {
String sa = new String("hello world");
String sb = new String("hello world");
System.out.println(sa==sb);//false
String sc = "hello world";
String sd = "hello world";
System.out.println(sc==sd);//true
//布尔值扩展
boolean flag = true;
if (flag==true){}//新手
if (flag){}//老手
}
}
05-类型转换
java是强类型语言
public class Demon05 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;//内容溢出,0-127
double b1 = i;
//强制转换 (类型)变量名 高--低
//自动转换 低--高
//低-----------------------------高
//byte,short,char -> int -> long -> float -> double 小数的优先级大于整数
System.out.println(i);
System.out.println(b);
System.out.println(b1);
/*
注意点:
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 c = 'a';
int d = c + 1;
System.out.println(d);
System.out.println((char)d);
}
}
public class Demon06 {
public static void main(String[] args) {
//操作比较大的数时,注意溢出问题
//JDK7 新特性,数字下面可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years;//-1474836480,计算的时候溢出了
long total2 = money*years;//默认是int,转换之前已经存在问题了?
long total3 =money*((long)years);//先把一个数转换为long
System.out.println(total3);
//注意使用L最好用大写
}
}
06-变量-常量-作用域
public class Demon07 {
public static void main(String[] args) {
//int a=1,b=2,c=3;最好写三行,程序可读性
int a=1;
int b=2;
int c=3;
String name = "ming";
char x = 'x';
double pi = 3.14;
}
}
public class Demon08 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0
//布尔值:默认是false
//除了基本类型,其余默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args){
//局部变量:必须声明和初始化值
//int i; //这样不行
int i = 10;
System.out.println(i);
//变量类型 变量名字 = new base.Demon08
Demon08 demon08 = new Demon08();
System.out.println(demon08.age);
System.out.println(demon08.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
public class Demon09 {
//修饰符不存在先后顺序
//final static 也可以
static final double PI =3.14;
public static void main(String[] args) {
System.out.println(PI);
}
}
07-基本运算符
Java 支持以下运算符
- 算术运算符:+ , - , * , / ,% , ++ , –
- 赋值运算符: =
- 关系运算符: > , < , >= ,<= , == , != , instanceof
- 逻辑运算符: && , || , !
- 位运算符: & , | , ^ , ~ , >> , << , >>>
- 条件运算符 ? :
- 扩展赋值运算符: += , -= ,*= , /=
算术运算符、赋值运算符
package operator;
public class Demon01 {
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(a/b);
如果输出a/b,即10/20=0.5,但是输出是0,因为int向下取整,
前面加double输出0.5
package operator;
public class Demon02 {
public static void main(String[] args) {
long a = 1234456789456L;
int b = 123;
short c = 10;
byte d = 8;
System.out.println(a+b+c+d);//Long,有一个long结果为long
System.out.println(b+c+d);//Int
System.out.println(c+d);//Int
}
}
关系运算符
package operator;
public class Demon03 {
//关系运算符返回的结果: 正确,错误 布尔值
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 21;
System.out.println(c%a);// 取余 c/a=1....1
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
package operator;
public class Demon04 {
public static void main(String[] args) {
//++ -- 自增 自减 一元运算
int a = 3;
int b = a++;//先给b赋值,再自增 //此时b为3 a为3
//a = a + 1
System.out.println(a); //此时a为4
//a++ a = a + 1;
int c = ++a;//先自增,再赋值 //此时a为5,c为5
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算
double pow = Math.pow(2, 3);
System.out.println(pow);
}
}
逻辑运算符
package operator;
//逻辑运算符
public class Demon05 {
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已经是false了,后面就没有必要运算了。所以c不变
System.out.println(d);
System.out.println(c);
}
}
位运算符
package operator;
public class Demon06 {
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
问 2*8 =16 怎么求运算快
<< *2 左移
>> /2 右移
*/
System.out.println(2<<3);
}
}
字符串连接符 +
package operator;
public class Demon07 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a+=b; // a = a+b
a-=b; // a = a-b
System.out.println(a);
//字符串连接符 +
System.out.println(a+b);
System.out.println(""+a+b);//输出1020,因为前面String
System.out.println(a+b+"");//输出30,因为前面先计算再变类型
}
}
三元运算符
package operator;
//三元运算符
public class Demon08 {
public static void main(String[] args) {
// x ? y : z
//如果x==true, 则结果为y,否则结果为z
int score = 80;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}