Java basics 01 from HelloWorld to object-oriented

Source: Internet
Author: User

Java is a fully object-oriented language. Through the operating mechanism of virtual machines, it creates a "cross-platform" concept, attracting many programmers to join the Java camp. In recent years, Android Development has contributed a lot to the popularity of Java. Here I want to present a suitable tutorial for beginners.

 

"Hello World! "
Let's first look at a HelloWorld. java program:


Public class HelloWorld
{
Public static void main (String [] args)
{
System. out. println ("Hello World! ");
}
} The above program defines a class (class) HelloWorld, and then defines a method (method) main for this class. The main method has a statement:

System. out. println ("Hello World! ");

Its function is to output the "Hello World!" string to the terminal! ". Note that the statements in Java should end with; (the same as C/C ++ ).

 

Use javac to compile:

$ Javac HelloWorld. java

Generate the HelloWorld. class file in the current path.

 

Run the command in java. The parameter following java is the class name (HelloWorld) with the same name as the file ). Java searches for the main method in the class and runs it.

$ Java HelloWorld

 

Variable
Variable occupies a certain amount of space in the memory and stores a certain type of value in the space. The variables in Java are as follows:

Storage size example value comment

Byte 1 byte 3 bytes

Int 4 bytes 3 integer


Short 2 bytes 3 short integer

Long 8 bytes 3 long integer

Float 4 bytes 1.2 Single-precision floating point number


Double 8 bytes 1.2 double-precision floating point number


Char 2 bytes 'A' character

Boolean 1bit true boolean Value

In Java, variables must be declared (declare) before they can be used. You can declare variables anywhere in the program. For example:

 

Public class Test
{
Public static void main (String [] args)
{
System. out. println ("Declare in the middle :");
Int;
A = 5;
System. out. println (a); // print an integer
}
} In Java, you can use // to lead the annotation. You can assign values to variables while declaring them, for example, int a = 5;

The concept of "variable" actually comes from process-oriented programming. In Java, the so-called variables are actually "premitive type ). We will go into more details in the class explanation.

 

Java contains arrays ). The array contains multiple values of the same type. We use the following method to declare an integer array:

Int [];

Note the difference between this writing method and the C array.

When declaring an array, the space required by the array is not actually allocated to the array. While declaring, we can use new to create the space required for the array:

Int [] a = new int [100];

You can also assign values to the array while declaring it (the size of the array is also determined ).

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

We use int [I] To Call The I subscript 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. It is equal to a new value. 1 + 1 is a common expression. For example:


Public class Test
{
Public static void main (String [] args)
{
System. out. println ("Declare in the middle :");
Int;
A = 5 + 1;
System. out. println (a); // print an integer
}
}

A mathematical expression that returns a number.


1 + 2 Addition

4-3.4 Subtraction

7*1.5 Multiplication

Division of 7/3.5


7% 2 Calculate the remainder

 

Relational Expression, returns a boolean Value

A> 4.2 or more

3.4> = B greater than or equal

Less than 1.5 <9

6 <= 1 is less than or equal

2 = 2 equals

2! = 2 not equal

 

Returns a boolean value.

True & false and

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


! True not

 

Bitwise operation: Performs logical operations on the binary form of an integer, and returns 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 common operators in C, which I will further explain when using:

M ++ variable m plus 1

N -- Variable n minus 1

Condition? X1: x2 condition is a boolean value. Returns the value of x1 or x2 Based on the condition.

 

Control Structure
The control flow syntax in Java is similar to that in C. Both use {} to express the affiliation.

 

Select (if)

If (conditon1 ){

Statements;

}

Else if (condition2 ){

Statements;

}

Else {

Statements;

}

 

Loop (while)


While (condition ){

Statements;

}

 

Loop (do... while)

Do {

Statements;

} While (condition); // pay attention to the ending;


 

Loop ()

For (initial; condition; update ){

Statements;

}

You can use

Break; // skip Loop

Continue; // directly go to the next link

 

Select (switch)

Switch (expression ){

Case 1:

Statements;

Break;

Case 2:

Statements;

Break;

...

Default:

Statements;

Break;

}

 

Object-oriented
"Object" is a way for computers to abstract the world. "Object-Oriented" can be understood in multiple ways. below is my understanding:

1. Every thing in the world can be called an object, such as Michael Jacob. Objects include Identity, State, and Behavior ).


2. The object State is represented by data member. A data member is also called a field ). We use other objects as data members of this object. For example, an integer indicating the height, such as a nose.

3. The behavior of an object is represented by the member method. This is short for method ). An object can have multiple methods, such as breathing and sleeping.

4. objects can be categorized into the same type ). Objects of the same type have the same method and data members of the same type. An object of a certain type is called an instance of this type ).

 

 

 

Class and Object

 

 

Syntax of the definition class:

Class ClassName


{

Member1;


Member2;

...

}

 

We define a human class:

 

Class Human
{
Void breath ()
{
System. out. println ("hu ...");
}
Int height;
} Within the range of {}, the Human class has two members: a data member height and a method breath ().

Methods represent the actions that an object can perform, that is, the operations that a computer can perform. Methods in OOP correspond to process-oriented functions. The method can accept parameters and return values.

The data member is an int base type object height.

Similar to C functions. In the breath () definition, () after breath is the parameter list. Because the parameter list is empty, breath () does not accept the parameter. The void before breath () is the type of the returned value, indicating that breath does not return the value.

 

Now, we create the object aPerson and call the method breath of the object:


Public class Test
{
Public static void main (String [] args)
{
Human aPerson = new Human ();
APerson. breath (); System. out. println (height );
}

}

Class Human
{
Void breath ()
{
System. out. println ("hu ...");
}

Int height;
} In the main method, we use the new keyword to allocate the required memory for the object. Objects occupy different memory, so their identities are different.

Human aPerson declares the type of aPerson.

We use object. Data members to reference data members and call methods using object. Method.

 

Summary
Many Java syntax formats are similar to those of C/C ++, but they differ in details and implementations. Be careful.

Object, Class

Object: method, field (data member)

Java is a fully object-oriented language.

 

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.