文章目录
JAVA基础语法
1.注释、标识符、关键字
2.数据类型
3.类型转换
3.变量、常量
注释、标识符与关键字
- 平时我们编写代码,在代码量比较少的时候,我们还可以看懂自己写的,但是当项目结构一旦复杂起来,我们就需要用到注释了。
- 注释并不会被执行,是给我们写代码的人看的
- 书写注释是一个非常好的习惯
- 平时写代码一定要注意规范。
java中的三种注释
- 单行注释 //
- 多行注释 /* */
- 文档注释 /** * */
*关键字
!!!关键字不能用来起名字!!!
标识符注意点
- 所有的标识符都应该以字母(A-Z或者a-z),美元符($),或者下划线(_)开始;"大小写十分敏感“
- 首字母之后可以是字母(A-Z或者a-z),美元符($),或者下划线(_)或者数字的任何字符组合;“特殊符号不能使用”
- 不能使用关键字作为变量名或者方法名。
- 合法标识符举例:age、 $salary、 _value、 __1_value
- 非法标识符举例:123abc、 -salary、 #abc
- 可以使用中文命名,但是一般不建议这样去使用,也不建议使用拼音,很Low
数据类型
package base;
public class Demo01 {
public static void main(String[] args) {
String name = "LYX";
byte num01 = 127;
short num02 = 20;
int num03 = 10;
long num04 = 10L;
float num05 = 0.1f;
double num06 =3.1415926;
char a ='a';
boolean i =true;
System.out.println(name);
System.out.println(num01);
System.out.println(num02);
System.out.println(num03);
System.out.println(num04);
System.out.println(num05);
System.out.println(num06);
System.out.println(a);
System.out.println(i);
}
}
类型转换
package base;
public class Demo02 {
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);
System.out.println((int)-45.89f);
System.out.println("==========================");
char d='a';
int e =d+1;
System.out.println(e);
System.out.println((char) e);
}
}
package base;
public class Demo03 {
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(total);
System.out.println(money);
System.out.println(total3);
}
}
变量、常量
package base;
public class Demo05 {
//类变量 static
static double salary = 2500;
//属性:变量
//实例变量:从属于对象;如果不自行初始化,这个类型的默认值 0 0.0 u0000
//布尔值:默认是false
//除了基本类型,其余的默认值都是null;
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量:必须声明和初始化值
int i=10;
System.out.println(i);
//变量类型 变量名字 = new base.Demo05();
Demo05 demo05 = new Demo05();
System.out.println(demo05.age);
System.out.println(demo05.name);
//类变量 static
System.out.println(salary);
}
//其他方法
public void add(){
}
}
运算符
基本运算符
package operator;
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);
}
}
package operator;
public class Demo02 {
public static void main(String[] args) {
long a = 123123123123123L;
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 = 10;
int b = 20;
int c = 22;
//取余 ,模运算
System.out.println(c%a);// c/a 22/10 = 2 ......2
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
}
}
自增自减运算符
package operator;
public class Demo04 {
public static void main(String[] args) {
//++ -- 自增 ,自建 一元运算符
int a =3;
int b =a++;//执行完这行代码后,先给b赋值,再自增
// a = a+1
System.out.println(b);
System.out.println(a);
//a = a+1
int c =++a; //执行完这行代码前,先自增,再给b赋值
System.out.println(a);
System.out.println(b);
System.out.println(c);
//幂运算 2^3 2*2*2 = 8 很多运算,我们会使用一些工具类来操作
double pow = Math.pow(3, 2);
System.out.println(pow);
}
}
逻辑运算符
package operator;
//逻辑运算符
public class Demo05 {
public static void main(String[] args) {
// 与(and) 或(or) 非(取反)
boolean a =true;
boolean b =false;
System.out.println("a&&b:"+(a&&b));//逻辑与运算:两个变量都为真,结果才为ture
System.out.println("a||b:"+(a||b));//逻辑或运算:两个变量有一个为真,则结果才为ture
System.out.println("!(a&&b):"+!(a&&b));//如果是真,则变为假,如果是假则变为真
//短路运算
int c = 5;
boolean d =(c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
}
}
位运算符
package operator;
public class Demo06 {
public static void main(String[] args) {
/*
A= 0011 1100
B= 0000 1101
------------------------------------
A&B = 0000 1100 两1为1 其余为0
A|B = 0011 1101 两0为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 1000 8
0001 0000 16
*/
System.out.println(2<<3);
}
}
字符串连接符
package operator;
public class Demo07 {
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);
//字符串連接符 + , String
System.out.println(""+a+b);
System.out.println(a+b+"");
}
}
三元运算符
package operator;
//三元運算符
public class Demo08 {
public static void main(String[] args) {
//x ? y : z
//如果x==ture ,則結果為y,否則結果爲z
int score = 80;
String type = score <60 ?"不及格" :"及格";
//if
System.out.println(type);
}
}
包机制
- 为了更好地组织类,JAVA提供了包机制,用于区别类名的命名空间。
- 一般公司用公司域名倒置作为包名
- 为了能够使用某一个包的成员,我们需要在Java程序中明确导入该包。使用“import”语句可完成此功能
JavaDoc
- javadoc命令是用来生成自己API文档的
- 参数信息
- @author 作者名
- @version 版本号
- @since 指明需要最早使用的jdk版本
- @param 参数名
- @return 返回值情况
- throws 异常抛出情况
//cmd
javadoc -encoding UTF-8 -charset UTF-8 Doc.java
//-encoding UTF-8 -charset UTF-8 使中文显示正常
package com.xiang.base;
/**
* @author LvYunXiang
* @version 1.0
* @since 1.8
*/
public class Doc {
String name ;
/**
* @author LvYunXiang
* @param name
* @return
* @throws Exception
*/
public String test (String name) throws Exception{
return name;
}
}
IDEA生成javadoc文档