Java心得

介绍

开始学这个是因为普林斯顿的算法,第一课我看了半天,结果也没学写出来。气馁并放弃。

2020/08/23 再次鼓起勇气出发,参考了网友的建议,拿出Head First Java。

基本概念

所以要怎样学习呢?首先,你必须能够理解,然后要确保不会忘记。

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

  • 可能是要借助于JVM(虚拟机)来实现?因为所有地方都有JVM

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

void代表没有返回值

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

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

不一定每一个类都需要一个main()

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"); //字符要用双引号括起来,单引号不行
   }
}

如果想用一个print打印多个数据,可以用加号连接(默认没有空格直接连接)。

可以采用“\n”来创建新的一行,但是更好的操作是 to use println to output a newline when possible, as println has some technical advantages not mentioned here.

变量

变量类型和变量赋值可以放到同一行。

int abc = 10;

变量名identifier:

  • 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.

变量类型有: int, double,

定义不会更改的变量(Constant/final Variable):A common convention, or good practice, is to name constant variables using upper case letters with words separated by underscores, to make constant variables clearly visible in code.

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.

变量类型转换:A programmer can precede an expression with (type) to convert the expression’s value to the indicated type.

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

整数类型:8-16-32-64 bits对应着byte-short-int-long
浮点类型:32-64 bits对应着float-double. Overflow with floating-point results in infinity.

函数-Method

函数里面的变量在定义的时候被称为参数parameter,在调用时被称为argument

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(""+...) 以防止将...以数字的形式打印

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!