Java基础
注释
- // 单行注释
- /*回车 多行注释
- /**回车 文档注释
标识符和关键字


数据类型

public class Demo01 {
public static void main(String[] args) {
int num1 = 10;
byte num2 = 10;
short num3 = 10;
long num4 = 10L;
float num5 = 3.1415926F;
double num6 = 3.1415926;
char name = 'A';
boolean flag = true;
}
}
数据类型扩展
public class Demo02 {
public static void main(String[] args) {
int i1 = 10;
int i2 = 010;
int i3 = 0x10;
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
float f = 0.1f;
double d = 1.0 / 10;
System.out.println(f == d);
float d1 = 23123123123123f;
float d2 = d1 + 1;
System.out.println(d1 == d2);
char c1 = 'a';
char c2 = '中';
System.out.println(c1);
System.out.println(c2);
System.out.println((int) c1);
System.out.println((int) c2);
char c3 = '\u0061';
System.out.println(c3);
System.out.println("hello\tworld\n");
boolean flag = true;
if (flag == true) {
}
if (flag) {
}
}
}
类型转换

public class Demo03 {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i;
double d = i;
System.out.println(i);
System.out.println(b);
System.out.println(d);
System.out.println((int)23.21);
System.out.println((int)-42.213);
char ch = 'a';
int i2 = ch+1;
System.out.println(i2);
System.out.println((char)i2);
int money = 10_0000_0000;
int year = 20;
int total = money*year;
long total2 = money*year;
long total3 = money*(long)year;
System.out.println(total);
System.out.println(total2);
System.out.println(total3);
}
}
变量 常量

public class Demo04 {
static double salary = 2500;
String name;
int age;
public static void main(String[] args) {
String name = "Mike";
char x = 'X';
double pi = 3.14;
int i = 100;
Demo04 demo04 = new Demo04();
System.out.println(demo04.name);
System.out.println(demo04.age);
System.out.println(salary);
}
}

public class Demo05 {
static final double PI = 3.14;
final static double PI_ = 3.1415926;
public static void main(String[] args) {
System.out.println(PI);
System.out.println(PI_);
}
}

运算符

package Base;
public class Demo06 {
public static void main(String[] args) {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println(a/(double)b);
long e = 12345643156431L;
int f = 123;
short g = 10;
byte h = 8;
System.out.println(getType(e+f+g+h));
System.out.println(getType(f+g+h));
System.out.println(getType(g+h));
System.out.println(a>b);
System.out.println(a<b);
System.out.println(a==b);
System.out.println(a!=b);
System.out.println(f%d);
}
public static String getType(Object o){
return o.getClass().toString();
}
}
自增自减运算符、初识Math类
package Base;
public class Demo07 {
public static void main(String[] args) {
int a = 1;
int b = a++;
int c = ++a;
System.out.println(a);
System.out.println(b);
System.out.println(c);
double pow = Math.pow(2,3);
System.out.println(pow);
}
}
逻辑运算符、位运算符
package Base;
public class Demo08 {
public static void main(String[] args) {
boolean a = true;
boolean b = false;
System.out.println("a && b:"+(a&&b));
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);
System.out.println(2<<3);
}
}
三元符及小结
package Base;
public class Demo09 {
public static void main(String[] args) {
int a = 1;
int b = 2;
a+=b;
System.out.println(a);
a-=b;
System.out.println(a);
System.out.println(""+a+b);
System.out.println(a+b+"");
int score = 80;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}
包机制


JavaDoc生成文档

package Base;
public class Demo10 {
String name;
public String test(String name){
return name;
}
}
(如何使用Intellij Idea生成JavaDoc文档)
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术