zoukankan      html  css  js  c++  java
  • 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 define 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.

  • 相关阅读:
    vue table 中 列 加上 下划线和click 方法
    vue 比较好的学习文章
    Hive 以及mysql 中如何做except 数据操作
    oracle 日期维表 原始版本 带注解
    RMI 实现的rpc 远程过程调用 Java
    剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
    剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
    模拟通讯录
    剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
    剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。
  • 原文地址:https://www.cnblogs.com/shendehong/p/11703043.html
Copyright © 2011-2022 走看看