Java Fundamentals (v)

Source: Internet
Author: User
Tags stringbuffer

(i) object class
(1) Object: is the root class of a class hierarchy. Each class uses Object as the superclass (parent class)

public int hashcode () returns the hash code value of the object. Understand the address value (not the address value of the actual meaning)
Public final class GetClass () returns the run-time class for this Object

    Class类中有一个方法:            public String getName()以 String 的形式返回此                             Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。    返回值是Class(反射)(2) Object中的一个方法:    public String toString(); 回该对象的字符串表示    Integer:是int类型的包装类类型    它里面提供了一个方法        public static String toHexString(int i):以十六进制表示对字符串形式

Direct output Object name: The actual execution of ToString () in object, the full class name of the output @ hexadecimal data

I'm outputting the object name now, and I don't want ToString () to output an address value (the object's address value) directly.

You need to rewrite tosring () in Object, General: auto-Generate
(3) Public boolean equals (Object obj) indicates whether an other object is "equal" to this object.

= =: The value of the comparison is equal (address value)
Equals: The method in the object class itself, the default comparison is whether the address value is the same

按照正常情况:equals方法底层实际执行的两个对象在==,比较是地址值;假如:学生的年龄和姓名一致,看成同一个人

In a custom class, override the Equals () method in object to compare whether the value of a member variable of two objects is the same.
Automatically generated!
(4) Protected Object Clone () creates and returns a copy of this object

The Clone method of the Object class performs a specific copy operation. First, if the class of this object cannot implement interface cloneable, then Clonenotsupportedexception will be thrown.

    **  注意,所有的数组都被视为实现接口 Cloneable        (二)Scanner类**

(1) Scanner class: Simple text scanner.

Recall: The steps of keyboard entry
1) Need to create keyboard entry object
Scanner SC =new Scanner (system.in);
2) Importing package: Import Java.util.Scanenr; Ctrl+shift+o
3) Receive data
XXX Variable name = Sc.nextxxx ();

Construction Method:
Public Scanner (InputStream source): Data entry in the form of an input stream
InputStream: Byte input stream:

        InputStream  in = System.in ; //底层执行返回的是一个字节输入流(标准输入流)        (2)Scanner类的一些常见的方法:            XXX 变量名 = 键盘录入对象.nextXXX();

It provides a judgment function in the scanner class:
public boolean hasnextxxx (): Current scanner Determines if there is a next type of XXX data that can be entered
Nextxxx ();

    java.util.InputMismatchException:录入的数据和接收的数据类型不匹配异常    (3)问题:    键盘录入两个int类型的数据,分别输出,没有问题    键盘录入两个String类型的数据,分别输出:没有问题    先录入一个String类型的数据,在录入int类型的数据,没有问题    先录入一个int类型的数据,在录入String类型的数据,有问题,第二个数据是字符串类型,又由于录入下一个数据要"空格"本身是一个字符串

b is a space, B's value is not entered!
How to: Override Create keyboard entry objects
(c) String class

(1)String:表示字符串:    字符串是常量;它们的值在创建之后不能更改。

A string is a special type of reference:
Default value: null

(2) Construction method:
String (): No parameter construction
String (byte[] bytes): Converts the number of bytes into a string
public string (byte[] bytes, int index,int length): Converts part of a byte array to a string
public string (char[] value): Converts a character array to a string ToCharArray (): Converts a string to a character
public string (char[] value, int index, int count): Converts part of a character array to a string
public string (string original): Constructs a string constant as a string object

        常用的方法:                                public int length()返回此字符串的长度        (3)String字符串的最大的特点:                                        字符串一旦被赋值,其值不能被改变        (4)String类的常用的判断功能    boolean equals(Object obj):将此字符串与指定的对象比较    boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较,不考虑大小写    boolean contains(String str):判断当前大川中是否包含子字符串  (重点)    boolean startsWith(String str):以当前str字符串开头(重点)    boolean endsWith(String str):以当前str字符串结尾(重点)    boolean isEmpty():判断字符串是否为空    public String concat(String str):字符串的特有功能:拼接功能和+拼接符是一个意思(5) tring类的常用获取功能:

public int Length (): Gets the length of the string
public char charAt (int index) returns the character at the specified index
public int indexOf (int ch) returns the index at the first occurrence of the specified character in this string
Question: Since the character passed in: why this is the int type
Both ' a ' and 97 denote a
public int indexOf (int ch,int fromIndex) returns the index at the first occurrence of the specified character in this string and searches starting at the specified index.
public int indexOf (string str) returns the index of the first occurrence of the specified substring at this string
public int indexOf (string str,int fromIndex) returns the first occurrence of the index at the specified string in this string, starting at the specified index.

interception function
public string substring (int beginindex): Intercept from the specified position, truncate to the end by default, and return a new string
Public String substring (int beginindex, int endIndex): ends at the specified position to the end of the specified position, and the package does not contain
(6) Common conversion functions of string:

