Java Basics Summary-common classes and package access rights

Source: Internet
Author: User
Tags array length dateformat float double square root string format string indexof stringbuffer

-----Object---The root class of all classes
1. How to get: through the constant upward extraction of all objects, with all the common objects of things
2. Common methods
* Equals (Object obj): Compares the two object references to the same object, essentially, if the reference address is the same, returns True, the function and the "= =" consistent, after its subclass inherits, only to determine the address is not meaningful, the method is generally rewritten, Overrides are compared to whether the object's unique content is the same. Also used polymorphic, for the extensibility of the code. The eg string equals the content.
Note A detail: There are polymorphism after rewriting, to access subclass-specific data, you must make a forced transformation
* Hashcode (): Based on hash value---hash algorithm--in-memory address
If a subclass does not override it, its implementation is system-dependent, and you can, of course, rewrite the method to redefine the hash value with the object's unique data.
The general object is the same as two aspects: the address of the object and the contents of the object, which is generally overridden by the Equals () Hashcode ().
*getclass (): To create an object, you must first have the class file that belongs to this object, different objects have different owning bytecode files--extract these bytecode files in common (name, construction method, etc.), encapsulated as Class
Typically, when a class file is loaded into memory, it is automatically encapsulated as an object of type class (only once), and then the object to which the class belongs is created.
GetClass () actually returns the byte-code file object of the class to which the current object belongs

The method of-----String------Common objects must be mastered
1. is a special object, two ways to create objects
2. Once the string is initialized, it cannot be changed-immutable objects
String str = "ABC"; s = "BCD"; represents the creation of two objects, s first pointing to the ABC entity, and then to the BCD entity
Note: the direct assignment string (stored in the string constant pool, once the string content is created the same way to point to the same object)
3.String str = "abc"; with Sting str1 = new String ("abc");
The former is stored in a constant pool of strings, which can be shared, the latter being stored in the heap new at once to produce an object (involving two objects in nature)
Str.equals (STR1); true--is overridden by the string class the Equals method of the object class--based on the string content
4. Common methods of operation
Construction Method:
With null parameters, string, byte array, character array, stringbuffer reference as argument--Convert to String
And the part of the array that consists of the string offset length start + length
General Method Classification:
1. Getting information about a string
Length length--See return value type S.length ()--int;
Gets the character charat (int index) of a specific position--char and supports an int type number of ASCII range
Get position based on character/string indexof (char c)--int first position (key mastery)
Index characters and string lastIndexOf (char c)
Get substring substring ()--string
2. Conversion of strings
Split string--Returns an array of strings (delimited by what string) split (string str)-involving regular expressions
(This method must be mastered)
String--Convert to character array ToCharArray ()--char[] Ch
String--Convert to byte array s.getbytes ()
Character case conversion in string toUpperCase ()/tolowercase (), etc.
Replaces the character/string in the string replace (char Oldchar,char Newchar);
Remove the space inside the string trim () Enter user information
String Connection concat () and + difference: former professional
3. Judging
string judgment equals () Equalsignorecase ()
Determines whether the string contains the specified string contains ()
Whether to start/end with the specified string startswith/endswith--Quick Find
4. Comparison (must be)
Compare string Size CompareTo ()--int (in front-to-back comparison) This.compareto (String str)
5. String intern () method-Returns the normalized representation of a string object
You can manipulate the string pool, and return objects that exist in the pool

----StringBuffer (guaranteed synchronization, thread safety)-----A string buffer object (emphasis)--as a container
1. Buffer is a container--temporary storage of data
2. Features: variable length, can store different types of data; default capacity is 16B
The buffer is essentially an array that encapsulates the array and provides methods externally.
The data to use the buffer must be converted to a string to be able to use it.
The string can be modified--
3. Functions that should be available
Add: Append ()--return value is StringBuffer object (put data in container, return value is that container)-Append Data Insert ()-can be inserted anywhere
Delete: Container-related methods Delete ()--/remove ()--
Find: Similar to string lookup methods
Modify: Replace () replaces Setcharat ()
Additions and deletions--basic functions of the container
Note: If the length of the buffer exceeds the processing: the array length is fixed
On the principle of variable array length *******
Creates a new array length = twice times the original array length, assigning the elements in the original array past
Buffer exists constructor method can specify the size of the buffer
An estimate of the size of a container--can be estimated--is specified as an estimated array length
---stringbuffer how to achieve synchronization----
Synchronous processing of the method of manipulating data-involving the method of handling data set to synchronous/synchronous (this is common)--but if multithreading is not involved-it is very slow to execute

