编写Hello,world!
public class Hello{
public static void main(String[]args){
System.out.println("Hello,world!");
}
}
注释
public class Kang{
public static void main(String[]args){
System.out.println("hello,world!");
}
}
//单行注释
/*
* 多行注释
* 多行注释
*/
/**
* @apiNote文档注释
* @deprecated 文档注释
*/
常用标识符关键字!
常用标识符及关键字 | ||||
---|---|---|---|---|
abstract | break | byte | boolean | catch |
case | char | class | continue | default |
do | double | else | extends | false |
final | float | for | finally | if |
import | implements | int | interface | Instancof |
long | length | native | new | null |
package | private | proteed | public | return |
switch | synchronized | short | static | super |
try | true | this | throw | thorws |
threadsafe | transient | void | volatile | while |
⬆表格内容手打,可能会有错误
标识符不能用作变量名,变量名可用大小写,_ $开头,其余符号均不可(A123 a123 $123 _123)
例如:
public class Kang{
public static void main(String[]args){
String A123 = "普泰";
String a123 = "普泰";
String _123 = "普泰";
String $123 = "普泰";
System.out.println(a123 ); //该行未测试
System.out.println(A123);
System.out.println(_123);
System.out.println($123);
}
}
数据类型
八大基本数据类型
整数类型
public class Kang{
public static void main(String[]args){
int num1 = 10;
byte num2 = 20;
long num3 = 30L;//long类型在后面加L
short num4 = 40;
System.out.println(num3);
}
}
浮点数类型(小数)
public class Kang{
public static void main(String[]args){
float num5 = 4.5F; //float类型要在后面加F
double num6 = 19.96;
System.out.println(num6);
}
}
字符 & 字符串
public class Kang{
public static void main(String[]args){
char name = '康'; //字符用单引号
String firm = "普泰";//字符串用的双引号i
System.out.Println(firm);
}
}
布尔值
public class Kang{
public static void main(String[]args){
boolean flag = true; // 是
boolean flag1 = false; // 非
}
}
明日学习P24(初学,有不足之处望指正)
public class Demo02 {
public static void main(String[] args) {
//整数拓展
int a = 10;
int a2 = 010; //八进制0
int a3 = 0x10; //十六进制0x
int a4 = 0b10; //二进制0b
System.out.println(a);
System.out.println(a2);
System.out.println(a3);
System.out.println(a4);
System.out.println("==============================================================================");
//浮点数拓展
float b = 0.1f;
double b2 = 1.0/10;
System.out.println(b); //0.1
System.out.println(b2);//0.1
System.out.println(b==b2);//false
float b3 = 1234567890f;
float b4 = b3 + 1;
System.out.println(b3==b4);//true
//浮点数有有限性,离散性,舍入误差,接近但不等于
System.out.println("==============================================================================");
//字符拓展
char c = 'a';
char c1 = '康';
System.out.println(c);
System.out.println((int)c);//97
System.out.println(c1);
System.out.println((int)c1);//24247
//所有的字符本质还是数字,依据unicode编码表,\u0000~\uffff
char c2 ='\u0061';
System.out.println(c2);//a
//转义字符
System.out.println("Hello\tworld");// \t 制表
System.out.println("Hello\nworld");// \n 换行
System.out.println("Hello\bworld");// \当前位置前移一列
System.out.println("Hello\0world");// \0空字符
System.out.println("==============================================================================");
String d = new String("hello,world");
String d2 = new String("hello,world");
System.out.println(d==d2);//false
String d3 = "hello,world";
String d4 = "hello,world";
System.out.println(d3==d4);//true
System.out.println("==============================================================================");
//布尔值拓展
boolean flag = true;
if (flag==true){}
if(flag){}
//以上两种写法一样的结果,但是代码要精简易读 Less is more
}
}