201771010110 Kongwi "Object-oriented Programming (Java)" Second week study summary

Source: Internet
Author: User
Tags arithmetic operators bitwise operators logical operators mathematical functions

Theoretical knowledge content :

1. Identifiers:

Identifiers consist of letters, underscores, dollar signs, and numbers, and the first symbol cannot be a number.

Legal identifiers: Hello, $1234, program name, www_123

Identifiers can be used as: class name, variable name, method name, array name, file name, and so on.

2. Keywords:

Keywords are words that have been given a particular meaning in the Java language.

Common: Class, public, try, catch, if, float, import, void, and so on.

Keyword does not make a variable name.

3. Notes:

There are three ways to annotate Java:

A.//

Note the content is from//until the end of this line.

B./* and */

Defines a comment block.

C./** start, */End

This annotation method can be used to automatically generate documents.

4. Data type:

(1) Integral type: Int,short,long,byte

(2) floating-point type: float,double

(3) Character type: Char

(4) Boolean type: Boolean

The range of integers in Java is independent of the machine running Java code, and there is no unsigned type in java.

5. Variables

Each variable in Java belongs to a type. When declaring a variable, the type to which the variable belongs is before the variable name.

A row can declare multiple variables. Declaring each variable individually can improve the readability of the program.

You cannot declare two variables of the same name within the same scope.

Once a variable is declared, it must be explicitly initialized with an assignment statement and cannot use the value of an uninitialized variable.

Although variable declarations can be made anywhere in the code, in order to ensure a good programming style, the declaration of the variable is as close as possible to where the variable was first used.

In Java, the keyword final is used to indicate constants (constant names are used in uppercase). The keyword final indicates that the variable can be assigned only once, and its value cannot be changed once it is set.

In Java, you often want a constant to be used in multiple methods within a class, often referred to as class constants. You can declare a class constant by using the keyword Staticfinal (class constants).

6. Operators

There are arithmetic operators, self-increment operators and decrement operators, relational operators, logical operators, bitwise operators

Where Java provides two special operators: new (used to create objects); Instanceof (Returns a Boolean value that indicates whether an object is a specific class or an instance of its subclass)

7. Mathematical Functions and constants

Mathematical functions are included in the Math Class (power function, trigonometric functions, Gong and its inverse functions, etc.)

Java also provides two constants: Math.PI and MATH.E

If you do not want to prefix the math method name and the constant name with "math.", add: Import static java.lang.math.* at the top of the source file;

8. Type Conversion

A. Implicit type conversions

Most numeric conversions are done automatically by following the precedence relationship.

If one of the two operands is of type double, the other is converted to a double type.

Otherwise, if one of the two operands is of type float, the other will be converted to the float type.

Otherwise, if one of the two operands is of type long, the other will be converted to a long type.

Otherwise, the two operands are converted to the int type.

B. Forcing type conversions

(target type) variable name

9. String

A Java string is a sequence of Unicode characters that is the basic data structure for organizing characters, similar to a character array. There is a built-in string type, but a Java-predefined class string is provided in the standard Java class library.

The string that needs to be used in the program can be divided into two categories: the immutable string class that will not be modified and changed after creation, and the build string StringBuilder class that allows for changes and changes after creation.

In Java, strings are treated as objects.

The basic operation of the String class: (1) Gets the substring of the string, (2) string connection (+), (3) The length of the string, (4) whether the string is equal, (5) string retrieval, (6) The conversion of the string to the value, and (7) Gets the string representation of the object.

10. Input and output

(1) Read input: When entering through the console, a scanner object needs to be constructed and associated with the "standard input stream" system.in.

(2) Formatted output: Using System.out.print (x) to output the value x to the console, this command will print out X for the maximum number of non-0 digits allowed by the data type x corresponds to.

(3) file input and output: to read a file, you need a scanner object constructed with a file object. To write to a file, you need to construct a PrintWriter object, in the constructor, simply provide the file name: Printwriterout = new PrintWriter ("MyFile.txt");

Experiment two:Java Basic program Design

1, the purpose and requirements of the experiment

1) be familiar with the basic steps of Java program development in two ways of command line and IDE;

2) Master The process of importing Java source program under Eclipse integrated development environment;

3) Master the Basic syntax of the Java language Construction Basic program data types, variables, operators, various types of expressions, input and output, flow control;

4) Master The use of the string class, the StringBuilder class, and the array class.

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)

The results are as follows:

                

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;

The results are as follows:

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 the string class object and the StringBuilder class object.

The code is as follows;

 Package experiment Three;  Public class experiment Three {    publicstaticvoid  main (string[] args) {        StringBuilder s1 =new StringBuilder ("Hello,");        StringBuilder s2=new StringBuilder ("world!" );        S1.append (S2);        System.out.println (S1);    }}

The results are as follows:

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 ( "!"  

The results are as follows:

Experiment 5:eclipse Environment to import the 3rd chapter of the example program Inputtest.java steps:

(1) New Java Project:

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

(3) Open the default package for the Inputtest Project Src folder and double-click inputtest.java to open the file in the IDE's source editing area. Right-click the inputtest.java file name to open the shortcut menu, choose Run As->java application running the program, combined with the program run results, Understand the scanner class object usage in your code, and master 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 Chapter 3 Example program, each sample program summarizes the learning content from the syntax, algorithm two angles.

(1) test Retirement.java,Retirement2.java,Lotteryodds.java GRASP the structure of cyclic control;

(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.

3. Experiment Summary: Through this study, I learned the basic Java language Program structure, and we have learned C language similar, but also different. Experiment one, two or three, let me understand the use of the string class, StringBuilder class, experiment four let me understand the variable. The subsequent experiments also allowed me to master the methods of importing existing code in the Eclipse Environment ,scanner class objects, arrays, and the input and output operations of the files of the loop control structure, as well as the use of some grammatical structures. Compared to the first experiment, I was able to use eclipse more skillfully and run the program using the command-line approach. However, due to the previous knowledge of C language forgotten, for multidimensional arrays, the use of irregular arrays has yet to be more profound learning. Java is a need for independent learning and practice of the combination of language courses, in the future I will be more efforts to learn this course seriously.

201771010110 Kongwi "Object-oriented Programming (Java)" Second week study 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.