public class animal{
private String name;
public string GetName () {///string means the GetName () method returns a string
return name;
}
public void SetName (int newName) {//void means that the SetName () method does not return a value
name = NewName;
}
}
The signature of a method consists of a method name and a parameter list, excluding the return value type and the access modifier
public int numbervisitors (int month)//return type int, parameter named month, int type
Comments
Single-line Comment
/* Multiple lines
* Notes
*/
/**
* Javadoc Multi-line Comment
* @author HR and Scott
*/
Class and file: The file name (*.java) must be the same as the public class name, including case, and the file extension must be. java
The Main method is the entry for the Java program execution.
Fixed notation for the main method:
Public statc void Main (string[] args) {}
Sample program:
public class zoo{
public static void Main (string[] args) {
}
}
Public indicates that the method can be called anywhere in the program
Static means that the method can be called directly from the class name, such as Zoo.main ()
void represents the return type. A method that does not return a value silently returns control to its caller
String[] args: Indicates that the parameter of the main method is an array of type string, with the parameter named args. can be written in other forms: string args[] or string ... args
Package and Import://Refer to the Java API CHM documentation
Classes are grouped by package
The import statement tells Java which package to find the appropriate class
As a best practice, separate the Java source files from the class files and use the-D option at compile time to set the location where the compiled class files are generated:
javac-d./classes Zoo.java//command is executed successfully, the Zoo.class file will be placed under the Classes subfolder under the current working folder
Using wildcard characters when importing a class *
Import java.util.*; Import all classes in the Java.util package
public class importexample{
public static void Main (string[] args) {
Random r = new Random (); Although all classes are imported, the compiler will only pick out the classes that are really needed
System.out.println (R.nextint (10));
}
}
Either just import the classes we actually need to write the code, or use wildcards to import all the classes, which make the code easier to read, and the latter to streamline the import checklist
Everything in the Java.lang package is automatically imported without using an import statement to explicitly import
Only classes and static members can be imported at import time
Note When importing packages, avoid redundant imports:
Import Java.lanag.System; The row is redundant
Import java.lang.*; The row is redundant
Import Java.util.Random;
Import java.util.*; The row is redundant
public class importexample{
public static void Main (string[] args) {
Random r = new Random ();
System.out.println (R.nextint (10));
}
}
Class name conflict:
Import Java.sql.Date; Importing the specified analogy uses a wildcard * with a high precedence, regardless of where the statement is located
Import java.util.*; Cannot use import java.util.Date, or compiler Error
public class conflicts{
Date date;
Some more code
}
Compile the code with the package name:
C:\temp\packagea\ClassA.java
Package Packagea;
public class ClassA {
}
C:\temp\packageb\ClassB.java
Package Packageb;
Import Packagea. ClassA;
public class ClassB {
public static void Main (string[] args) {
ClassA A;
System.out.println ("Got it");
}
}
Compile (assuming the current directory is C:\temp):
Javac Packagea\classa.java Packageb\classb.java
Run:
Java Packageb. ClassB
To create an object:
Construction Method (constructors)
Random r = new Random ();
A reference variable for the random type is created on the left side of the equals sign R
New Random () creates an object
Examples of programs:
public class chick{
Public chick () {
System.out.println ("in constructor");
}
}
Remember two points:
1. The construction method name is consistent with the class name;
2. The construction method does not have a return type.
The function of the constructor method is to initialize the member variables
public class chicken{
int Numeggs = 0; Declaring and initializing member variables at the same time
String name;
Public Chicken () {
Name = "Duke"; Initializing member variables in a construction method
}
}
Break and Continue
Break does not have a label, the default behavior is to jump out of the inner layer of the loop
Continue without tags, the default behavior is to skip the inner layer of this cycle
NULL pointers and Null values
Object String
Java Learning Note 1