Java Summary

来源:互联网 发布:淘宝基金不见了 编辑:程序博客网 时间:2024/06/06 01:07

  • Important Names
  • Modifier
    • Class Modifier
    • Variable Modifiers
    • Method Modifier
    • Constructor Modifier
  • Primitive Types
  • Operator Precedence


Important Names

  1. Instance variables, which is also called fields. (实例变量,或叫域)
  2. Reference type(引用类型,对应于于primitive type)
  3. All parameters in Java are passed by value

Modifier

Class Modifier

  • The abstract class modifier describes a class that has abstract methods. Abstract methods are declared with the abstract keyword and are empty (that is, they have no block defining a body of code for this method). A class that has nothing but abstract methods and no instance variables is more properly called an interface, so an abstract class usually has a mixture of abstract methods and actual methods. (* abstract class must have abstract methods)
  • The final class modifier describes a class that can have no subclasses.
  • The public class modifier describes a class that can be instantiated or extended by anything in the same package or by anything that imports the class. Public classes are declared in their own separate file called classname .java, where “classname” is the name of the class.
  • If the public class modifier is not used, the class is considered friendly. This means that it can be used and instantiated by all classes in the same package. This is the default class modifier.

Variable Modifiers

  • public: Anyone can access public instance variables.
  • protected: Only methods of the same package or of its subclasses can access protected instance variables.
  • private: Only methods of the same class (not methods of a subclass) can access private instance variables.
    Additional:
  • static: The static keyword is used to declare a variable that is associated with the class, not with individual instances of that class. Static variables are used to store “global” information about a class (for example, a static variable could be used to maintain the total number of Gnome objects created). Static variables exist even if no instance of their class is created.
  • final: A final instance variable is one that must be assigned an initial value, and then· can never be assigned a new value after that. If it is a base type, then it is a constant (like the MAX_HEIGHT constant in the Gnome class). If an object variable is final, then it will always refer to the same object (even if that object changes its internal state).

Method Modifier

  • public: Anyone can call public methods.
  • protected: Only methods of the same package or of subclasses can call a protected method.
  • private: Only methods of the same class (not methods of a subclass) can call a private method.
  • If none of the modifiers above are used, then the method is friendly. Friendly methods can only be called by objects of classes in the same package.
    Additional:
  • abstract: Amethod declared as abstract has no code. The signature of such a method is followed by a semicolon with no method body. For example:
    public abstract void setHeight (double newHeight);
    Abstract methods may only appear within an abstract class.
  • final: This is a method that cannot be overridden by a subclass.
  • static: This is a method that is associated with the class itself, and not with a particular instance of the class. Static methods can also be used to change the state of static variables associated with a class (provided these variables are not declared to be final).
    Limitation of static methods:
    • Can only call other static methods.
    • Can only access static variable
    • Can not use “this” or “super” (“this” is associated with object and “super” is related to inheritance)

Constructor Modifier

Constructor modifiers follow the same rules as normal methods, except that an abstract, static, or final constructor is not allowed.


Primitive Types

Type Size boolean Boolean value: true or false char 16-bit Unicode character byte 8-bit signed two’s complement integer short 16-bit signed two’s complement integer int 32-bit signed two’s complement integer long 64-bit signed two’s complement integer float 32-bit floating-point number (IEEE 754-1985) double 64-bit floating-point number (IEEE 754-1985)
public class Base {    public static void main (String[J args) {     boolean flag true;     char ch 'A';     byte b 12;     short s = 24;     int i -257;     long I -890l; // note the use of "l" here     float f = 3.1415F; // note the use of "F" here     double d = 2.1828;     }}

Operator Precedence

Operator Precedence

0 0