JAVA 8の研究ノート - 第一章

第1章Javaのビルディングブロック

1つのコメント

//:単一行コメント

/ *

*

* /:複数行コメント

/ **

*

* /:Javadocコメント

 

2.クラス

メソッドとフィールドは、クラスは、基本的なビルディングブロックです:クラスは二つの主要な要素を持っています。

ファイルの名前は、クラスの名前と一致し、.javaファイルの拡張子を持っている必要があります。

コマンドラインでjavafileコンパイルします。javac xxx.java

JavaClassを実行します。java XXX

メイン方法:パブリック静的無効メイン(文字列[] args){}

main()メソッドがなければ、クラスはコンパイルできますが、実行することはできません。

 

3.インポート

指定されたパッケージで輸入のみのクラスを、それが指定の子パッケージ内のクラスをインポートしません。

java.langパッケージが自動的にインポートされます。

明示的なインポートは、ワイルドカードの存在の上にprecedureかかります。

競合を命名修正するために、完全修飾クラス名を使用します

輸入java.util.Date;

インポートjava.sqlの。*;

明示的なクラス名:

 java.util.Date日。// java.util.Date型という名前の日付の新しい組み込みのオブジェクト

 java.sql.Date sqldate; //

 

4.オブジェクトの作成

1)コンストラクタ

- 戻り値の型なしでクラスの名前と一致します。

- フィールドを初期化します

2)インスタンス初期化子

方法外側に表示されるコードブロックを{}、インスタンスイニシャライザと呼ばれます。

初期の3)順序

フィールドとインスタンス初期化子ブロックは、彼らがファイルに表示される順序で実行されます。

その後、コンストラクタが実行されます。

 

4.データの種類

1)プリミティブ型

8つのエージェントbulitイン:バイト、短い、int型、長い、フロート、ダブル、文字、ブール値。

デフォルトでは、Javaは、ユーザーが定義していることを前提としてい  int型を  リテラルと。

長い最大= 3123456789; //コンパイルされません。

長い最大= 3123456789L;

浮動小数点リテラルは二重であると仮定されるので、フロートは数以下F文字を必要とします。

float f = 2.2;  // doesn't compile

float f = 2.2f;

Undercore can be added into a number except at the beginning of a literal, the end of a literal, right before the decimal point , or right after the decimal point.

2) reference types

A reference type refers to an object (instance of a class).

Test s = new Test();

3) key differences

- A reference type can be asigned null, while primitive type can't.

- A reference type have methods, while primitive type don't.

 

6. Variables

Declare a variable: state the type and give a name.

Initialize a variable: give a value.

Multiple variables of defferent types can't be declared in the same statement.

String s, int num;  //doesn't compile

1) identifiers

- Can be combinations of letter, number, _ and $.

- Can't begin with numbers.

- Can't use java-reserved words.

- Java is case-sensitive.

Method and variable names begin with an lowercase letter followed by ComelCase.

Class names begin with an uppercase letter followed by ComelCase.

Package names use only lowercase.

Constant names use only uppercase.

2)initialization

Local variables is a variable defined within a method. Local variables must be initialized before use.

Instance variables are also called fields.

Class variables hava a keyword static before it, and shared by multiple objects.

As soon as you declare a class variable or instance variable, it's given a default value.

Default value:

boolean: false

byte, short, int, long: 0

float, double: 0.0

char: '\u0000'

reference type:  null

3) variable scope

Local variables can NEVER have a scope larger than the method they are defined in.

Local variables---in scope from declaration to the end of the block{};

Instance variables--in scope from declaration until object garbage collected

Class variables (static) -- in scope from declaration until program ends.

 

7. elements orders in class

1) package

2) import

3) class

mutiple classes can be defined in the same file, but only one of them is allowed to be public.  The public class matches the name of the file.

A file is also allowed to have neither classes be public.

 

8. garbage collection

1) System.gc(): suggests garbage collection to run, but java is free to ignore the request.

2) finalize(): is only run when the object is eligible for garbage collection. it could run zero or one time.

 

9. benefits of java

Object - Oriented: code defined in classes and instantiated into objects.

Encapsulation: Java supports access identifiers to protect data from unintended access and modification.

Platform Independent: compiled to bytecode and can be run on different systems.

Robust: Java manage memory on its own and does garbage collection automatically, proventing memory leaks.

Simple: get rid of operator overloading

Secure: Java code runs inside the JVM.

おすすめ

転載: www.cnblogs.com/shendehong/p/11703043.html