Tag: obj means swap print 1.5 long can learning 5.5
Reference data type
Reference data type classification, refers to reference data type (class), in fact, we are not unfamiliar with it, such as the use of the scanner class, the random class.
We can put the type of the class into two kinds:
- The first, Java provides us with good classes, such as the scanner class, the random class, etc., These existing classes contain a lot of methods and properties that we can use.
- second, we create our own classes, according to the definition of the class, you can include multiple methods and properties in the class for us to Use.
Here we mainly introduce the second case of simple use.
Overview of custom data types
In java, you abstract real-life things into Code. At this point, we can use a custom data type (class) to describe (map) things in real life.
class, which is a reference data type, which is the same as all previously learned reference data types, and a custom class is also a data type. Just the custom type is not Java for us to provide a good type beforehand, but rather we define a reference data type to describe a thing.
Definition and use of classes
The process of mapping Java code into real things is the process of defining classes.
So we're going to take a cell phone and analyze it, what can it do? It can call, surf the internet, chat and so on, these are the functions provided by the mobile phone, that is, the method, mobile phone also has its characteristics, such as color, size, brand model, etc., These are the characteristics of mobile phones, that is, Attributes.
For now, we are only interested in the properties in the class, and the methods in the class are learned in the object-oriented part.
The definition format of the class
Create a Java file that is the same as the class name
public class class name { data Type property name 1; data Type property name 2; ... }
The description of the phone class is made by the definition of the class, as shown below
public class Phone { / * * attribute * * String brand; // Brand Model String color; // Color Double // size}
The code above, is the process of creating a class, the name of the class we named the phone, the class contains three attributes (brand model, color, size size). Note that the attributes defined in the class do not have a number of requirements.
The use format of the class
Once the phone class is defined, we can use this class in a way that is similar to using the reference data type scanner class. The format is as Follows:
- Guide Package: We put all the classes in the same folder, you can avoid the guide package;
- Create Object: data type variable name = new data type ();
- Call Method: Currently we define a custom class that does not involve methods, just attributes;
- Access attribute: Variable Name. property (this is the current way, and later the method is called to replace the direct access way to complete the access to the Property.) )
When you have a variable for the phone data type, we can use the properties in the phone class. Access to the properties let's demonstrate that as Follows:
packagearraylist; public classTest { public Static voidmain (string[] Args) {//defines a variable p for the phone typePhone p =NewPhone ();/** Through p, use the properties in Phone*///access to brand properties in PP.brand = "apple 6s";//assign a value of Apple 6s to the brand property in P//to access the Color property in PP.color = "white";//assign a value of "white" to the Color property in P//Accessing the Size Size property in PP.size = 5.5;//assign a value of 5.5 to the Size property in PSystem.out.println ("mobile Phone brand" +p.brand); System.out.println ("phone Color is" +p.color); System.out.println ("phone size is" +p.size); }}//The handset brand is Apple 6s//phone color is white//phone size is 5.5
Custom type considerations vs. memory Graphs
In the code above, the variable p created by the class phone, which corresponds to the box in our life, contains the properties it can Use.
Properties can be manipulated by using the P. property name
Like an array of reference types, a variable of a custom type of a reference type, a direct variable, results in an object address value, which can be simply explained by a memory Graph.
Let's look at a class to create a memory graph of two objects:
ArrayList Collection
In the previous we learned about arrays, arrays can hold multiple elements, but in some cases it is not possible to determine exactly how many elements to save, at which point the array will no longer apply because the length of the array is Immutable. To preserve these indeterminate numbers of elements, the JDK provides a series of special classes that can store any type of element and are variable in length, collectively referred to as a Collection. here, We introduce the ArrayList collection, and the rest of the collections are taught in subsequent Courses.
The ArrayList collection is the most common collection in a program, which belongs to the reference data type (class). A variable-length array is encapsulated inside the arraylist, and when the deposited element exceeds the array length, ArrayList allocates a larger array in memory to store the elements, so the ArrayList collection can be thought of as a variable-length array.
Creation of collections
The common format for creating collections is described here:
1, Guide package: Import java.util.ArrayList;
2. Create objects: identical to other normal reference data types, but specify the data types stored in the Container:
3. arraylist< The data type of the element to be stored > variable name = new arraylist< the data type to store the element > ();
- The elements stored in the collection, only the data type elements specified in the <> brackets;
- The data type in the < data type > to store element must be a reference data type and cannot be a basic data type;
The following is a representation of the reference data type for the 8 basic data types:
Reference data type representation for the base data type
byte -- byteshort-- shortint--> Integerlong< /c7>-- Longfloat--- floatdouble -- DoubleChar-- Character----boolean
Let's take a few examples to clarify how a collection is Created:
// storing elements of type string New arraylist<string>(); // storing data of type int New arraylist<integer>// storage Phone type data new arraylist<phone> ();
common methods in collections
next, Let's learn some of the common methods provided by the ArrayList collection, as Follows:
Boolean Add (object obj) //appends the specified element obj to the end of the collection Object get (int Index) //returns The element at the specified position in the collection int size () //returns The number of elements in the collection
The following code demonstrates the use of the above METHODS. Arraylistdemo01.java
packagearraylist;Importjava.util.ArrayList; public classArrayListDemo01 { public Static voidmain (string[] Args) {//Create ArrayList Collectionarraylist<string> list =NewArraylist<string>();//adding elements to the collectionList.add ("stu1"); List.add ("stu2"); List.add ("stu3"); List.add ("stu4");//gets the number of elements in the collectionSystem.out.println ("length of the collection:" +list.size ());//Remove and print the element at the specified positionSystem.out.println ("the 1th element is:" + list.get (0)); System.out.println ("the 2nd element is:" + list.get (1)); System.out.println ("the 3rd element is:" + list.get (2)); System.out.println ("the 4th element is:" + list.get (3)); }}//length of collection: 4//the 1th element is: stu1//the 2nd element is: STU2//the 3rd element is: Stu3//the 4th element is: Stu4
It is emphasized that the ArrayList collection is equivalent to a variable-length array, so accessing the elements in the collection is also indexed, the first element is stored at index 0, the second element is stored in the position of index 1, and so On.
Traversal of a collection
By iterating through the collection, you get each element in the collection, which is the most common operation in the Collection. The traversal of a collection is much like the traversal of an array, and is indexed by the way the collection is Traversed: Arraylistdemo02.java
packagearraylist;Importjava.util.ArrayList; public classArrayListDemo02 { public Static voidmain (string[] Args) {//Create ArrayList Collectionarraylist<integer> list =NewArraylist<integer>(); //adding elements to a collectionList.add (13); List.add (15); List.add (22); List.add (29); //iterating through the collection for(inti = 0; I < list.size (); I++) {//[gets The number of elements in the collection]//get to each element in the collection by index intn = list.get (i);//[gets The element value at the specified position in the collection];System.out.println (n); } }}// -// the// a// in
In the code above, a collection of int elements can be stored, followed by storing the value of the int type in the collection, and then implementing iterating over the collection Element. One thing to emphasize here is that the type of the Get method return value is the type of the element in the Collection.
Common methods supplement in a collection
The ArrayList collection provides some common methods, as follows:
boolean Add (int index, object Obj) //inserts The specified element, obj, into the specified position in the collection, object Remove (int index)// Deletes the element at the specified index from the collection, returns the element void clear () //empties all the elements in the collection object Set ( int index, Object Obj) //replaces The element at the specified position in the collection with the specified element, obj
ASCII encoding table
ASCII encoding table, English full name American Standard code for information interchange, United States standards Information Interchange Code.
ASCII encoding table origin
In a computer, all data is stored and computed using a binary number representation
a, b, c, D such 52 letters (including uppercase), as well as 0, 1 and other numbers also have some commonly used symbols, in the computer is also used to store the binary number to represent, specifically with which binary numbers to indicate which symbol, of course, everyone can contract their own set (this is called code).
If you want to communicate with each other without causing confusion, then everyone must use the same coding rules, so the United states, the standardization of the introduction of ASCII code, the uniform rules of the above-mentioned symbols with which binary number to Represent.
Chinese Code table
An important ASCII code correspondence between characters
Storage of type Char
Short: occupies two bytes, is signed data, the value range -32768-32767,char: two bytes, is unsigned data, the value range 0-65536. Data of type char is first converted to an int data type when it participates in the Operation.
Case code
packagearraylist;/*ASCII coded performance display characters Java data type, Char integer java data type, int int type and char data type convert char two bytes, int four bytes char to int type, type automatically prompt, char data type, will query the encoding table, get integer int into char type, cast, will query encoding table char store kanji, query Unicode encoding table char can be computed with int, prompt for int type, two bytes in memory char range is 0-65535, unsigned data type*/ public classAsciidemo { public Static voidmain (string[] Args) {Charc = ' a '; inti = C + 1; System.out.println (i); intj = 90; CharH = (Char) j; System.out.println (h); System.out.println ((Char) 6 ); Chark = ' You '; System.out.println (k); //Char m =-1; }}//98//Z////you're
Java Basic Learning Note Six Java basic syntax and ArrayList