Java Foundation 01 from HelloWorld to Object-oriented (reproduced)

Source: Internet
Author: User

Java is a fully object-oriented language. Java realizes the concept of "cross-platform" through the operation mechanism of virtual machine. "Hello world!" public class helloworld{
public static void Main (string[] args) {
System.out.println ("Hello world!");
}
}

Some basic features of Java are included in the program:

    • Class: The above program defines a class HelloWorld whose name is the same as the name of the. java file.
    • Method: The inside of the class defines a method of the class main.
    • Statement (statement): The true "print" function is implemented by a statement, namely: System.out.println ("Hello world!");
Compiling and running

Use Javac to compile. On the command line, enter the following statement to compile: $javac Helloworld.java

Under the current path, a file named Helloworld.class will be generated.

Use Java commands to run: $java HelloWorld. Java will search for the main method in the class and execute it.

Variable computer languages often need to store data in memory, such as variables in C, and Java has similar variables. Both the Java and C languages are static types of languages. Declare the type of the variable before you use it. Variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes. The types of variables in Java are as follows: Storage size Sample Value comment
Byte 1byte 3 bytes
int 4bytes 3 integer
Short 2bytes 3 Shorter integer
Long 8bytes 3 Length integer
Float 4bytes 1.2 Single-precision floating point number
Double 8bytes 1.2 Dual precision floating point number
Char 2bytes ' a ' character
Boolean 1bit True Boolean value in Java, a variable needs to be declared (declare) before it can be used. public class test{
public static void Main (string[] args) {
System.out.println ("Declare in the Middle:");
int A;
A = 5;
System.out.println (a);
}
}

Above A is the variable name. You can assign a value to a variable, such as int a = 5, while declaring a variable;

The concept of "variable" actually comes from a process-oriented programming language. In Java, the so-called variable is actually a "basic type" (premitive type). In Java, annotations are available//LED.

Array

There is an array in Java. The array contains multiple data of the same type. I declare an array of integers in the following way:

Int[] A;

When declaring an array, the space required for the array is not really assigned to the array. I can use new to create the required space for the array at the same time as the declaration:

Int[] A = new int[100];

This creates an array that can hold 100 integers. The corresponding memory allocation is also complete.

I can also assign values to the array at the same time as the declaration. The size of the array is also determined.

Int[] A = new int[] {1, 3, 5, 7, 9};

Use Int[i] to invoke the I subscript element of an array. I start from 0.

Other types of arrays are similar to integer arrays.

Expression expressions are a combination of variables, constants, and operators that represent a single data type. 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);
}
}

The above 5 + 1 is also an expression, equal to 6.

Mathematical expressions

Mathematical operation, the result is a numeric value

1 + 2 Additions

4-3.4 Subtraction

7 * 1.5 Multiplication

3.5/7 Division

7% 2 Seek remainder

Relational expressions

Determines whether an expression is true. A Boolean value that is true or false

A > 4.2 greater than

3.4 >= B is greater than or equal to

1.5 < 9 less than

6 <= 1 is less than or equal to

2 = = 2 equals

2! = 2 does not equal

Boolean expression

Logical relationship of two Boolean values with, or, and not

True && false and

(3 > 1) | | (2 = = 1) or

!true not

Bit arithmetic

A bitwise logical operation of the binary form of an integer to get an integer

& and

| Or

^ xor

~ Not

5 << 3 0b101 left shift 3 bits

6 >> 1 0b110 right shift 1 bit

There are also the following operators that are common in C, which I will explain further when I use them:

m++ variable m plus 1

n--variable n minus 1

Condition? X1:x2 condition is a Boolean value. According to condition, take the value of X1 or x2

The syntax for controlling the control flow in control structure Java is similar to C. They all use {} to express their affiliation. Select (IF)if (condition1) {
statements;
...
}else if (condition2) {
statements;
...
}else{
statements;
...
The condition above is an expression that represents a true or false value. statements; a statement. Loop (while) While (condition) { statements; } Loop (do ... while) Do { statements; } while (condition); Pay attention to the end; Loop (for) for (initial; condition; update) { statements; } skipping or jumping out of a loopIn the loop, you can use the Break ;Jump out of the loop continue;Go directly to the next ring Select (switch) switch (expression) { Case 1: statements; Break ; Case 2: statements; Break ;      ... Default: statements; Break ; } Object oriented

An "Object" is a way for a computer to abstract the world. "Object-oriented" can be expressed in many ways. Here is an inexact, but more intuitive way to understand:

    1. Every thing in the world can be called an object, such as Zhang San. The object has identity, state, and Behavior (Behavior).
    2. The state of the object is represented by the data member (member). Data members are also called domain (field). We use other objects as data members for that object. For example, an integer that represents height, such as a nose.
    3. The behavior of the object is represented by the Member method (member). We are referred to simply as methods (method). An object can have multiple methods, such as breathing, sleeping.
    4. objects can be categorized (class) or classified as the same type. Objects of the same type have the same method and have data members of the same type. An object of a type is called an instance of that type (instance)
Class human{
void Breath () {
System.out.println ("Hu.. Hu.. ");
}
int height;
}

Within the {} range, the human class has two members: a data member height, a method breath ().

    • Data member height is an integer type that can be used to store an integer.
    • Methods represent the actions that an object can perform, that is, what the computer can do. The method can accept parameters and can return a value. In the definition of breath (), the parameter list is in the () after breath. Because the argument list is empty, breath () does not accept parameters. void before breath () is the type of the return value, stating that breath does not return a value.
    • (The method is similar to a function in a process-oriented language)

Now, we create the object Aperson and call the object's method breath:

public class test{
public static void Main (string[] args) {
Human Aperson = new Human ();
Aperson.breath ();
System.out.println (Aperson.height);
}
}
Class human{
void Breath () {
System.out.println ("Hu.. Hu.. ");
}
int height;
}

In the main method, create the object using the New keyword. Even objects from the same class occupy different kinds of memory, that is, the identity of the object is not the same.

Human Aperson declares that the Aperson object belongs to the Human class, which describes the type of the object.

After the object is established, we can use the object. Data members to refer to the data members, using the object. Method () to invoke the method. As we print aperson.height in the back

Related Article

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.