Some basic stuff

Data and Object

Class Modifiers

Basic Types

Variable Modifiers

BufferedReader

Package

Writing Pattern

Exceptions

1. Data and Object

Data of Java Objects are stored in instance variables (also called fields)
Instance variables must have a type, either be a base type or a reference

2. Class Modifiers

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). An abstract class usually has a mixture of abstract methods and actual methods.

The final class modifier describes a class that 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.

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

3. Basic Types

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
double64-bit floating-point number

A variable declared to have one of these types simply stores a value of that type, rather than a reference to some object. Therefore when we create a number object, we do the following:

BaseTypeClassNameCreation ExampleAccess Example
byteByten = new Byte((byte)34)n.byteValue()
shortShortn = new Short((short)100)n.shortValue()
intIntegern = new Integer(1045)n.intValue()
longLongn =  new Long(10849L)n.longValue()
floatFloatn = new Float(3.934F)n.floatValue()
doubleDoublen = new Double(3.934)n.doubleValue()

4. Variable Modifiers

  • public: Anyone can access public instance variables.

  • protected: Only methods of the same package or of subclasses can access protected instance variables.

  • private: Only methods of the same class(not methods of a subclass) can access private instance variables.

  • If none of the above modifiers are used, the instance variable is considered friendly. Friendly variables can be accessed by any class in the same package.

  • static: A static variable 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. It can 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. it is a base type, then a constant.

    All parameters in java are passed by value, that is, any time we pass a parameter to a method, a copy of that parameter is made for use within the method body. So if we pass an int variable to a method, then that variable's integer value is copied. The method can change the copy but not the original. If we pass an object reference as a parameter to a method, then the reference is copied as well. Remember that we can have many different variables that all refer to the same object.

5. BufferedReader

java.io.BufferedReader stndin; // Standard input(buffered)
stndin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
使用"stndin.readLine()"就可以得到input

6. Package

用法: 在文件最上面写: package (package_name);

import (package_name).* ; 就能够import所有的在次包里面的文件了.

In the case where two packages have classes of the same name, we must specifically reference the package that contains a class. Like this:
Gnomes.Mushroom shroom = new Gnomes.Mushroom("purple");
Cooking.Mushroom topping = new Cooking.Mushroom();

7. Writing Pattern

Responsibility: divide the work into different actors, each with a different responsibility.  Try to describe responsibilities using action verbs. These actors will form the classes for the program.

Independence: define the work for each class to be as indempendent from other.

Behaviors: so that the consequences of each action performed by a class will be well understood by other classes that interact with it.

8. Exceptions

How to throw an exception object:

if (insertIndex > size()) {

  throw new

    BoundaryViolationException("No element at index " + insertIndex);

}

or

public void getReadyForClass() throws ShoppingListTooSmallException, OutOfMoneyException {

  goShopping();

  makeCookiesForTA();

}

Exception and Error are subclasses of Throwable, which denotes any object that can be thrown and caught.

Catching Exceptions:

try-catch block

try

   (block_of_statements_1)

catch ((exception_type) (identifier))

   (block_of_statements_2)

[catch ((exception_type) (identifier))

   (block_of_statements_3)

   ...

[finally

   (block_of_statements_n)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值