Public byte[] GetBytes (): Converting a string to a byte array
Public char[] ToCharArray (): Converts a string into a character array (emphasis)
public static string valueOf (int i): Converts data of type int to string (emphasis)
This method can convert any type of data into a string type
Public String toLowerCase (): Turns lowercase
public string toUpperCase (): All characters in the string become uppercase
(7) Other features of string type:
public string replace (char Oldchar,char Newchar): Replaces a character in a large string with a new character
public string Replace (string oldstr,string newstr): Replaces a substring in a large string
public string Trim (): Remove whitespace at both ends of string
(d) StringBuffer
(1) StringBuffer: variable character sequence for line Regulation regulation security

Threads (in multithreaded)

Thread security problem is difficult (multithreading is difficult) multithreading------> solves Multithreading Security issues------> is deadlock: Production consumption mode (waiting for wake-up mechanism in Java)

----> Inefficient execution of thread safety---> Synchronization
Example:
Bank's website, medical platform ...

Thread insecure----> out of sync----> High execution efficiency
Example:
news website, XXX forum ...
(2) The construction method of Tringbuffer:
StringBuffer (): Non-parametric construction form, initial capacity 16
StringBuffer (int capacity): Specifies the capacity to construct a string buffer
StringBuffer (String str) constructs a string buffer and initializes its contents to the specified string content

(3) StringBuffer access function:
public int Length () returns lengths
public int Capacity () returns the current capacity (if the capacity is exceeded, the system is automatically assigned (when the string is stored in English))
(4) StringBuffer add-on function
(More in real-world development):p ublic stringbuffer append (String/boolean ...): Appends data to the string buffer (appended at the end) and returns the string buffer itself

    public StringBuffer insert(int offset,String str):将当前str字符串添加到指定位置处,它返回字符串缓冲区本身

(5) StringBuffer Delete function:
Public stringbuffer deletecharat (int index): Removes the character at the specified position ...
Public StringBuffer Delete (int start,int end): Removes the substring from the specified position to the end-1 ...
(6) StringBuffer reversal function:
Public StringBuffer reverse (): Replaces the character sequence reversal in the buffer, returning it (string punch) itself
(7) The interception function of StringBuffer:
public string substring (int start): Intercept from the specified position, the default intercept to the end, the return value is not the buffer itself, but a new string
public string substring (int start,int end): From the specified position to the specified position end interception, after the packet is not wrapped, the return value is not the buffer itself, but a new string
(8) StringBuffer replacement function:
Public stringbuffer replace (int start,int end,string str)
Ends at the specified position to the specified position, replaced with a new STR string, and the return value is the string buffer itself
Note: Jdk5 features: auto-unboxing
int---interger (a wrapper class type of type int) requires an int--->string
Interger--int: Automatic Unpacking

Char---Character: char--string required

Why to convert between classes and classes

To convert a class type to type B, in order to use the function of Class B

Sometimes, there is a need to convert class B into Class A, which may require a type

Mutual conversion between string and StringBuffer
(v) Integer class
(1) Intege is a wrapper class type of type int
The integer provides static functionality:
public static String tobinarystring (int i)
public static String tooctalstring (int i)
public static String tohexstring (int i)

There is a guaranteed type for the base type counterpart, in order to convert the base data type to each other between string types

  • BYTE byte
  • Short Short
  • int Integer (speaking)
  • Long Long
  • float float
  • Double Double
  • char character (speaking)
  • Boolean Boolean
    (2) The construction method of integer:
    public Integer (int value)
    Public Integer (String s)
    (3) How to convert int to string type
    valueOf (int i);
    Converting a string to an int type
    static int parseint (String s);
    (4) Auto-unboxing is provided after JDK5.
    Integer--> can be disassembled int type
    You can type int---> Boxed integer type

    JDK5 also provides features:
    Generics, variable parameters, enhanced for loop (in the collection), etc...

    JDK8 Features:
    Stream--expression
    (vi) Character category
    (1) The Character class wraps the value of a base type char in an object. An object of type Character contains a single field of type Char.
    To determine the categories of characters (lowercase letters, numbers, and so on),
    Construction Method:
    Public Character (char value)
    (2) The judgment function of character class:
    public static Boolean isdigit (char ch) determines whether the specified character is a number.
    public static Boolean islowercase (int ch): Determines whether lowercase alphabetic characters
    public static Boolean isuppercase (int ch): Determines whether uppercase characters

    (3) Two conversion functions:
    public static int toLowerCase (int codepoint)
    public static int toUpperCase (int codepoint)
    (vi) Array Advanced section
    (1) Bubble sort: 22 comparison, large back, the first comparison is complete the maximum value is now the largest index ....
    (2) Select sort: The elements corresponding to the 0 index are followed by the corresponding elements of the index to compare, 1 index ... Small data forward method, you can get a well-ordered array ...
    (3) Element lookup method in Array (Basic lookup method)

    Binary find (binary search): arrays must be orderly
    Analysis:
    A: Define minimum and maximum indexes
    B: Calculate Intermediate Index
    C: the element corresponding to the intermediate index and the element to be searched for comparison
    Equal, returns the intermediate index directly
    Not equal:
    Big, left to find.
    max = mid-1;
    Small, on the right to find
    Min = mid + 1;
    E: Recalculate the intermediate index and go back to B to find

Java Fundamentals (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.