CS61B Spring 2014 | Lecture Notes | 4. Types/Conditionals

这是我学习CS61B Spring 2014第四课整理的笔记,先记录下来课堂内容以便后期复习,希望大家学习愉快!

课堂内容版权为伯克利CS61B所有

课堂链接:https://www.bilibili.com/video/BV1vt411W7yu?p=3

Primitive Types
-built right into Java
-core to the language
-no need to create an object to store a primitive type
-can be stored in a local variable

byte: 8-bit integer
short: 16-bit
int: 32-bit
long: 64-bit; long x = 43L;
double: a 64-bit floating-point number
float: 32-bit, less accurate
boolean: “true” or “false”
char: a character

double & float values must have a decimal point:

double y = 18.0;
float f = 43.9f;
Object Types Primitive Types
Contains a reference value
How defined? class definition built into Java
How created? “new” “6”, “3.4”, “true”
How initialized? constructor default (usually zero)
How used? method operators ("+", “*”)

Create和initialize的区别:
-create: 让这个事物存在
-initialize: 决定这块内存用来储存什么

=================================
java.lang library:
-Math class x = Math.abs(y);
-Integer class int x = Integer.parseInt("1984");
-Double class double d = Double.parseDouble("3.14");

Java library 和 API 区别:
API is a description of all the methods that are in the library that can be called

=================================
Integers can be assigned to variables of longer types

int i = 43;
long l = 43;
i = (int)l; //this is called cast

=================================
Boolean values

boolean x = 3 == 5; // x is false

=================================
”Switch“ statements

switch (month) {
     
	case = 2:
		days = 28;
		break; //jumps to end of the "switch" statement
	case = 4:
	case = 6:
	case = 9case = 11:
		days = 30;
		break;
	default:
		days = 31;
		break;
}

如果别的条件不符合的话,java会选择default

=================================
The “return” keyword
-causes a method to end immediately
-control returns to the calling method

Prints numbers from 1 to x:

public static void oneToX(int x) {
     
	if (x < 1) {
     
		return;
	}
	oneToX(x - 1);
	System.out.println(x);

-Return也可以用来让一个function返回值

-function: method declared to return a non-void type

=================================
Iteration: a pass through the loop body

你可能感兴趣的:(java)