---StringBuilder (does not guarantee synchronization)----API compatible with StringBuffer
1. Because of the different steps, so the execution speed is faster-for single-thread ratio is good
2. Multithreading using--stringbuilder single threaded use StringBuffer
Reason for 3.JDK upgrade: Simplified writing (often with limitations)-Increased efficiency-Increased security (trouble writing)
4. More data-store it-use Stringbuilder/stringbuffer if you output it as an entire string
5. Arrays can usually be one-to-one operation element

---the basic data type wrapper class---
In order to better manipulate the basic data type value, wrap it into an object parameter basic data wrapper class
Integer short Boolean Byte Long Character Float double--final class
Construction method: value,string--conversion to a numeric object
A constant that defines the range of the type is defined: Max_value min_value
A common approach
Numeric format string--basic numeric conversion (most commonly used) parseint (string s)-int (static method)
NumberFormatException runtime exceptions may be thrown (method declaration exceptions are expected to be handled by the caller)
There is no such method in character-(because it can be manipulated directly and numerically)
Intvalue ()--int converts an integer object to int (non-static)
ValueOf (String s)-integer
Binary Conversion---Decimal conversion binary, octal, Hex (other decimal)--static method
parseint (String s, int radix)--int decimal
Auto-boxing and automatic unpacking (JDK1.5 start later)
Auto-boxing: Assigning a basic data type to a reference
Auto-Unpacking: Calculating wrapper class objects and basic data types

---The System class---method is static
1. field (static member variable-reference)
Static PrintStream err--standard error output stream
Static InputStream in--standard input stream-standard input device keyboard
Static PrintStream out--standard output stream-standard output device display
2. Common methods
Long Currenttimemillis () indicates the number of milliseconds from 1970 1.1.00.00.00 to present
Properties getProperties ()
Gets the properties of the current system stored in the Properties<map key-value pair > collection
The set properties key-value pair is a string type-preferably using its own method to get the element
Stringpropertynames ()-set<string>, and then use the advanced for traversal collection and get all the elements

---runtime class---No construction method GetRuntime () returns this class object-singleton mode
Each Java program will have a runtime instance that makes it possible to connect to the running environment
Note: The main purpose is to understand the use of the singleton design pattern in this class

---math class---to provide mathematical applications (static methods)
Trigonometric functions, exponential function, absolute value, square root
Take integer function
Ceil (): Returns the smallest integer greater than the parameter
Floor (): Returns the largest integer less than the parameter
Round (): Returns the rounded integer
Random number: Random ()-double[0.0,1.0)

--date class---util pack inside
1. Method of construction Date () date (long date) milliseconds
Year-1900, month 0-11, hour 0-23, minute 0-59
2. Common methods
Print Date object directly-print result is date + time
Millisecond value--Date Object
* Through the construction method of date
* by calling SetTime (time)
The Date object--the millisecond value
* by calling GetTime () [You can also call getday and other methods to get the value of each field of the date]
Converting a date format--a conversion between a Date object and a string (DateFormat abstract class)
Date Object + Date Formatter object
date-string Format (data)
About customizing the date format: SimpleDateFormat (subclass of DateFormat)
There is a certain format defined in the class, and we can directly use its defined notation to represent the custom format
String-date
The method inside the Date parse (String source)--dateformat

---calendar abstract class---
Main role: Some of the methods defined are used to replace obsolete methods in date

----Access Rights-----
1. Benefits of the Package
Classify a class file
Provide multiple layers of namespaces to class files
Write on the first line of the program
The class name becomes: Package name. class Name
The package is also an extended form of
2. Access scope of four access rights
Scope Public protected Default private
Same package in same class ok ok ok
Not similar in the same package OK OK
Parent-Child OK in different packages
OK in different packages

Java Basics Summary-common classes and package access rights

Related Article

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.