Java两大核心机制:
Java虚拟机(Java Virtual Machine):JVM是一个虚拟的计算机,具有指令集并使用不同的存储区域。负责执行指令,管理数据、内存、寄存器。Java虚拟机机制屏蔽了底层运行平台的差别,实现了“一次编译,到处运行”。
垃圾收集机制(Garbage Collection):
垃圾回收:将不再使用的内存空间进行回收。
在 C/C++ 等语言中,由程序员负责回收无用内存。
Java 不需要程序员负责回收无用的内存:它提供一种系统级线程跟踪存储空间的分配情况。并在JVM空闲时,检查并释放那些可被释放的存储空间。
垃圾回收在 Java 程序运行过程中自动进行,程序员无法精确控制和干预。
一个 Java 源文件中最多只能有一个 public 类。其它类的个数不限,如果源文件文件包含一个 public 类,它必须是该类名命名。
Java是强类型语言,每个变量必须先声明类型,后使用。
变量的作用域:一对{ }之间有效
Java 的浮点型常量默认为 double 型,声明 float 型常量,须后加 ‘f’ 或 ‘F’。
在Java中不可以写成3<x<6,应该写成x>3 && x<6 。还是python简洁啊。:-)
//: Property.java
import java.util.*;
/** The first Thinking in Java example program.
* Lists system information on current machine.
* @author Bruce Eckel
* @author http://www.BruceEckel.com
* @version 1.0
*/
public class Property {
/** Sole entry point to class & application
* @param args array of string arguments
* @return No return value
* @exception exceptions No exceptions thrown
*/
public static void main(String[] args) {
System.out.println(new Date());
Properties p = System.getProperties();
p.list(System.out);
System.out.println("--- Memory Usage:");
Runtime rt = Runtime.getRuntime();
System.out.println("Total Memory = "
+ rt.totalMemory()
+ " Free Memory = "
+ rt.freeMemory());
}
}
输出结果:
中文注释的例子:
若该类使用 public 修饰, 则类名必须和源文件名一致
/*
这是我们的第一个 Java 应用程序.
若该类使用 public 修饰, 则类名必须和源文件名一致
注意: 多行注释不能嵌套.
*/
class HelloWorld2{
//main 方法
public static void main(String [] args){
//在控制台中打印字符串.
System.out.println("HelloWorld!");
}
}
class Test{}
来一个详细的例子:
public class Test{
//成员变量,方法外部、类的内部定义的变量
int c = 12;
public static void main(String [] args){
//声明了一个变量, 局部变量:方法或语句块内部定义的变量
int a = 12;
a = 15;
System.out.println(a);
//关于变量的作用域
{
int b = 20;
System.out.println(b);
System.out.println(a);
}
//System.out.println(b);
//整型
byte i1 = 124;
short i2 = 3456;
int i3 = 3456789;
long i4 = 2345L;
//浮点型
//Java 的浮点型常量默认为 double 型,声明 float 型常量,须后加 ‘f’ 或 ‘F’。
float i5 = 12.34F;
//注意: 直接给 byte 类型的变量赋值为一个合法范围内的数值, 可以通过编译.
//而直接给 float 类型的变量赋值为一个合法范围内的浮点型, 则不能通过编译
byte i6 = 12;
float i7 = 12;
//i6 = 9;
//int i8 = 9;
//出现编译错误.
//i6 = i8;
//字符型
char c1 = '易';
System.out.print(c1);
//转义字符
char c2 = '\n';
System.out.print(c2);
//System.out.println() 打印后换行, System.out.print() 打印后不换行
System.out.println(c1);
//使用 unicode 编码来标示字符
char c3 = '\u0061';
System.out.println(c3); //'a'
System.out.println(c3 + 1); //'b'
//使用强制类型转换
System.out.println((char)(c3 + 1));
//String str = "abcde";
//boolean 类型
boolean b1 = true;
b1 = false;
//不可以0或非 0 的整数替代true和false,这点和C语言不同。
//b1 = 0;
//关于基本数据类型的转换.
//1. 自动类型转换
int i9 = 123;
float i10 = i9;
//2. 强制类型转换.
//使用时要加上强制转换符 ()
float i11 = 12F;
int i12 = (int)i11;
//注意: 不兼容的类型之间不能转换
//1. 字符串不能直接转换为基本类型
//2. boolean类型不可以转换为其它的数据类型
//3. 有多种类型的数据混合运算时,
//系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算
//两个 int 类型相除的结果仍是一个 int 类型.
//以下代码编译出错.
//int i13 = 12 / 5.0F;
//System.out.println(i13);
//4. byte,short,char之间不会相互转换,他们三者在计算时首先转换为int类型。
byte i14 = 12;
byte i15 = 13;
//以下代码编译出错.
//byte i16 = i14 + i15;
}
}
//类外面(类对应的大括号外面)不能有变量的声明
//int x = 123;
(3) 找出Property.java第二个版本的代码,这是一个简单的注释文档示例。请对文件执行javadoc,并在
自己的Web浏览器里观看结果。
(4) 以练习(1)的程序为基础,向其中加入注释文档。利用javadoc,将这个注释文档提取为一个HTML文
件,并用Web浏览器观看
TODO.........................
-------------------------------------------分隔线(变量,声明,赋值,初始化)-------------------------------------
结合声明和赋值的普遍语法有时叫做 初始化。
public class Initialization{
public static void main (String [] args)
{
int x = 1;
String empty = "";
double pi = 3.14159;//这种结合声明和赋值的普遍语法有时叫做 初始化。
System.out.println(x);
System.out.println(empty);
System.out.println(pi);
}
}
类型转换:转换一个浮点数到一个整数的时候使用typecase(类型转换)。它也叫Typecasting(类型转换)
public class TypeCast{
public static void main (String [] args)
{
double pi = 3.1415;
int x = (int)pi; //转换一个浮点数到一个整数的时候使用typecase(类型转换)。它也叫Typecasting(类型转换)
System.out.println(pi);
System.out.println(x);
}
}
Math:
public class MathDemo{
public static void main (String [] args)
{
double root = Math.sqrt(17.0);
double angle = 1.5;
double height = Math.sin(angle);
double degrees = 90;
double angle2 = degrees * 2 * Math.PI / 360.0; //PI大写
long x = Math.round(Math.PI * 20.0); //output 63
System.out.println(root);
System.out.println(height);
System.out.println(angle2);
System.out.println(x);
}
}
类型转换: Type conversion
public class StringAdd{
public static void main (String [] args)
{
printLogarithm(3.14);
}
public static void printLogarithm(double x){
if(x <= 0.0){
System.out.println("Positive numbers only, please.");
return; //return
}
double result = Math.log(x);
System.out.println("The log of x is " + result);//Type Conversion
}
}
You might wonder how you can get away with an expression like "The log of x is " + result, since one of the operands is a String and the other is adouble.In this case Java is being smart on our behalf, automatically converting the double to a String before it does the string concatenation.
Whenever you try to “add” two expressions, if one of them is a String, Java converts the other to a String and then perform string concatenation. What do you think happens if you perform an operation between an integer and a floating-point value?
"The log of x is " + result ,一个操作数是String,另一个操作数是double. 在这种表况下,java非常聪明地能够识别你的行为,会在拼接字符串之前将double转换成String类型。
无论你什么时候试着"add"两个表达式,如果有一个是String类型的,java 会把另一个转换为String,然后执行String的拼接操作。
==========================================================================================
==============================================================================================
术语2:点击打开链接 http://www.greenteapress.com/thinkapjava/html/thinkjava004.html#toc18
variable:
A named storage location for values. All variables have a type, which is declared when the variable is created.
value:
A number or string (or other thing to be named later) that can be stored in a variable. Every value belongs to a type.
type:
A set of values. The type of a variable determines which values can be stored there. The types we have seen are integers (int in Java) and strings (String in Java).小写int,大写String
keyword:
A reserved word used by the compiler to parse programs. You cannot use keywords, like public, class and void as variable names.
declaration:Java变量是需要声明的,python就不用。:)
A statement that creates a new variable and determines its type.
assignment:
A statement that assigns a value to a variable.
expression:
A combination of variables, operators and values that represents a single value. Expressions also have types, as determined by their operators and operands.
operator:
A symbol that represents a computation like addition, multiplication or string concatenation.
operand:
One of the values on which an operator operates.
precedence:
The order in which operations are evaluated.
concatenate: 端对端连接两个操作数
To join two operands end-to-end.
composition:结合单个表达式和语句来准确表示复杂计算的能力。
The ability to combine simple expressions and statements into compound statements and expressions to represent complex computations concisely.
3--initialization:初始化(声明且赋值)
A statement that declares a new variable and assigns a value to it at the same time.
floating-point:
A type of variable (or value) that can contain fractions as well as integers. The floating-point type we will use is double.
class:
A named collection of methods. So far, we have used the Math class and the System class, and we have written classes named Hello and NewLine.
method:
A named sequence of statements that performs a useful function. Methods may or may not take parameters, and may or may not return a value.
parameter:形参
A piece of information a method requires before it can run. Parameters are variables: they contain values and have types.
argument:实参
A value that you provide when you invoke a method. This value must have the same type as the corresponding parameter.
frame:
A structure (represented by a gray box in stack diagrams) that contains a method’s parameters and variables.
invoke:调用
Cause a method to execute.
2.11 练习
(1) 参照本章的第一个例子,创建一个“Hello,World”程序,在屏幕上简单地显示这句话。注意在自己的
类里只需一个方法(“main”方法会在程序启动时执行)。记住要把它设为static形式,并置入自变量列
表——即使根本不会用到这个列表。用javac编译这个程序,再用java运行它。
/****************** Exercise 1 ****************** * Following the HelloDate.java example in this * chapter, create a "hello, world" program that * simply prints out that statement. You need * only a single method in your class (the "main" * one that gets executed when the program * starts). Remember to make it static and to * include the argument list, even though you * don't use the argument list. Compile the * program with javac and run it using java. If * you are using a different development * environment than the JDK, learn how to compile * and run programs in that environment. ***********************************************/
public class HelloWorld{ //若该类使用 public 修饰, 则类名必须和源文件名一致 public static void main (String [] args) { System.out.println("Hello World");//打印一行 } }
总结:
1、类名首字母大写,编码风格、命名规范:包名,类名、接口名,方法名,变量名。
2、花括号,代码块
3、单行注释,doc注释
4、println是print newline的简写,打印后会换行
5、编译型语言,编译,运行
6、关键字:public static void
7、Java强类型语言,变量必须先声明,再使用。
8、变量的作用域,在一对花括号 { } 中有效。
(2) 写一个程序,打印出从命令行获取的三个自变量。(three arguments taken from the command line.)/****************** Exercise ****************** * Write a program that prints three arguments * taken from the command line. To do this, * you'll need to index into the command-line * array of Strings. ***********************************************/ public class ShowArgs{ public static void main (String [] args) { System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); } }
运行结果:
![]()
扩展,检测数组参数的长度:
/****************** Exercise ****************** * Testing for the length of the array first. ***********************************************/ public class ShowArgs2{ public static void main (String [] args) { if(args.length < 3){ System.out.println("Need three arguments"); System.exit(1); } System.out.println(args[0]); System.out.println(args[1]); System.out.println(args[2]); } }
System.exit()方法的作用是什么?非零值(1)标明执行失败。