JAVA 8学习笔记-第一章

CHAPTER 1  Java Building Blocks

1. comment

// : single-line comment

/*

*

*/ : multi-line comment

/**

*

*/ : javadoc comment

 

2. class

Classes have two primary elements: methods and fields, classes are basic building blocks.

The name of the file must match the name of the class, and have a .java extension.

compile javafile in commandline: javac xxx.java

run javaclass: java xxx

main method: public static void main(String[] args){}

Without main( ) method, the class can compile but can't run.

 

3. import

Imports only classes in the designated package, it doesn't import classes in child packages of designated.

java.lang package is automatically imported.

Explicit import takes precedure over the wildcard present.

Use fully qualified class name to fix naming conflict

import java.util.Date;

import java.sql.*;

Explicit class name:

 java.util.Date  date; //new-built object of type java.util.Date named date

 java.sql.Date sqldate; //

 

4.  object creating

1) constructor

- match the name of the class without a return type.

- to initialize fields

2) instance initializer

Codes blocks {}, which appear outside a method, are called instance initializers.

3) order of initialization

Fields and instance initializer blocks are run in the order in which they appear in the file.

Then the constructor runs.

 

4. Data type

1) primitive types

eight bulit-in:  byte, short, int, long, float, double, char, boolean.

By default, java assumes that you are defining an int with a literal.

long max = 3123456789;  // doesn't compile

long max = 3123456789L;

A floating-point literals are assumed to be double, so a float requires a letter f following the number.

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.

posted @ 2019-10-30 21:58  小白闯天涯  阅读(210)  评论(0编辑  收藏  举报