前言:雖然說學過設計模式,J2EE,這個學期才開始學Java,呵呵,有點顛倒了,但是還是要從基本的抓起。hoho~~
(一)一個簡單的java應用程式
Package edu.ynu.java.lession1
/*
The simplest Java program
*/
public class FirstJavaProg
{
public static void main(String[] args)
{
// System.out is the standard output stream.
System.out.println("Hello!");
}
}
(二)注釋
// This is my first Java program
(三)資料類型
byte (1 bytes, -128 … 127)
short (2 bytes, -32,768 … 32,767)
int (4 bytes, -2,147,483,648 … 2,147,483,647)
long (8 bytes)
Integer literals can be specified in several bases:
Decimal — -99 or 32174
Octal — 012 or 07
Hexadecimal — 0xff or 0XABCDEF01
float
4 bytes
6.5 significant digits
±3.4028E+38F
denoted by ‘F’ or ‘f’ suffix
double
8 bytes
15 significant digits
±1.7977E+308
denoted by ‘D’ or ‘d’ suffix (or no suffix)
if (Double.isNaN(x)) // check whether x is "not a number"
boolean
char
(四)變數
賦值和初始化
常量
Example:
public class Constants
{
public static void main(String[] args)
{
final double CM_PER_INCH = 2.54;
double paperWidth = 8.5;
double paperHeight = 11;
System.out.println("Paper size in centimeter: "
+ paperWidth * CM_PER_INCH + " by "
+ paperHeight * CM_PER_INCH);
}
}
(五)運算子
Arithmetic +, -, *, /, and %.
Prefix and postfix ++ and --.
Boolean ==, !=, <, <=, >, >=, &&, and ||.
Bitwise &, |, ^, and ~.
Arithmetic shift << and >>.
Logical shift >>>.
Java logical operators
&& (conditional AND)
& (boolean logical AND)
|| (conditional OR)
| (boolean logical inclusive OR)
^ (boolean logical exclusive OR)
! (logical NOT)
數學函數和常量
Constants (e.g, Math.PI)
Functions (e.g., Math.sin())
數實值型別之間的轉換
強制類型轉化
double x = 9.997;
int nx = (int)Math.round(x);
括弧和運算子層級
如記不得可以使用括弧
枚舉類型
enum Size{SMALL, MEDIUE, LARGE, EXTRA——LARGE};
Size s = Size.SMALL;
註:變數用小寫字母開頭,常量和類名用大寫字母開頭