Java Foundation------from HelloWorld to object-oriented

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. I want to present a tutorial for beginners, I hope it will 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:

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!");

The following two points about how Java is written:

    • The statements in Java are to be terminated with the same as C + +.
    • Use curly braces {} to consolidate statements and form blocks. Through the 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 need to be compiled by the compiler to execute. Under Linux or Mac, you can download and install the Java JDK.

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 will search for the main method in the class and execute it.

$java HelloWorld

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. In the declaration, I describe the type of the variable, giving the variable a special name so that it is called in a later program. You can declare variables anywhere in the program. Like what:

public class test{public    static void Main (string[] args)    {        System.out.println ("Declare in the Middle:"); C4/>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 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). We will be more in-depth in the class explanation.

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

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.

An expression

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

public class test{public    static void Main (string[] args)    {        System.out.println ("Declare in the Middle:"); C4/>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 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

Bollinger Bands 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

Control structure

The syntax for control flow in Java is similar to C. They all use {} to express their affiliation.

Select (IF)

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

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

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

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 loop

In the loop, you can use the

Break ; Jump out of the loop

continue; Go directly to the next ring

Practice writing a Java program, calculated from 1 plus 2, plus 3 ... Always add to the sum of 999

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).

Classes and objects

To define the syntax for a class:

Class ClassName

{

Member1;

Member2;

...

}

We define a human class:

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.

Summarize

Many of the syntax forms of Java are similar to C + +, but there are differences in detail and implementation that require caution.

Object, Class

Objects: Methods, fields (data members)

Java is a fully object-oriented language.

Java Foundation------from HelloWorld to object-oriented

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.