# 基本概念

Java具有跨平台可移植性，写一次就可以在所有地方执行。

• 可能是要借助于JVM（虚拟机）来实现？因为所有地方都有JVM

Java的程序结构：源文件 -> 类 -> 方法

void代表没有返回值

Java有三种循环：while / do-while / for

Java所有的东西都包含在类中，因为Java是面向对象的语言。

print与println的区别在于，println会在最后面插入换行

java的main函数为什么是public static void main(String[] args)

• 因为jvm启动时，直接去找static的，参数为String数组的main方法。
Math.random() // 可以产生一个0~1的随机数

### 基础知识

A sequence of instructions that solves a problem is called an algorithm.

A simple Java program contains:

• A program starts in main(), executing the statements within main’s braces { }, one at a time.
• Each statement typically appears alone on a line, and ends with a semicolon, like English sentences end with a period.

//1.这是单行注释，两个斜杠
/*
2.这是多行

*/
// 这样也可以，后面的双斜杠会被忽略 //
/*
Java supports a third type of comment, known as a JavaDoc comment (discussed elsewhere), which is a specially formatted multi-line comment that can be converted to program documentation in HTML.
*/

yetAnotherVar = myFirstVar;        // Aligned = operators
thirdVar      = yetAnotherVar + 1; //相关联的语句等号也要对齐

Some errors create an upsettingly long list of error messages. Good practice is to focus on fixing just the first error reported by the compiler, and then re-compiling.

A logic error, also called a bug/runtime error, is an error that occurs while a program runs.

In fact, many programmers recommend the good practice of configuring compilers to print even more warnings. For example, javac can be run as javac -Xlint yourfile.java

A bit can only have the value of 0 or 1.

Some programmers warn against using floating-point for money, as in 14.53 representing 14 dollars and 53 cents, because money is a countable item

NaN: Not a number

public indicates the method may be called from any class in the program, and static indicates the method only uses values that are passed to the method;

### 输入数据

Scanner is a text parser that can get numbers, words, or phrases from an input source such as the keyboard. Getting input is achieved by first creating a Scanner object via the statement:

import java.util.Scanner;
Scanner scnr = new Scanner(System.in);

System.in corresponds to keyboard input. Then, given Scanner object scnr, the following statement gets an input value and assigns x with that value:

x = scnr.nextInt();

### 打印

System.out.print 正常打印. System.out.println (note the ln at the end, short for “line”)在打印结束之后自动开始新的一行。Outputting a blank line is achieved by: System.out.println().

public class KeepCalm {
public static void main (String [] args) {
System.out.println("Keep calm");// println starts new line after output
System.out.print("and");       // Note: Does NOT print on new output line
System.out.println("carry on"); //字符要用双引号括起来，单引号不行
}
}

# 变量

int abc = 10;

• must be a sequence of letters (a-z, A-Z), underscore (_), dollar signs ($), and digits (0-9) • must start with a letter, underscore, or dollar sign • case sensitive 两种命名哲学： 1. Lower camel case，除了第一个单词之外其他的首字母大写：numApples or peopleOnBus 2. Underscore separated，用下划线连接：num_apples or people_on_bus A good practice followed by many Java programmers is to not use _ or$ in programmer-created identifiers.

final double SPEED_OF_SOUND   = 761.207;

Because of implicit conversion, statements like

double someDoubleVar = 0;
someDoubleVar = 5;

are allowed, but discouraged. Using 0.0 or 5.0 is preferable.

avgKidsPerFamily = (double)(kidsInFamily1 / kidsInFamily2)
//但是这样的问题在于没啥子用，应该把double加在两个数字中的一个，而不是加在最外面。

# 函数-Method

A method can only return one item, not two or more.
A void return type means the method returns no value.

# 包包

Math: Sqrt(x), pow(x,y), abs(x), import java.lang.Math;

# 错误总结

If a program needs a floating-point result from dividing two integers, then at least one of the integers needs to be converted to double so floating-point division is performed.

System.out.println(""+...) 以防止将...以数字的形式打印