The term "instance variable" is another name for non-static field
The term "class variable" is another name for static field
A local variable stores temporary state; it is declared inside a method
A variable declared within the opening and closing parenthesis of a method signature is called a parameter
What are the eight primitive data types supported by the Java programming language?
byte short int long float double boolean char
Character strings are represented by the class
java.lang.String
An array is a container object that holds a fixed number of values of a single type.
2.2.2 Operators
2.2.2.1 Assignment,Arithmetic,and Unary Operators
The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value
++i相对效率更好
i++是先把i的值拿来用,然后在自增1
++i是想把i自增1然后拿来用
The arthimetric operators
Operator
Description
+
Additive
-
Subtraction operator
*
Multiplication operator
/
Division operator
%
Remainder operator
The unary operators
Operator
Description
+
Unary plus operator
-
Unary minus operator
++
Increment operator
--
Decrement operator
!
Logical complement operator
2.2.2.2 Equality,Relational, and Conditional Operators
The Equality and Relational Operators
==
equal to
!=
not equal to
>
gearter than
>=
greater than or equal to
<
less than
<=
less than or equal to
public class Array public static void main(String[] argv)
int value1=1; int value2=2; if(value1==value2)
System.out.println("value1==value2");
if(value1!=value2)
System.out.println("value1!=value2")
if(value1>value2)
System.out.println("value1>vluae2");
if(value1<value2)
System.out.println("value1<value2");
The Conditional Operators
public class ConditionalDemo1 public static void main(String[] argv)
int value1 = 1; int value2 = 2; if((value1==1)&&(value2==2))
System.out.println("value1 is 1 AND value2 is 2");
if((value1==1)||(value2==2))
System.out.println("value1 is 1 OR value2 is 1");
Ternary operator
if someCondition is true , assign the value of value1 to result. Otherwise assign the value of value2 to result
class ConditionalDemo2
public static void main(String[] args) int value1 = 1; int value2 = 2; int result; boolean someCondition = true; result = someCondition ? value1 : value2;
class BitDemo public static void main(String[] args) int bitmask = 0x000F; int val = 0x2222; // prints "2" System.out.println(val & bitmask);
2.2.2.4 Summary of Operators
Simple Assignment Operator
=
Simple assignment operator
Arthimetirc Operators
+
Additive operator(also used for String concatenation)
-
Subtraction operator
*
Multiplication operator
/
Division operator
%
Remainder operator
Unary Operators
+
Unary plus operator;indicates positive value
-
Unary minus operator;negates an exprssion
++
Increment operator
Equality and Relational Operators
==
Equal to
!=
Not equal to
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
Conditional Operators
&&
Conditional-And
||
Conditional-OR
?:
Ternary
Type Comparison Operator
instanceof
Compares an object to a specified type
Bitwise and Bit Shift Operators
~
Unary bitwise complement
<<
Signed left shift
>>
Signed right shift
>>>
Unsigned right shift
&
Bitwise AND
^
Bitwise exclusive OR
|
Bitwise incluseive OR
2.2.3 Expressions, Statements, and Blocks
Expression
An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value
Statements
Statements are roughly equivalent to sentences in natural languages
Block
block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.
2.2.4 Control Flow Statements
2.2.4.1 The if-then and if-then-else Statements
The if-then Statement
The if-then-else Statement
class IfElseDemo public static void main(String[] args)
int testscore = 76; char grade;
if (testscore >= 90) grade = ‘A‘; else if (testscore >= 80) grade = ‘B‘; else if (testscore >= 70) grade = ‘C‘; else if (testscore >= 60) grade = ‘D‘; else grade = ‘F‘;
System.out.println("Grade = " + grade);
一旦满足一个条件,剩余条件就不会被执行
2.2.4.2 The switch Statement
public class SwithDemoFallThrough
public static void main(String[] args)
java.util.ArrayList<String> futureMonths = new java.util.ArrayList<String>(); int month = 8; switch(month)
case 1: futureMonths.add("January"); case 2: futureMonths.add("February"); case 3: futureMonths.add("March"); case 4: futureMonths.add("April"); case 5: futureMonths.add("May"); case 6: futureMonths.add("June"); case 7: futureMonths.add("July"); case 8: futureMonths.add("August"); case 9: futureMonths.add("September"); case 10: futureMonths.add("October"); case 11: futureMonths.add("November"); case 12: futureMonths.add("December"); break; default: break;
if(futureMonths.isEmpty())
System.out.println("Invalid month number");
else
for(String monthName:futureMonths)
System.out.println(monthName);
2.2.4.3 The while and do-while Statements
while(expression)
statements
先执行一次,再判断
do
statement(s) while(expression)
2.2.4.4 The for Statement
for(initilization;termination;increment)
statement(s)
initilization:初始化表达式初始化循环,它在循环开始执行一次
termination:如果等于false,循环结束
increment: 每一次迭代后,increment 调用一次;可增,可减
more compact and easy to read
public class EnhancedForDemo public static void main(String[] args) int[] numbers=1,2,3,4,5,6,7,8; for(int item: numbers)
System.out.println(item);
2.2.4.5 Branching Statements
break
带标签的break
continue
public class BreakWithLabelDemo public static void main(String[] args) int[][] arrayOfInts=
32, 87, 3, 589 , 1, 1076, 2000, 8, 622, 127, 77, 955 ; int searchfor =12; int i; int j=0; boolean foundIt= false;
search:for(i=0;i<arrayOfInts.length;i++)
for(j=0;j<arrayOfInts[i].length;j++)
if(arrayOfInts[i][j]==searchfor)
foundIt=true; break search;
if(foundIt==true)
System.out.println("found it!");
else
System.out.println("not found it!");
public class ContinueWithLabelDemo
public static void main(String[] args)
String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max=searchMe.length()-substring.length();
test:for(int i=0;i<=max;i++)
int n=substring.length();
int j=i; int k=0;
while(n-- !=0)
if(searchMe.charAt(j++)!=substring.charAt(k++))
continue test;
if(foundIt=true)
break test;
if(foundIt)
System.out.println("found it");
else
System.out.println("Not found it");
return
Questions and Exercises
The most basic control flow statement supported by java programming language is the if-then statement
The switch statements allows for any number of possible execution paths
The statements is similiar to the while statement, but evaluates its expression at the botto, of the loop
How do you write an infinite loop using the for statement?
for( ; ; )
How do you write an infinite loop using the while statement?
while(true)
2.2.4.6 Summary of Control Flow Statements
2.2.4.7 Questions and Exercises
2.3 Classes and Objects
2.3.1 Classes
2.3.1.1 Declaring Classes
class declaration
class body
class MyClass
//fields,constructor, and //method declarations
MyClass继承于MySuperClass 实现接口YourInterface
class MyClass extends MySuperClass implements YourInterface
//field, constructor, and //method declarations
2.3.1.2 Declaring Member Variables
三种不同的变量:
类中的成员变量-叫做字段
方法或者代码中的变量叫本地变量
字段声明叫做参数
fields的例子:
public int cadence public int gear public int speed
Field declaration 由三个部分组成
修饰符:
Zero or more modifiers
字段类型
The field‘s type
字段名字
The field‘s name
Access Modifiers
public:
字段可以访问所有类
private:
字段仅能够再类中访问
public class PrivateBicycle
private int cadence; private int gear; private int speed;
public PrivateBicycle(int startCadence, int startSpeed, int startGear)