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';
//字符串,String不是关键字
//String name = "Mike";
//布尔值
boolean flag = true;
}
}
数据类型扩展
public class Demo02 {
public static void main(String[] args) {
//整数扩展: 进制 二进制0b 十进制 八进制0 十六进制0x
int i1 = 10;
int i2 = 010;//八进制
int i3 = 0x10;//十六进制
System.out.println(i1);
System.out.println(i2);
System.out.println(i3);
//浮点数扩展
//避免使用浮点数比较
//用BigDecimal 数学工具类
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);
//所有字符本质还是数据
//编码 Unicode 2字节 0-65536
char c3 = '\u0061';
System.out.println(c3);
//转义字符
// \t 制表符
// \n 换行
// ...
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);
/*
* 1、不能对布尔值进行转换
* 2、不能把对象类型转换为不相干的类型
* 3、转换的时候可能存在内存溢出或精度问题
* */
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);
//操作较大数的时候注意溢出问题
//JDK7新特性,数字之间可以用下划线分割
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
static double salary = 2500;
//实列变量:从属于对象,如果不初始化则为默认值
//基本类型为0
//布尔值为false
//其余为null
String name;
int age;
public static void main(String[] args) {
//int a=1,b=2,c=3;//不推荐
//变量声明
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));//long
System.out.println(getType(f+g+h));//int
System.out.println(getType(g+h));//int
//关系运算符
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(); //使用int类型的getClass()方法
}
}
自增自减运算符、初识Math类
package Base;
//自增自减运算符、初识Math类
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));//非
//短路运算(与运算时,若前面为false,则后面不会运行)
int c = 5;
boolean d = (c<4)&&(c++<4);
System.out.println(d);
System.out.println(c);
//位运算
/*
* A = 0011 1100
* B = 0000 1101
* A&B = 0000 1100
* A|B = 0011 1101
* A^B = 0011 0001
* ~A = 1100 0011
* A<<2 = 1111 0000 //乘
* A>>2 = 0000 1111 //除
* */
System.out.println(2<<3);
}
}
三元符及小结
package Base;
//三元符及小结
public class Demo09 {
public static void main(String[] args) {
int a = 1;
int b = 2;
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+"");
//三元运算符
// x ? y : z
//如果x为真,则y,否则
int score = 80;
String type = score < 60 ? "不及格":"及格";
System.out.println(type);
}
}
包机制
JavaDoc生成文档
package Base;
//JavaDoc生成文档
/**
* @author saber
* @version 1.0
* @since 1.8
*/
public class Demo10 {
String name;
/**
* @author saber
* @param name
* @return
* @throws Exception
*/
public String test(String name){
return name;
}
}
(如何使用Intellij Idea生成JavaDoc文档)
标签:Java,String,int,基础,System,println,public,out From: https://www.cnblogs.com/lyc2001/p/16649742.html