Java review

java basic knowledge review.

Primitive Data Types

byte, short, int, long, float, double, char, boolean

String

Actually a class.

String a = “ “

String Operation

String + String = StringString

String + Int = (String) StringInt // Int converted into String automatically

String Method

1
2
3
4
randomString.equals( " ");

randomString.toLowerCase();
randomString.toUpperCase();

Ternary Operator

isTrue ? resultTrue : resultFalse

方法 method

DiffMerge Tool

Parse values from Strings

Interger.parseInt( ) 将字符串解析成int

1
2
3
4
5
6
7
8
String numberAsString = "2018";
int number = Interger.parseInt(numberAsString);

numberAsString += 1; ()
// automatically convert int 1 to String "1";
// 20181
number += 1;
// 2019

You could say that parsing a string of characters is analyzing this string to find tokens, or items and then create a structure from the result.

In your example, calling Integer.parseInt on a string is parsing this string to find an integer.

So, if you have:

String someString = "123";

And then you invoke:

int i = Integer.parseInt( someString );

You’re telling java to analyze the "123" string and find an integer there.

Other example: One of the actions the java compiler does, when it compiles your source code is to “parse” your .java file and create tokens that match the java grammar.

When you fail to write the source code properly ( for instance forget to add a ; at the end of a statement ), it is the parser who identifies the error.

Here’s more information: http://en.wikipedia.org/wiki/Parse

OOP

Classes, Constructors

Inheritage

keyword this/ super

The this keyword refers to the current object in a method or constructor.

e.g. this.model = model; // where model is both the name for the class attribute and the parameter

while super is used to call parental fields.

this() call / super() call

this() calls the current constructor,多个构造方法时,减少代码重复。

super() calls the parental constructor.

Static / Instance methods / variables

static methods

static methods are declared using static modifier.

Cannot access instance methods / variables directly.

Used for operations that do not require any data from an instance of the class.

—> Methods that do not use instance variables should be declared as static methods.

In static method we cannot use this keyword.

Calling

Two patterns: ClassName.methodName(); or methodName() // only if in the same class

Declaration

Does the method use any fields?

Yes—> Instance method || N0—> Static methods.

static variables

e.g. when using Scanner will declare scanner as a static variable

static methods can access directly.

Class Features

Compositions

Encapsulation

  • when we dont want unathorized external access.

  • when we change a name of a method or a field, we dont want to change it in every class that the exact name has appeared.

Polymorphism

Actions being performed differently on different object.

Inner and Abstract Classes

Interfaces

Array, Lists, Autoboxing and Unboxing

Array

Array methods

List and ArrayList

list is a public interface

while ArrayList inherits from it.

ArrayList

1
2
3
4
5
6
7
8
9
10
Class ArrayList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

链表,用来存放String类型的对象,那么你可以像下面这样做:

ArrayList list = new ArrayList();

如果你需要创建一个指定初始容量的数组链表,你可以像下面这样做:

ArrayList list = new ArrayList(7);

注意:ArrayList类只支持对象类型,不支持 基础数据类型。就是说ArrayList对象只能存放对象,不能存放基础数据类型的数据。

ArrayList常用方法

下面是总结了一些比较常用的ArrayList类成员方法:

  • 增加元素到链表中

    • boolean add(Element e)
      增加指定元素到链表尾部.

    • void add(int index, Element e)
      增加指定元素到链表指定位置.

  • 从链表中删除元素

    • void clear()
      从链表中删除所有元素.

    • E remove(int index)
      删除链表中指定位置的元素.

    • protected void removeRange(int start, int end)
      删除链表中从某一个位置开始到某一个位置结束的元素。

  • 获取链表中的元素

    • E get(int index)
      获取链表中指定位置处的元素.

    • Object[] toArray()
      获取一个数组,数组中所有元素是链表中的元素.(即将链表转换为一个数组)

  • 修改某个元素

    • E set(int index, E element)
      将链表中指定位置上的元素替换成新元素。
  • 搜索元素

    • boolean contains(Object o)
      如果链表包含指定元素,返回true.

    • int indexOf(Object o)
      返回元素在链表中第一次出现的位置,如果返回-1,表示链表中没有这个元素。

    • int lastIndexOf(Object o)
      返回元素在链表中最后一次出现的位置,如果返回-1,表示链表中没有这个元素。

  • 检查链表是否为空

    • boolean isEmpty()
      返回true表示链表中没有任何元素.
  • 获取链表大小

    • int size()
      返回链表长度(链表包含元素的个数).

Attention

  • 向Arraylist里面传递对象时,不是新建一个对象存在Arraylist里面,而是引用它。(OOP作业 消费券)

  • //remember to use wrapper classes in ArrayList Type.

  • ```java
    Error (someLine, someByte):
    java: unexpected type
    required: reference
    found: int // You have to appoint wrapper class

                   // which is the capital-written version 
                   // of the primitive types. //Interger, Boolean, etc.
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    ##

    ## Autoboxing and Unboxing.



    ## Java Generics 泛型





    # Basic I/O 控制台输入输出

    ## 控制台输入

    ### Scanner 类 `import java.util.Scanner;`

    `Scanner sc = new Scanner(System.in);`

    **//读取字符串**

    `String n = sc.nextLine();`

    **//读取单个字符**

    ```java
    String s = sc.nextLine(); //Scanner 没有读取单个字符的方法

    char[] chars = s.toArray();

    char c = chars[0]; //c即为单个字符

Packages and Naming Conventions

Java Collections

Java FX

Networking Programming

Databases

Debugging