Java Second Week Learning summary

Source: Internet
Author: User
Tags array length comparable

Object-Oriented programming (Java) The second week of learning summary

experimental Time 2018-9-6

1, chapter III Summary of Knowledge points

first, the basic data type: divided into integral type(4), floating-point type (2), character type, Boolean, altogether 8 kinds
1. The integral type needs to be noted: the scope of the Java integer is not related to the machine running Java code, so it is very difficult to get the case of integer overflow, the principle is that each of Java data types of the value range is defined, can not be changed. Byte occupies 1 bytes, the range is 128 to 127;short of 2 bytes, the range is about 3W, int is 4 bytes, the range is about 2 billion, and long is 8 bytes, it should be noted that there is a suffix L. Java does not have an unsigned type.
2. Floating-point type: Float is 4 bytes, suffix f;double is 8 bytes; Java float is represented by a binary system, so it is not possible to accurately represent 1/10, so it is not suitable for high-precision computing systems that require no rounding errors, such as a financial system. The Double class has 3 constants: Double.positive_infinity means infinity, double.negative_infinity means infinity, Double.NaN represents not a number, but all non-numeric values in Java are considered different, so the method to compare Nan is: Double.isnan (Object o);
3. Character type: Describes a unit of code in Java with UTF-16 encoding. A code point is a number that can be used to encode a character set, UTF-32: A code point with 4 bytes and 32 bits, consuming too much resources; UTF-8: Encode Unicode code points with a sequence of one to four bytes, good resource savings, and good XML for Ajax returns. UTF-16: Encodes a Unicode code point using a sequence of one or two unassigned 16-bit code units; The \u represents the character escape number and can also be used for "and" outside.
4. Boolean: Boolean:false and True, it should be noted that: Java can not be 0 worth true,0 when false, direct conversion is not supported.


second, variable: keywordFinal represents a constant that, once assigned, cannot be changed, and the customary constant name is capitalized.


Third, Operation: floating-point number is0 In addition to the infinite or Nan, and the integer will produce an exception; the STRICTFP keyword can be used on a field, including a class, with a strict floating-point calculation in it;
1. Type conversions in operations: if one of the two numbers is of type double, the other is converted to a double type, and if one of the two numbers is of type float, the other is converted to float type, and if one of the two numbers is of type long, The other one is also converted to a long type; otherwise, two operands will be converted to the INT type. The Math class provides a number of static methods for operations
2. Shift operation:<< left, all operands moved to the left, one to the right with 0 supplement;>> with sign bit right: Along with the sign bit to the right, each move to the leftmost with the symbol bit complement;>>> unsigned right shift: Along with the sign bit to the right, Each shift one leftmost with 0 supplements. The right parameter requires a modulo 32-bit operation (long for 64-bit modulo operation). Note that the result of 1<<31 is negative: -2147483648,java is not 0 when the highest bit in 32 bit is 1 and the remainder is 0. It is a 32-bit minimum negative integer: 2147483648


Four, String: code point-codepoint.
1.String Common API:
char charAt (int index) returns the unit of code at the specified position.
int codepointat (int index) Returns a code point that starts or ends at a given position.
int offsetbycodepoints (int startindex,int cpcount) Returns the code point index, starting with the StartIndex code point, after the displacement cpcount.
int codepointcount (int startindex,int Endinex) returns the number of code points between the starting position and the ending position.
int CompareTo (string other) returns a negative number if the string is before other and returns a positive number if the string is behind other, or 0 if it is equal.
Boolean StartsWith (String startstr) returns True if the string starts with Startstr.
Boolean EndsWith (String endstr) returns True if the string is terminated with ENDSTR.
Boolean equals (Object other) 2 objects are equal and return true.
Boolean equalsignorecase (String other) 2 strings ignore case equality returns TRUE.
int indexOf (String str) | | (String str,int fromIndex) | | (int cp) | | (int cp,int fromIndex) is calculated from the FromIndex position, returning the string or position where the code point first appears.
int LastindexOF, but returns the last occurrence of a string.
int length () returns the string length, which is exactly the number of return code units.
String substring (int begin) | | (int begin,int end) to intercept the string.
String toLowerCase (); String touppercase (); Turns lowercase, and goes to uppercase, respectively.
string trim (); Delete the original string header and trailing spaces.
String Replace (string old,string new) replaces old with new, and no construction is good to use.
2.StringBuilder Common API: The same as the use of Class StringBuffer, just StringBuffer support multi-threading, the efficiency is slightly lower.


Five, input and output:
1. Scanner class: You can create the object with a given input stream, for example, with a standard input stream:
Scanner s=new Scanner (system.in), you can also construct a Scanner object with a file object, such as:
Scanner s=new Scanner (New File (String FilePath));
as a read class,Scanner mainly has the following methods: String nextline (); String next (); Int nextint ();
Double nextdouble ();
(Java6 has console class can read password is not visible: Console Cons=system.console (); Cons.readpassword (""))
some of the corresponding detectionHasnext method;
2. PrintWriter class: You can construct a PrintWriter object with a file object:
PrintWriter pw=new PrintWriter (New File (String FilePath));
as a class that is written out,The main methods of PrintWriter are: write (String str), print,println,printf, etc.


