语言基础
本文接续上一篇:跟着官方文档学Java_jdk1.8_(2)——系列
2 变量
前文说在面向对象的语言中,不用变量这个词。但是Java不讲码德。
所以在Java中,也使用变量。。。(field和varieble都可以用。)
命名
注:Java的官方的规定相对是宽泛的,而在实际的生产环境中,会用更严格的规范。也有助开发。
可以搜一下,网上阿里开发规范。
2.1基本数据类型
Java中有8种基本数据类型:
各类都有默认值
字面值
这里类型各种进制比较多
//整型
// The number 26, in decimal
int decVal = 26;
// The number 26, in hexadecimal
int hexVal = 0x1a;
// The number 26, in binary
int binVal = 0b11010;
//浮点型
double d1 = 123.4;
// same value as d1, but in scientific notation 这里是d1的科学计数法表示
double d2 = 1.234e2;
float f1 = 123.4f;
值得注意的是,在String 类型的字面值中,有转义字符
String literals:
\b (backspace),
\t (tab),
\n (line feed),
\f (form feed),
\r (carriage return),
\" (double quote),
\' (single quote),
\\ (backslash).
数字中使用下划线
这个功能不是很常用。
用下划线来使数字易读。
规则繁复。各位看官,有兴趣。有用,查一下。
2.2 数组
数组是一种类型的数字的容器。
一旦定义,长度就是固定的。元素可以更改。
上图是一个有10个元素的数组。
可以通过下标来获取每个元素。下标开始的值是0。
以下是一个数组案例:
class ArrayDemo {
public static void main(String[] args) {
// declares an array of integers声明一个整数数组
int[] anArray;
// allocates memory for 10 integers 分配存储10个元素
anArray = new int[10];
// initialize first element 第一个元素
anArray[0] = 100;
// initialize second element 第二个元素
anArray[1] = 200;
// and so forth 依次
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;
System.out.println("Element at index 0: "
+ anArray[0]);
System.out.println("Element at index 1: "
+ anArray[1]);
System.out.println("Element at index 2: "
+ anArray[2]);
System.out.println("Element at index 3: "
+ anArray[3]);
System.out.println("Element at index 4: "
+ anArray[4]);
System.out.println("Element at index 5: "
+ anArray[5]);
System.out.println("Element at index 6: "
+ anArray[6]);
System.out.println("Element at index 7: "
+ anArray[7]);
System.out.println("Element at index 8: "
+ anArray[8]);
System.out.println("Element at index 9: "
+ anArray[9]);
}
}
打印结果:
除了int,其它的基本数据类型也可以存储在数组中。
String,实例对象也都可以。
二维数组,也用的不多。(从略)
注:数组的长度属性,可以用length来输出。
练习
Questions
1.The term "instance variable" is another name for ___.
2.The term "class variable" is another name for ___.
3.A local variable stores temporary state; it is declared inside a ___.
4.A variable declared within the opening and closing parenthesis of a method signature is called a ____.
5.What are the eight primitive data types supported by the Java programming language?
6.Character strings are represented by the class ___.
7.An ___ is a container object that holds a fixed number of values of a single type.
Exercises
1.Create a small program that defines some fields. Try creating some illegal field names and see what kind of error the compiler produces. Use the naming rules and conventions as a guide.
2.In the program you created in Exercise 1, try leaving the fields uninitialized and print out their values. Try the same with a local variable and see what kind of compiler errors you can produce. Becoming familiar with common compiler errors will make it easier to recognize bugs in your code.
答案:
1. non-static field
2. static field
3. method
4. parameter
5. byte,short,int,long,char,float,double,boolean
6. java.lang.String
7. array
3 运算符
列表
赋值、算术和一元运算符
=
就是赋值- 算术(5种)
如果你学过c/c++
和它们一样。四则运算,再有一个取余(也称模)。
class ArithmeticDemo {
public static void main (String[] args) {
int result = 1 + 2;
// result is now 3
System.out.println("1 + 2 = " + result);
int original_result = result;
result = result - 1;
// result is now 2
System.out.println(original_result + " - 1 = " + result);
original_result = result;
result = result * 2;
// result is now 4
System.out.println(original_result + " * 2 = " + result);
original_result = result;
result = result / 2;
// result is now 2
System.out.println(original_result + " / 2 = " + result);
original_result = result;
result = result + 8;
// result is now 10
System.out.println(original_result + " + 8 = " + result);
original_result = result;
result = result % 7;
// result is now 3
System.out.println(original_result + " % 7 = " + result);
}
}
特别地,+
也可以作为字符串拼接符号。
举例:
class ConcatDemo {
public static void main(String[] args){
String firstString = "This is";
String secondString = " a concatenated string.";
String thirdString = firstString+secondString;
System.out.println(thirdString);
}
}
输出结果是:“This is a concatenated string.”
3.一元运算符
class UnaryDemo {
public static void main(String[] args) {
int result = +1;
// result is now 1
System.out.println(result);
result--;
// result is now 0
System.out.println(result);
result++;
// result is now 1
System.out.println(result);
result = -result;
// result is now -1
System.out.println(result);
boolean success = false;
// false
System.out.println(success);
// true
System.out.println(!success);
}
}
比较烦人的是++
和--
,它们在前和在后,还不太一样。
有些语言就没有这个问题。如Scala.
举个烦人的例子。
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
// prints 4
System.out.println(i);
++i;
// prints 5
System.out.println(i);
// prints 6
System.out.println(++i);
// prints 6
System.out.println(i++);
// prints 7
System.out.println(i);
}
}
等,关系,条件运算符
== equal to 等于
!= not equal to 不等于
> greater than 大于
>= greater than or equal to 大于或等于
< less than 小于
<= less than or equal to 小于或等于
举例
class ComparisonDemo {
public static void main(String[] args){
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 > value2");
if(value1 < value2)
System.out.println("value1 < value2");
if(value1 <= value2)
System.out.println("value1 <= value2");
}
}
&& Conditional-AND 与
|| Conditional-OR 或
也可将!
算作逻辑运行符非
class ConditionalDemo1 {
public static void main(String[] args){
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 == 1))
System.out.println("value1 is 1 OR value2 is 1");
}
}
三目运算符
也算在逻辑运算符里。
直接举例
class ConditionalDemo2 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
System.out.println(result);
}
}
对象类型的比较
关键字instanceof
class InstanceofDemo {
public static void main(String[] args) {
Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent: "
+ (obj1 instanceof Parent));
System.out.println("obj1 instanceof Child: "
+ (obj1 instanceof Child));
System.out.println("obj1 instanceof MyInterface: "
+ (obj1 instanceof MyInterface));
System.out.println("obj2 instanceof Parent: "
+ (obj2 instanceof Parent));
System.out.println("obj2 instanceof Child: "
+ (obj2 instanceof Child));
System.out.println("obj2 instanceof MyInterface: "
+ (obj2 instanceof MyInterface));
}
}
class Parent {}
class Child extends Parent implements MyInterface {}
interface MyInterface {}
位运算符
是基于底层,二进制数计算。
class BitDemo {
public static void main(String[] args) {
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println(val & bitmask);
}
}
一般开发者,好像不常用???
Questions
1. Consider the following code snippet.
arrayOfInts[j] > arrayOfInts[j+1]
Which operators does the code contain?
2.Consider the following code snippet.
int i = 10;
int n = i++%5;
What are the values of i and n after the code is executed?
What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i))?
3.To invert the value of a boolean, which operator would you use?
4.Which operator is used to compare two values, = or == ?
5.Explain the following code sample: result = someCondition ? value1 : value2;
Exercises
1.Change the following program to use compound assignments:
class ArithmeticDemo {
public static void main (String[] args){
int result = 1 + 2; // result is now 3
System.out.println(result);
result = result - 1; // result is now 2
System.out.println(result);
result = result * 2; // result is now 4
System.out.println(result);
result = result / 2; // result is now 2
System.out.println(result);
result = result + 8; // result is now 10
result = result % 7; // result is now 3
System.out.println(result);
}
}
2.In the following program, explain why the value "6" is printed twice in a row:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
i++;
System.out.println(i); // "4"
++i;
System.out.println(i); // "5"
System.out.println(++i); // "6"
System.out.println(i++); // "6"
System.out.println(i); // "7"
}
}
Q:
1. >大于号 +加号
2.i = 11; n = 0; i = 11; n = 1;
3.!
4.==
5.如果result = someCondition, output value1; otherwise, assign the value2
4.表达式、声明和块
表达式
int cadence = 0;
anArray[0] = 100;
System.out.println("Element 1 at index 0: " + anArray[0]);
int result = 1 + 2; // result is now 3
if (value1 == value2)
System.out.println("value1 == value2");
如上,是表达式的例子。
总的来说,就是要清晰。
声明和语句块,略。比较简单。
练习
Questions
Operators may be used in building ___, which compute values.
Expressions are the core components of ___.
Statements may be grouped into ___.
The following code snippet is an example of a ___ expression.
1 * 2 * 3
Statements are roughly equivalent to sentences in natural languages, but instead of ending with a period, a statement ends with a ___.
A block is a group of zero or more statements between balanced ___ and can be used anywhere a single statement is allowed.
Exercises
Identify the following kinds of expression statements:
aValue = 8933.234;
aValue++;
System.out.println("Hello World!");
Bicycle myBike = new Bicycle();