1. Simple Java language templates
package inclass1_1; import java.io.*; // import package (equivalent to the C language of #include<stdio.h>) public class Hellojava {// java each project can contain more than one class, But only one class public Span style= "COLOR: #0000ff" >static void Main (string[] args) { // display message "He Llo World "on the console. System.out.println ("Hello world!"); // console output statement }}
(1) where the public (keyword) is the permission modifier, there are three of the following, you can compare their list of differences:
| Access location |
Private |
Protected |
Public |
| This type of internal |
Visible |
Visible |
Visible |
| Other classes or subclasses of the same package |
Not visible |
Visible |
Visible |
| Other Packages |
Not visible |
Not visible |
Visible |
(2) Features of main function (Main method):
The ① master method must be static, with the static keyword, so the method must be static if other methods are called directly within the Main method.
② main function is void (no return value)
2.Java of standard input and output
1 Packageinclass1_1;2 ImportJava.io.*;3 ImportJava.util.Scanner;;4 Public classInOut {5 Public Static voidMain (string[] args) {6Scanner in =NewScanner (system.in);//enter data from the console multiple times with just one write7 intA=in.nextint ();//The value of reading data is assigned to a variable, and the combination of the previous sentence can realize the function of C language scanf.8 intb=in.nextint ();9System.out.printf ("%s", "The result is");TenSystem.out.println (A *b); One } A}
This program gives the Java program to implement the C language standard input and output functions. The input function differs according to the type of the previous variable nextint,nextdouble.
There are several ways to output in the console, which are common:
1:system.out.printf ("%s", "The result is");
This is similar to the C-language printf use method, first format the string, then a number of parameters, note that the output is not automatically followed by a newline character
2:system.out.println (A*B);
This default expression is inside a string and can be concatenated with a + sign between different strings. Note that the line break is added by default after each output is finished.
There are some other forms of output that can be consulted on the Internet.
3.this keywords
Public class setName (String name) { this. name=name; }
Java specifies that this is used as a reference to this class of objects, which refers to an object of this class, in a local variable (referring to a variable defined within a member method)
Or if the method parameter overrides the member variable of the class (when the name is the same name), add this.
4. Naming and knowledge of basic operations (data types)
(1) Constant naming
Final datatype constantname = value; Final Double pi=3.14159
(2) Naming habits:
① class Name: Capitalize the first letter of each word. Eg.computerarea
② constants to capitalize all the letters;
③ variables and methods: The first letter is lowercase, and then the first letter of each word is capitalized. eg. Eggnum
(3) Operation: Power operation Math.pow (2,3) 2*2*2;
Force type Conversion double (1.7=1[), (int) Note that the fractional part is dropped directly instead of rounded]. Use 3.0/2.0=1.5 this notation.
5. Errors in this assignment and in the practice process:
(1) syntax error:
Missing closing parenthesis;
Missing semicolon (Each statement ends with a plus, but the end of the class does not need to be added)
Missing quotes (strings are enclosed in double quotes, single characters are enclosed in single quotes)
(2) Java is case sensitive, note system common class string to capitalize the first letter, class is strictly lowercase.
Java Learning Notes (First Class)