Vi.. Control process:The local variable in the Do statement block in the Do_while statement has no life cycle in the while (). The variables in the () in the For statement can be used in the {} statement block.
The case label of the switch statement must be an integer or an enumeration type constant, and if there is no break statement at the end of an event branch statement, then the next one is executed.
with a labelBreak statement, the label must be placed immediately before the outermost loop that you want to jump out, followed by a colon.


Seven, big value:BigInteger and BigDecimal respectively implement arbitrary-length shaping and arbitrary-length floating-point types. can use the valueof (long/double) method to construct them by using a basic type, the same way they do the following:
Add: addition; subtract: subtraction; multiply: multiplication; divide: Division; mod: the remainder; all two classes implement the comparable interface, so there is a way to compareto the comparison size.


Eight, array: inThe arrays class in Java 6 has a method object[] copyOf (Object oldarray[],int length) that static method copies the value of an array into a new array, the 2nd parameter specifies the length of the new array, and returns a new array, If the new array has extra elements, the numeric type is assigned a value of 0,boolean to false. If the length is less than the original array length, only the front data elements are copied. The above methods only have JDK6.0 version.
The System.arraycopy (from[],int fromindex,to[],int toindex,int Conut) method is also a copy of the array, The parameter meaning is that the element starting from the position of the array from is fromindex and the total number of copies is conut elements to the array to start from the Toindex position, the method needs to be aware that the array to must have enough space to hold the copied elements.
Math.ronud is rounded, math.random returns a floating-point number from 0 to 1 (without 0, 1), multiplied by this floating point, to get an immediate count between 0 and N-1.
arrays can be usedfor (:) This loop, arrays has a number of static methods to use:
ToString (type[]a): Converts an array to a string
COPYOF is available after 6.0, and the parameters can be 3 (type[]a,int Start,int end), and start-end represents the length to be copied.
Sort: Sorting of fast sorting algorithms.
BinarySearch (Type[]a,type v) | | (Type []a,int start,int End,type v) finds an element with a binary search method, returns the subscript value successfully, otherwise returns a negative number r,-r-1 is the position to be inserted in order to keep a, I probably estimated the next, since it is a 2-point search, Then the elements in this array should be implemented by the comparable this interface is ordered, this method is faster than its own to loop lookup, but to do a sort method, do not know whether the overall impact on efficiency.
Fill (Type[],type v) Sets the value of all elements of the array to V.
Equals (TYPE[]A,TYPE[]B) returns True if A, B is the same size, and the same elements are equal.
Java is actually not a multidimensional array, only an array of arrays, which makes Java very flexible can create arbitrary rules of the array.

2. Experiment contents and Steps

Experiment 1: write the Java application and output the values of the following 4 expressions.

int i=1;

Double d=1.0;

(1) 45+45*50%i--

(2) 1.5*3+d++

(3) (true) && (3>4)

(4) (i>0) | | (i<0)

Experiment 2: Write a Java application that contains the following code fragment , outputting the value of the string class object S3.

String s1= "hello!";

String s2= "World";

String s3=s1+s2;

Experiment 3: Change Experiment 2 s1,S2,S3 as StringBuilder class object, observe the program running results and compare with experiment 2 results, Understand the difference between a string class object and a StringBuilder class object.

Experiment 4: Run the following program in command-line mode to understand the use of Java Application command-line parameters.

public class Message

{

public static void Main (string[] args)

{

if (Args[0].equals ("-H")) System.out.print ("Hello");

else if (args[0].equals ("-G"); System.out.print ("Goodbye,");

for (int i=1;i<args.length;i++)

System.out.print ("+args[i");

SYSTEM.OUT.PRINTLN ("!");

}

}

Lab 5:Import Chapter 3 sample program Inputtest.java steps in Eclipse environment :

(1) new Java project such as:

(2) Select File->import->file ystem->next, open the File Import window like, click on the above browse Select Import source program and select, click Browse below After selecting the source program import location for the new project inputtest/src location, click Finish to complete the import.

(3) open the default package of the Inputtest Project Src folder, and double-click Inputtest.java to open the file in the IDE's source program editing area.

(4) right-click the Inputtest.java file name to open the shortcut menu, choose Run As->java application running the program, in conjunction with the program run results, understand the code Scanner Class object usage, mastering the Java Console input method.

Experiment 6 : follow The procedure of Experiment 5, Import Writereadfiletest.java sample program, understand program code with program running result , observe the contents of file MyFile.txt under project folder, master the input and output operation of file.

Experiment 7: Follow the procedure of experiment 5, import the 3rd Chapter sample program, each sample program summarizes the learning content from the syntax and algorithm two angles.

(1) test Retirement.java,retirement2.java,lotteryodds.java to master the cyclic control structure;

(2) test Bigintegertest.java, master the use of large numerical classes;

(3) test Lotterydrawing.java, master the use of arrays;

(4) test Compoundinterest.java, master the use of multidimensional arrays;

(5) Test Lotteryarray.java, master the use of irregular arrays.

Java Second Week Learning summary

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.