Java Basics Teaching Hello world to object-oriented _java

Source: Internet
Author: User

Java is a fully object-oriented language. Java through the virtual machine operating mechanism to achieve the "cross-platform" concept. I would like to present a tutorial for beginners, I hope to be useful to everyone.

"Hello world!"

First look at a Helloworld.java program. This program prints a string of characters "Hello world!" on the screen:

Copy Code code as follows:

public class HelloWorld
{
public static void Main (string[] args)
{
System.out.println ("Hello world!");
}
}

Some basic features of Java are included in the program:

1. Class: The above program defines a class HelloWorld, the name of which is the same as the name of the. java file.
2. Method: The interior of a class defines a method main for that class.
3. Statement (statement): The true "print" function is implemented by a statement, namely: System.out.println ("Hello world!");

The following two points about the way Java is written:

The statement in 1.Java is to end with the same as C + +.
2. Use curly braces {} to consolidate statements to form a program block. Through the program block, we can know the scope of different parts of the program, such as where the class starts and where it ends.

Compiling and running

Java programs are compiled by compilers to execute. Under Linux or Mac, you can download and install the Java JDK.

Use Javac to compile. At the command line, enter the following statement compilation:

Copy Code code as follows:

$javac Helloworld.java

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

Run using Java commands. Java searches for the main method in the class and executes it.

Copy Code code as follows:

$java HelloWorld

Variable

Computer languages usually need to store data in memory, such as variables in the C language, and Java has similar variables. Both the Java and C languages are statically typed languages. Before you use a variable, you declare the type of the variable.

Variable (variable) occupies a certain amount of memory space. Different types of variables occupy different sizes. The variable types in Java are as follows:

Copy Code code as follows:

Name Store Size Case Value Comment

Byte 1byte 3 bytes

int 4bytes 3 integer

Short 2bytes 3 Small integer

Long 8bytes 3 Length 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, a variable needs to be declared (declare) before it can be used. In the declaration, I describe the type of the variable, giving it a special name so that it can be called in a later program. You can declare variables anywhere in the program.

Like what:

Copy 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
}
}

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

The concept of "variable" actually comes from a process-oriented programming language. In Java, the so-called variable is actually "basic type" (premitive type). We will be more in-depth in the class explanation.

The above procedure can also be seen in Java, available//leading annotations.

Array

There are arrays (array) in Java. Arrays contain multiple data of the same type. I declare an array of integers in the following way:

Copy Code code as follows:

Int[] A;

When you declare an array, the space required by the array is not actually assigned to the array. I can use new to create the space required for an array while declaring it:

Copy Code code as follows:

Int[] A = new int[100];

This creates an array that can hold 100 integers. The corresponding memory allocations are also complete.

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

Copy Code code as follows:

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


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

An array of other types is similar to an array of integers.

An expression

An expression is a combination of variables, constants, and operators that represents a data. 1 + 1 is a common expression. Another example:

Copy 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
}
}

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

Mathematical expressions

Mathematical operation, the result is a numerical

Copy Code code as follows:

1 + 2 Addition

4-3.4 Subtraction

7 * 1.5 Multiplication

3.5/7 Division

7% 2 to find the remainder

Relationship expressions

Determine if the expression is valid. A Boolean value that is true or false

Copy Code code as follows:

A > 4.2 is greater than

3.4 >= B is greater than or equal to

1.5 < 9 less than

6 <= 1 less than equal

2 = 2 equals

2!= 2 is not equal to

Boolean expression

The logical relationship of two Boolean values with, or, or not

Copy Code code as follows:

True && false and

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

!true not

Bit operations

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

Copy Code code as follows:

&

| 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:

Copy Code code as follows:

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

Control structure

The syntax for control structures in Java is similar to C. They all use {} to express the affiliation relationship.

Select (IF)

Copy Code code as follows:

if (Conditon1) {
statements;
...
}
else if (condition2) {
statements;
...
}
else {
statements;
...
}

The condition above is an expression that represents a true or false value. Statements is a statement.

Practice writing a Java program to determine whether 2013 is a leap year.

Loop (while)

Copy Code code as follows:

while (condition) {

statements;

}

Loop (do ... while)

Copy Code code as follows:

do {

statements;

while (condition); To pay attention to the end;



Loop (for)
Copy Code code as follows:

for (initial; condition update) {

statements;

}

Skip or jump out of the loop

In loops, you can use the

Copy Code code as follows:

Break Jump out of the loop

Continue Go straight to the next link.

Practice writing a Java program, counting from 1 plus 2, plus 3 ... The sum that has been added to 999

Select (switch)

Copy Code code as follows:

switch (expression) {

Case 1:

statements;

Break

Case 2:

statements;

Break

...

Default

statements;

Break

}

Object-oriented

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

1. Every thing in the world can be called an object, such as John. Objects have identities (identity), status (state), and Behavior (Behavior).
2. The state of the object is represented by the data member. Data members are also called domains (field). We use other objects as data members of this object. An integer that represents height, such as a nose.
3. The behavior of the object is represented by the member method. We are short for methods (method). An object can have multiple methods, such as breathing, sleeping.
4. Objects can be categorized (class) or grouped into 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).

Classes and objects

To define the syntax for a class:

Copy Code code as follows:

Class ClassName

{

Member1;

Member2;

...

}



We define a human class:

Copy Code code as follows:

Class Human
{
void Breath ()
{
System.out.println ("hu...hu ...");
}

int height;
}

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

1. Data member height is an integer type that can be used to store an integer.
2. The method represents the action that the object can perform, which is what the computer can do. Method can accept parameters and can return a value. In the definition of breath (), the breath () is the list of parameters in the following (). Breath () does not accept parameters because the argument list is empty. Void is the type of the return value before breath (), indicating that breath does not return a value.

(The method is similar to a function in a procedure language)

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

Copy Code code as follows:

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, you use the New keyword to create the object. Even objects from the same class, each object occupies different memory, that is, the identity of the object.

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

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.

Summarize

Java has many syntactic forms that are similar to C + +, but differ in detail and implementation, and need to be careful.
object, Class
Object: Method, field (data member)
Java is a fully object-oriented language.

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.