本文共 13324 字,大约阅读时间需要 44 分钟。
需要在终端运行java文件时
public class Hello{ public static void main(String[] args){ System.out.print("Hello World!"); }}
注释分为单行注释、多行注释以及文档注释3种
单行注释以 //
开头
多行注释以/*
开头,*/
结尾
文档注释以/**
开头,*/
结尾
public class HelloWorld { public static void main(String[] args) { //单行注释 输出一个 Hello World! System.out.println("Hello World!"); //多行注释:可以注释一段文字 /* 注释 */ /* 我是多行注释 我是多行注释 我是多行注释 */ //JavaDoc:文档注释 /** */ /** * @Description HelloWorld * @Anthor * */ /** * .-~~~~~~~~~-._ _.-~~~~~~~~~-. * __.' ~. .~ `.__ * .'// \./ \\`. * .'// | \\`. * .'// .-~"""""""~~~~-._ | _,-~~~~"""""""~-. \\`. * .'//.-" `-. | .-' "-.\\`. * .'//______.============-.. \ | / ..-============.______\\`. * .'______________________________\|/______________________________`. * */ }}
Java所有的组成部分都需要名字。类名、变量名以及方法名都被称为标识符。
标识符可以是中文,但是不建议这样使用
String 王者荣耀 = "荣耀王者";
基本数据类型(Primitive Type)
//八大基本数据类型: //整数 int num1 = 2147483647; //最常用 short num2 = 32767; byte num3 = 127; long num4 = 922L; //小数:浮点数 float num5 = 5.20F; double num6 = 3.141592653589793238462643; //字符 char name1 = 'A'; //字符串,String不是关键字,是类 String name2 = "www"; //布尔值:是 非 boolean flag = true;// boolean flag = true;//最好完全避免使用浮点数比较大小//最好完全避免使用浮点数比较大小//最好完全避免使用浮点数比较大小//整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0xint i = 10;int i2 = 010; //八进制0int i3 = 0x10; //十六进制0x 0~9 A~F
引用数据类型(Reference Type)
类Class、接口Interface、数组Array,除了八大基本类型,其他的都是引用数据类型。
优先级:
/** 低 ------------------------------------------->高* byte,short,char -> int -> long -> float -> double* */int i = 128;byte b = (byte)i; //内存溢出double d = i; //这里是自动转换//强制转换 (类型)变量名 高-->低//自动转换 低-->高/*注意点: 1、不能对布尔值进行转换 2、不能把对象类型转换为不相干的类型 3、在把高容量转换到低容量的时候,进行强制类型转换 4、转换的时候可能存在内存溢出,或者精度问题!*///操作比较大的数的时候,注意溢出问题//JDK7新特性,数字之间可以用下划线分隔int money = 10_0000_0000;
局部变量:使用之前必须声明和初始化
实例变量:从属于对象(类);如果不进行初始化,这个类型的默认值 0 0.0
布尔值默认false 除了基本类型,其余的默认值都是null类变量 static便于调用
public class Demo07 { //类变量 static 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); //变量类型 变量名字 = new Demo08(); Demo07 demo07 = new Demo07(); System.out.println(demo07.age); System.out.println(demo07.name); //类变量 System.out.println(salary); } //其他方法 public void add(){ }}
public class Demo08 { //修饰符,不存在先后顺序 final static double PI = 3.14; //常量用 final 定义 所有字母大写// static final double PI = 3.14; public static void main(String[] args) { System.out.println(PI); }}
类名:首字母大写和驼峰原则
常量:所有字母大写,单词之间加_下划线
其他:首字母小写和驼峰原则
整数加减时,有long类型的话结果为long类型;// 没有long类型的话结果为int类型;关系运算符返回结果: 正确 , 错误 布尔值 // ++ -- 自增 自减 一元运算符int a = 3;int b = a++; //执行这行代码后,先给b赋值,a再自增//a++ a = a + 1;System.out.println(a);//++a a = a + 1;int c = ++a; //执行这行代码前,a先自增,再给b赋值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);//falseSystem.out.println(c);//5/*A = 0011 1100B = 0000 1101-------------------------A&b = 0000 1100A|B = 0011 1101A^B = 0011 0001~B = 1111 0010位运算<< -> *2>> -> /2*/int a = 10;int b = 20;// 字符串连接符 + , StringSystem.out.println(a+b+" "); //30System.out.println(" "+a+b); // 1020// String类型在前面的话就不需要运算,全都看成字符串;// String类型在后面的话,前面照常运算//三元运算符// x ? y : z// 如果x为true,返回y;否则返回z
一般利用公司域名倒置作为包名
ex. com.baidu.www
@author 作者名@version 版本@since 指明需要最早使用的jdk版本@param 参数名@return 返回值情况@throws 异常抛出情况
// 通过终端命令行生成文档 javadoc 参数 Java文件// 编码问题 javadoc -encoding UTF-8 -charset UTF-8 Java文件//学会使用IDEA生成JavaDoc文档/*Tools -> Generate JavaDoc... -> -encoding utf-8 -charset utf-8 */
Java的Scanner还没这么理解,比如
if(scanner.hasNextInt())
这条语句中,.hasNextInt()方法的返回值是boolean类型,如果的返回的值是false。
Scanner每次调用都会有一次人机交互,也就是程序猿敲键盘进行‘I’操作,如果没有scanner.next()之类的语句的话,那之前进行的‘I’操作的内容无法被 ‘释放’ 掉,就会保存直至下一次调用Scanner。不知道这样你能不能看懂。
import java.util.Scanner;public class Demo05 { public static void main(String[] args) { // 我们可以输入多个数字,并求其总和平均值, // 每输入一个数字用回车确认, // 通过输入非数字来结束输入并输出执行结果 // 和 double sum = 0.0; // 计数 int count = 0; System.out.println("请依次输入数字:"); Scanner scanner = new Scanner(System.in); while(scanner.hasNextDouble()){ double x = scanner.nextDouble(); count += 1; sum += x; System.out.println("你输入了第"+count+"个数据,当前总数结果为"+sum); } System.out.println("总和平均值为" + sum/count); scanner.close(); }}
基本结构,从前至后。
if单选择结构
package struct;import java.util.Scanner;public class IfDemo01 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入内容:"); String s = scanner.nextLine(); // .equals:判断字符串是否相等 if (s.equals("hello")){ System.out.println(s); } System.out.println("End"); scanner.close(); }}
if双选择结构
import java.util.Scanner;public class IfDemo02 { public static void main(String[] args) { System.out.println("请输入成绩:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); if(i>=60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close(); }}
if多选择结构
import java.util.Scanner;public class IfDemo03 { public static void main(String[] args) { System.out.println("请输入成绩:"); Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); if(i==100){ System.out.println("恭喜满分!"); }else if (i>=85){ System.out.println("优秀"); }else if (i>=60){ System.out.println("及格"); }else { System.out.println("不及格"); } scanner.close(); }}
嵌套的if结构
switch多选择结构
public class SwitchDemo01 { public static void main(String[] args) { // scanner 无法读入 char 类型 思考怎么转变 char grade = 'B'; // case 具有穿透性,如果想只执行一个 case语句的话需要加入 break switch (grade){ case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); case 'C': System.out.println("及格"); case 'D': System.out.println("不及格"); } }}
while循环
public class WhileDemo02 { public static void main(String[] args) { // 计算 1+2+...+100 int i = 0; int sum = 0; while(i<100){ i++; sum += i; } System.out.println(sum); }}
do…while循环
public class DoWhileDemo01 { public static void main(String[] args) { int i = 0; int sum = 0; do{ i++; sum +=i; } while (i<100); System.out.println(sum); }}
for循环
public class ForDemo03 { // 打印九九乘法表 public static void main(String[] args) { System.out.println("==============九九乘法表=============="); for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) System.out.print(j+"*"+i+"="+i*j+"\t"); System.out.println(); } System.out.print("==============九九乘法表=============="); }}
增强for循环
public class ForDemo04 { // 增强for循环 public static void main(String[] args) { int[] numbers = { 10,20,30,40}; for (int x:numbers){ System.out.println(x); } }}
//continue语句用在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,//接着下一次是否执行循环的判断public class ContinueDemo { public static void main(String[] args) { int i = 0; while(i<100){ i++; if (i%10==0) { System.out.println(); continue; } System.out.print(i+"\t"); } System.out.println("==="); }}
//break在任何循环语句的主体部分,均可用break控制循环的流程//break用于强行退出循环,不执行循环中的剩余语句。(break语句也可用在switch语句中)public class BreakDemo { public static void main(String[] args) { int i = 0; while(i<100){ System.out.println(i); i++; if(i==30) break; } System.out.println("==="); }}
public class TestDemo01 { public static void main(String[] args) { // 打印三角形 for (int i = 1; i < 6; i++) { for (int j=5; j>i; j--) System.out.print(' '); for (int j=1; j<=i; j++) System.out.print('*'); for (int j=1; j
public class Demo02 { public static void main(String[] args) { int max1 = max(10, 20); System.out.println(max1); double max2 = max(10.0, 20.0); System.out.println(max2); } public static int max(int a, int b){ int result; result = a>=b? a: b; return result; } public static double max(double a, double b){ double result; result = a>b? a: b; return result; }}
到根包下才可使用 java 文件名(class)
method(double... numbers){}
public class Demo04 { public static void main(String[] args) { // 调用可变参数 printMax(34, 3, 2,56, 6.7); printMax(new double[] { 34, 3, 2,56, 6.7, 100.9}); } public static void printMax(double... numbers){ if(numbers.length==0){ System.out.println("No argument pass!"); return; } double result = numbers[0]; // 比较 for(int i = 1; i< numbers.length; i++){ if (numbers[i]>result){ result = numbers[i]; } } System.out.println("The max value is :" + result); }}
public class Demo05 { // 递归思想 (数据量大的时候尽量不要使用,栈机制会崩溃) public static void main(String[] args) { System.out.println(f(1)); } public static int f(int i){ int result; if(i==1){ return 1; }else { result = i*f(i-1); } return result; }}
public class ArrayDemo02 { public static void main(String[] args) { // 静态初始化:初始+创建 // 初始化结束之后 a数组的大小就确定了,数组大小不可更改 int[] a = { 1,2,3,4,5}; // 动态初始化:包含默认初始化 int[] b = new int[10]; b[9] = 10; for(int i=0;i<=a.length;i++){ System.out.println(a[i]); } }}
public class ArrayDemo03 { public static void main(String[] args) { int[] numbers = { 1,2,3,4,5}; printArray(numbers); System.out.println("Max:"+max(numbers)); } public static void printArray(int[] array){ for(int i=0; imax) max=array[i]; } return max; }}
public class ArrayDemo04 { public static void main(String[] args) { int[][] arrays = { { 1,2},{ 2,3},{ 3,4},{ 4,5}}; printArray(arrays[0]); System.out.println(arrays[0][0]); System.out.println(arrays.length); System.out.println(arrays[0].length); } public static void printArray(int[] array){ for(int i=0; i
Arrays.toString()方法用于数组的直接输出,输出的形式是字符// 升序排序Arrays.sort(a);// 填充Arrays.fill(b,0);// 比较数组是否相等Arrays.equals(a,b)
package array;public class ArrayDemo07 { public static void main(String[] args) { // 创建一个11*11的数组, 0表示没有棋子,1表示白旗,2表示黑棋 int[][] a = new int[11][11]; a[1][2] = 1; a[2][3] = 2; // 输出原始数组 System.out.println("输出原始的数组:"); for(int[] b:a){ for (int c:b) System.out.print(c+" "); System.out.println(); } System.out.println("==================="); // 压缩成稀疏数组 int sum = 0; // 记录非零个数 for (int i = 0; i < a.length; i++) for(int j=0; j
2021-02-05
转载地址:http://qxqez.baihongyu.com/