书籍:《Java核心技术卷I 基础知识》
内容:第三章3.1-3.7
主要内容:基础语法,数据类型,string字符串操作,输入输出
笔记:
import java.io.*; // 使用Console类
import java.nio.charset.StandardCharsets;
import java.nio.file.*; //使用Paths
import java.util.*; // Scanner 类的定义在java.util包中,当使用的类不是定义在基本java.lang包中时,需要使用相应的包 (类似头文件?)
import static java.lang.Math.*; //加上此行后,以后使用Math内的所有数学方法和常量名不再需要加入前缀Math.
/*Math 包含的内容:
* sin,cos,tan,atan,atan2
* exp(e^x),log,log10
* PI(圆周率),E (自然无理数e)
* */
public class helloworld {
public static final double MIN = -1000.0;// 若常量需要在多个类中使用,将这些常量设为类常量,使用static final 设置一个类常量
public static void main(String[] args) throws IOException {//已经知道有可能出现“ 输人 / 输出 ” 异常。这需要在 main 方法中用 throws 子句标记
//程序入口
//基础语法和数据
System.out.println("hello world");
System.out.print("hello");//不换行
System.out.println(111);
int a = 114514;
final double MAX = 100005; // final指示常量,相当于const
System.out.println("a = \n" + a);//用加号连接字符串和变量/常量
int b = 12, c = 43;
System.out.println("b = " + b + ", b * c = " + b * c);
System.out.printf("%d %d\n", b, c); //使用print类似c++的输出方式
System.out.println("MAX = " + MAX + ", MIN = " + MIN);
double x = 3.0;
double y = sqrt(x);
double z = pow(x, 3);
double n = atan(1);
System.out.printf("n = %f\n", n);
System.out.println("x = " + x + ", y = " + y + ", z = " + z);
System.out.println();
double q = 9.997;
int nq = (int) q;
int mq = (int) round(q);//round会进行舍入运算,以便得到更接近的整数
int w = (int) round(3.5);
int e = (int) round(3.4444444449);
System.out.println("q = " + q + ", nq = " + nq + ", mq = " + mq);
System.out.println(w + ", e = " + e);
//枚举类型
enum Size {s, m, l, xl}
Size s = Size.m;
/ 字符串 //
String greeting = "hello";
String str = greeting.substring(2, 5);// 截取一部分子串
String str1 = greeting + str;// 拼接字符串
String all = String.join("/", "114514", "1919810", "1437");// 多个字符串放在一起,用定界符分隔(例子中为"\")
String str2 = "qwe" + 13;//自动将后面内容转化为字符串
//string 不可整体修改,如果要把greeting改为“help”,只能: greeting = greeting.substring(0,3) + "p";
System.out.println(greeting + " " + str);
System.out.println(str1);
System.out.println(all);
System.out.println(str2);
boolean strj = "hello".equals(greeting);
boolean strj1 = "HELLO".equalsIgnoreCase(greeting);// 忽略大小写
boolean strj2 = "Hello".equals(greeting);
System.out.println(strj);
System.out.println(strj1);
System.out.println(strj2);
//null 与 空字符串 不一样,“”为一个空字符串,长度为0,内容为空,但是null表明目前没有任何对象与之关联
if (str != null && str.length() != 0) {
System.out.println(6);
}//检测其既不是null也不是空串
// string.length()得到的是代码单元数量,而码点数量(实际值)用string.codePointCount(0,string.length());
char first = greeting.charAt(0); //返回位置n的码点单元
int index = greeting.offsetByCodePoints(0, 3);
int cp = greeting.codePointAt(index);//得到第3个码点
//所以为了避免犯错,不要使用char类型!!!
System.out.println(first);
{
String sentence = "qwedsaqswqs";
int i = 0;
int cpp = sentence.codePointAt(i);
if (Character.isSupplementaryCodePoint(cp)) i += 2;
else i++;
//遍历一个字符串,并且一次查看每一个码点。
}
//构建字符串,用许多小段的字符串构建一个字符串,StringBuilder 是一个字符串构建器
StringBuilder builder = new StringBuilder();
//每次要添加一部分内容是,就调用append
char ch = 'a';
builder.append(ch);
builder.append(str);
String completed_string = builder.toString();
System.out.println(completed_string);
输入输出/
//读取输入
/*
* 首先构造一个Scanner对象,并与“标准输入流”System.in关联
* 然后就可以使用Scanner类的各种方法实现输入操作了
* */
Scanner in = new Scanner(System.in);
System.out.println("What's your name?");
String name = in.nextLine();//使用 nextLine 方法是因为在输人行中有可能包含空格.要想读取一个单词(以空白符作为分隔符),就调用next(),读取整数就是用nextInt(),浮点数就使用nextDouble().
System.out.println("How old are you?");
int age = in.nextInt();
System.out.println("Your name is " + name + " and next year, you'll be " + (age + 1));
//因为输入是可见的,不适用于从控制台读取密码。可以使用Console类实现这个目的。
Console cons = System.console();
String username = cons.readLine("User name:");
char[] passwd = cons.readPassword("Password:");
//为了安全起见,返回的密码存放在一维数组中。对密码处理之后,应该马上用一个填充值覆盖数组元素
//采用Console对象处理没有Scanner方便。
//文件输出输出
Scanner fin = new Scanner(Paths.get("myFile.txt"), StandardCharsets.UTF_8); // 用File创建一个Scanner对象
PrintWriter out = new PrintWriter("myFile.txt", StandardCharsets.UTF_8); //若文件不存在,创造该文件。
//文档注释,注释内容可被JDK提供的工具javadoc解析
/**
文档注释
@author qsn
@version v114.514
*/
}
}
有误的话还请dalao们指正Orz