单行注释:
// 单行注释用双斜杠
比如,
public class Comments {
public static void main(String[] args) {
System.out.println("How to make comments in JAVA?"); // 单行注释用双斜杠
}
}
// 单行注释用双斜杠
多行注释:
使用/* 和 */开头和结尾
/*
多行注释
多行注释
多行注释
多行注释
*/
比如,
/*
多行注释
多行注释
多行注释
多行注释
*/
public class Comments {
public static void main(String[] args) {
System.out.println("How to make comments in JAVA?");
}
}
/*
多行注释
多行注释
多行注释
多行注释
*/
文档注释
使用/** 开头 */结尾,中间每一行以 * 开头,可以用@+参数名称来进行注释。同时可以和JavaDoc进行联动。
比如,
public class Comments {
public static void main(String[] args) {
System.out.println("How to make comments in JAVA?");
}
}
/**
* @Description Practice
* @Author JY
*/
关键字
JAVA中包含规定的关键字,比如class,void,static这些
不能使用关键字来取名
标识符
JAVA所有的组成部分都需要名字
类名、变量名以及方法名都被称为标识符
标识符注意点:
JAVA是一种强类型语言,要求变量必须先定义才能使用。
数据类型 变量名 = 值;
比如,
int num1 = 10;
也可以使用逗号隔开来声明多个同类型的变量,比如:
String name = "xujieyang", age = "male", sex = "default";
public class BasicDataType {
public static void main(String[] args) {
//八大基本数据类型
//整数
int num1 = 100; //最常用
byte num2 = 20;
short num3 = 30;
long num4 = 40L; //long类型要在数字之后加上L
//小数(浮点数)
float num5 = 50.1F; //float类型要在数字之后加上F
double num6 = 50.111;
//字符
char name1 = ‘A‘;
char name2 = ‘徐‘;
//字符只能包含一个字符
//字符串 注意,String不是关键词而是一个类
String name3 = "徐杰炀";
//布尔值 只有 是 和 非 两种值
boolean flag1 = true;
boolean flag2 = false;
}
}
包括类、接口、数组
由于JAVA是强类型语言,所以进行有些运算的时候需要用到类型转换。
转换的优先级为如下:
byte,short,char -> int -> long -> float -> double
在运算中,不同类型的数据先转化为同一类型再进行计算。
强制转换
将优先级高的转换为优先级低的类型,需要用到强制转换。
方法为,在变量名前加上(转换后的类型名称),比如:
public class Operations {
public static void main(String[] args) {
int i =128;
byte b = (byte) i; //强制转换为byte
System.out.println(i);
System.out.println(b);
}
}
// 128
// -128
这一段将i强制从int类型转换为byte类型,然后因为内存溢出,byte类型并不能表示128,因此向右移动了一个退回到了最左边的-128。
自动转换
将优先级低的转换为优先级高的类型,可以直接自动转换。
public class Operations {
public static void main(String[] args) {
int i =128;
float f = i;
System.out.println(i);
System.out.println(f);
}
}
// 128
// 128.0
注意:
JAVA变量是程序中最基本的存储单位,其要素包括变量名,变量类型和作用域。
注意事项:
public class Variables_demo {
int a = 1;
int b = 2;
int c = 3;
String name = "徐杰炀";
char char1 = ‘T‘;
double pi = 3.14;
}
变量作用域
类变量
需要加上static这个关键词
实例变量
不需要加上任何关键词
局部变量
现在方法中的变量
public class VariablesNameField {
//属性
//类变量
static double salary = 2500;
//实例变量 从属于对象 如果不进行初始化它会返回默认值0, 0.0, False 或者 null
String name;
int age;
//main方法
public static void main(String[] args) {
//局部变量 必须声明和初始化值
int i = 10;
System.out.println(i); //可以直接使用局部变量
//使用实例变量前要先实例化
VariablesNameField class_demo = new VariablesNameField(); //建立一个实例化的class_demo
System.out.println(class_demo.age); //打印class_demo的类变量age 结果为初始值0
//类变量可以直接调用
System.out.println(salary);
}
//其他方法
public void add(){
}
}
常量指初始化后不会再改变的值。不会变动的值。
常量可以被理解成一种特殊的变量,它的值被设定后,在程序运行过程中不允许被改变。
常量名一般使用大写字符。
设定常量的格式为:final 类型 常量名=值,比如:
public class Constant_Demo {
static final double PI = 3.14; //定义常量
public static void main(String[] args) {
System.out.println(PI);
}
}
// 3.14
算术运算,
public class Operators {
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((double)a/b); // /
System.out.println(a%b); // 余数
System.out.println(Math.pow(a, b)); // a的b次方
//Math是一个JAVA中自带的数学工具 pow是Math中幂运算的方法
}
}
逻辑运算,
public class LogicOperation {
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))); //取反运算 如果是真则为假,如果是假则为真
System.out.println("==============================");
// 拓展:短路运算
// 如果 && 与运算第一个参数为false,则会直接跳过之后的运算给出结果为false,这样的目的是为了提高效率
// 验证方法如下
int c = 5;
boolean d = (c<4) && (c++<4);
System.out.println(d);
System.out.println(c);
// 结果发现 c++并没有被执行
}
}
// a && b: false
// a || b: true
// !(a && b): true
// ==============================
// false
// 5
public class Operator_demo {
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
>> 右移运算 /2
0000 0000 -> 0
0000 0001 -> 1
0000 0010 -> 2
0000 0100 -> 4
0000 1000 -> 8
发现左移运算就是将二进制的数乘以2
而右移运算就是将二进制的数除以2
并且这个算法效率极高
*/
int a = 2;
System.out.println(a<<3); // 相当于a*2*2*2 = 16
}
}
// 16
public class Operator_demo2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
int d = 20;
a += b; // a = a + b
c *= d; // c = c * d
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
}
}
// 30
// 20
// 200
// 20
public class TrinityOperator {
public static void main(String[] args) {
// x ? y : z
// 如果x为真,则结果为y,否则结果为z
int score = 40;
boolean passOrNot;
passOrNot = (score >= 60) ? true : false; // 如果大于60分,则passOrNot为true,不然为false
String result = (score >= 60) ? "及格" : "不及格"; // 可以用一个string来接收结果
System.out.println(passOrNot);
System.out.println(result);
}
}
// false
// 不及格
package NewPackage; // 每个在package中定义的类都要包含这个声明
// 导入语句要在package下面
import java.util.Date; // 这边导入的Date类是java数据库中自带的 使用前也要先用import导入
public class Package_Demo {
public static void main(String[] args) {
Date date1 = new Date();
System.out.println(date1);
}
}
// Sun Feb 21 14:39:22 CST 2021
javadoc是用来生成自己的API自述文档的
参数信息包括:
/**
* @author JY
* @version 1.0
* @since 1.8
*/
// 以上为类注释
public class JavaDocDemo {
String name;
/**
*
* @param name
* @return
* @throws Exception
*/
// 以上为方法注释
public String test(String name) throws Exception{
return name;
}
}
在文件所在文件夹打开cmd,输入
javadoc -encoding UTF-8 -charset UTF-8 [文件名]
或者在IDEA工具菜单下添加
原文:https://www.cnblogs.com/cutomorrowsmile/p/14429543.html