Java is a completely object-oriented language. Java realizes the concept of "cross-platform" through the running mechanism of the virtual machine. Here I want to present a beginner-friendly tutorial that I hope will be useful to everyone.
"Hello World!"
Let’s first look at a HelloWorld.java program. This program prints a string of characters "Hello World!" on the screen:
Copy the code code as follows:
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
The program includes some basic features of Java:
1. Class: The above program defines a class HelloWorld, and the name of the class is the same as the name of the .java file.
2. Method: A method main of the class is defined inside the class.
3. Statement: The real "printing" function is implemented by a statement, namely: System.out.println("Hello World!");
The following two points are related to the way Java is written:
1. Statements in Java should end with; (same as C/C++).
2. Use curly braces {} to integrate statements to form program blocks. Through program blocks, we can know the scope of different parts of the program, such as where the class starts and where it ends.
Compile and run
Java programs must be compiled by a compiler before they can be executed. Under Linux or Mac, you can download and install Java JDK.
Use javac to compile. Enter the following statement on the command line to compile:
Copy the code code as follows:
$javac HelloWorld.java
Under the current path, a file named HelloWorld.class will be generated.
Use the java command to run. Java will search for the main method in the class and execute it.
Copy the code code as follows:
$java HelloWorld
variable
Computer languages usually need to store data in memory, such as variables in C language, and Java has similar variables. Both Java and C are statically typed languages. Before using a variable, declare its type.
Variables occupy a certain amount of memory space. Different types of variables occupy different sizes. Variable types in Java are as follows:
Copy the code code as follows:
Name storage size example value annotation
byte 1byte 3 byte
int 4bytes 3 integer
short 2bytes 3 short integer
long 8bytes 3 long integer
float 4bytes 1.2 single precision floating point number
double 8bytes 1.2 double precision floating point number
char 2bytes 'a' character
boolean 1bit true Boolean value
In Java, variables need to be declared before they can be used. In the declaration, I state the type of the variable and give it a special name so that I can call it later in the program. You can declare variables anywhere in your program.
for example:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
System.out.println("Declare in the middle:");
int a;
a = 5;
System.out.println(a); // print an integer
}
}
A above is the variable name. You can assign a value to the variable while declaring it, such as int a = 5;
*** The concept of "variables" actually comes from procedural programming languages. In Java, so-called variables are actually "primitive types". We will go into more depth in the explanation of classes.
You can also see from the above program that in Java, // can be used to lead comments.
array
There are arrays in Java. Arrays contain multiple data of the same type. I use the following method to declare an integer array:
Copy the code code as follows:
int[] a;
The space required by the array is not actually allocated to the array when it is declared. I can use new to create the space required for the array at the same time of declaration:
Copy the code code as follows:
int[] a = new int[100];
Here we create an array that can hold 100 integers. The corresponding memory allocation is also completed.
I can also assign values to the array at the same time as the declaration. The size of the array is also determined at the same time.
Copy the code code as follows:
int[] a = new int[] {1, 3, 5, 7, 9};
Use int[i] to call the i-indexed element of the array. i starts from 0.
Other types of arrays are similar to integer arrays.
expression
An expression is a combination of variables, constants, and operators that represents a piece of data. 1 + 1 is a common expression. Another example:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
System.out.println("Declare in the middle:");
int a;
a = 5 + 1;
System.out.println(a); // print an integer
}
}
5 + 1 above is also an expression, equal to 6.
mathematical expression
Mathematical operation, the result is a numerical value. The copy code is as follows:
1 + 2 addition
4 - 3.4 Subtraction
7 * 1.5 multiplication
3.5 / 7 division
7 % 2 Find the remainder
relational expression
Determine whether the expression is true. That is, a boolean value, true or false. The copy code code is as follows:
a > 4.2 is greater than
3.4 >= b greater than or equal to
1.5 < 9 less than
6 <= 1 less than or equal to
2 == 2 is equal to
2 != 2 is not equal to
boolean expression
The logical relationship between AND, OR and NOT of two boolean values is as follows:
true && false and
(3 > 1) || (2 == 1) or
!true not
Bit operations
Perform logical operations on the binary form of the integer bit by bit to obtain an integer. The copy code is as follows:
& and
| or
^ xor
~ not
5 << 3 0b101 left shift 3 bits
6 >> 1 0b110 right shift 1 bit
There are also the following common operators in C, which I will explain further when they are used:
Copy the code code as follows:
m++ adds 1 to variable m
n-- variable n minus 1
condition ? x1 : x2 condition is a boolean value. According to condition, take the value of x1 or x2
control structure
The syntax of control structures (control flow) in Java is similar to C. They all use {} to express affiliation.
select(if)
Copy the code code as follows:
if (condition1) {
statements;
...
}
else if (condition2) {
statements;
...
}
else {
statements;
...
}
The condition above is an expression that represents a true or false value. statements; are statements.
Practice writing a Java program to determine whether 2013 is a leap year.
loop(while)
Copy the code code as follows:
while (condition) {
statements;
}
Loop (do... while)
Copy the code code as follows:
do {
statements;
} while(condition); // Pay attention to the ending;
Loop (for)
Copy the code code as follows:
for (initial; condition; update) {
statements;
}
Skip or break out of a loop
In the loop, you can use the copied code code as follows:
break; // Break out of the loop
continue; // Go directly to the next loop
Practice writing a Java program to calculate the total from 1 to 2, to 3... and then to 999.
select(switch)
Copy the code code as follows:
switch(expression) {
case 1:
statements;
break;
case 2:
statements;
break;
...
default:
statements;
break;
}
object-oriented
"Objects" are a way for computers to abstract the world. "Object-oriented" can be expressed in many ways. The following is an imprecise, but more intuitive way of understanding:
1. Everything in the world can be called an object, such as Zhang San. Objects have identity, state and behavior.
2. The state of the object is represented by data members. Data members are also called fields. We use other objects as data members of this object. For example, an integer representing height, such as a nose.
3. The behavior of an object is represented by member methods. We call it method for short. An object can have multiple methods, such as breathing and sleeping.
4. Objects can be classified (class), or classified into the same type (type). Objects of the same type have the same methods and data members of the same type. An object of a certain type is called an instance of that type.
Classes and Objects
Syntax for defining classes:
Copy the code code as follows:
class ClassName
{
member1;
member2;
...
}
We define a human class:
Copy the code code as follows:
classHuman
{
void breath()
{
System.out.println("hu...hu...");
}
int height;
}
In the scope of {}, the Human class has two members: a data member height and a method breath().
1. The data member height is an integer type and can be used to store an integer.
2. Methods represent the actions that the object can perform, that is, the operations that the computer can perform. Methods can accept parameters and return values. In the definition of breath(), the () after breath is the parameter list. Since the parameter list is empty, breath() accepts no parameters. The void before breath() is the type of return value, indicating that breath does not return a value.
(The method is similar to functions in procedural languages)
Now, we create the object aPerson and call the object's method breath:
Copy the code code as follows:
public class Test
{
public static void main(String[] args)
{
Human aPerson = new Human();
aPerson.breath();
System.out.println(aPerson.height);
}
}
classHuman
{
void breath()
{
System.out.println("hu...hu...");
}
int height;
}
In the main method, use the new keyword to create the object. Even for objects from the same class, the memory occupied by each object is different, that is, the identity of the object is also different.
Human aPerson declares that the aPerson object belongs to the Human class, which indicates the type of the object.
After the object is created, we can use object.data members to reference data members and use object.method() to call methods. As we print aPerson.height later.
Summarize
Many syntax forms of Java are similar to C/C++, but there are differences in details and specific implementation, so you need to be careful.
Object, class object: methods, fields (data members)
Java is a completely object-oriented language.