Java Summary


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

TypeSize
booleanBoolean value: true or false
char16-bit Unicode character
byte8-bit signed two’s complement integer
short16-bit signed two’s complement integer
int32-bit signed two’s complement integer
long64-bit signed two’s complement integer
float32-bit floating-point number (IEEE 754-1985)
double64-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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值