Getting Started with Java (ii)--it's all about being punished ...

Source: Internet
Author: User

Broken more than one months, reading immediately fell from the 100+ to 10-, although not very important this, after all, just when this is their own study notes, but some people see, someone commented, some people agree and criticize the feeling is very Barth, especially in the past has been deprived, miserable XI.

Write it well.

Now carry out the "business", one is PHP, one is Android development.

The former is biased toward three directions, summarizing before (reading, knocking code implementation), e-commerce website development, some skills of the realization;

The latter start stage, one side of Java learning, while doing things out, to win a week within a confessed bar first.

Today is still a little bit, the uninstall yesterday, as in the virtual interface, with the friend recommended Genymotion Bar, at least someone with, this is better than PHP (tears Ben

Time is always a problem, we have to cut the revenue and expenditure, refueling!

1. Getting Started with Java (ii)

Second, constant and variable

2.1 Keywords in Java

Some words in the Java language that have special uses are called keywords. The keyword on the Java compiler has a special meaning, in the application of the program must be cautious Oh!!

Common keywords in Java:

Q: So many, can't remember Ah ... -_-||

A: Now you do not need to remember all, mix a familiar , in the course of learning, you will gradually find the feeling and remember them

Yes, almost forget the big thing, the Java keyword is case-sensitive Oh! So void is the keyword, but void is not the ~ ~

PS: I can turn off the sensitivity of the case in as, in fact I do not recommend (personal feeling only

2.2 Understanding Java Identifiers

Q: Is the identifier God horse?

A: Identifiers are symbols that are used to name variables, classes, methods, and so on in a Java program.

There are several rules to follow when using identifiers:

1. Identifiers can consist of letters, numbers, underscores (_), dollar symbols ($) , but cannot contain other special characters, such as @,%, space, etc. , and cannot begin with a number . For example: 123name is not legal drop

2. Identifiers cannot be Java keywords and reserved words (Java reserved keywords, which may be used as keywords in later versions of the upgrade), but can contain keywords and reserved words. For example, void may not be used as an identifier, but myvoid can

3. Identifiers are strictly case-sensitive . So nirvana, be sure to distinguish between Chu Imooc and Imooc is two different identifiers Oh!

4. The name of the identifier should best reflect its role, to be known .

What are the 2.3 variables?

Simply put, we can think of variables as a box that can store items such as keys, cell phones, and drinks in this box, or we can replace them with new items that we want to store when we need them.

In Java, we describe variables by three elements: variable type, variable name , and variable value .

If we compare a variable to a hotel room, the data to be stored is like the guest who wants to stay, we can arrange it according to Guest's request to stay "standard room" or "Presidential Suite", and can quickly find the guest's information according to the room name. In the same vein, in a Java program, we can also save it in a variable space of the specified type according to the format of the data we need to save, and quickly locate it with the variable name!

For example, we define a variable love, which is used to hold a string "Imooc", and in the program we can find the "IMOOC" stored in it just by finding the variable of love! Of course, we can also change the value of love into a new string "I love Imooc"!

Operation Result:

Dear friends, you must pay attention to it: The punctuation in Java is in English . For example, the end of the sentence semicolon, is a semicolon of English symbols, tens of millions of tables written in Chinese Oh ~ ~

2.4 How to name Java variables

As the hotel will give each room a personalized name, the variables in the program also need to use a reasonable name for the management---variable name!

It is important to note that the name of the hotel room can be a number, such as "802", can also be interesting names, such as "Peony", "President of the United States", "Water curtain hole", etc., but in the name of the variable must conform to certain rules , as follows:

The following variables are named in accordance with the specification:

But take a look at the following code, you know ha:

The habit of good siege master:

1, the variable name consists of multiple words, the first letter lowercase , followed by the first letter of the word capitalization , commonly known as camel-named method (also known as hump name), such as MyAge

2, when the variable name, as short as possible and can clearly express the role of variables, do see the name of understanding . Such as: Define variable name stuname save "student name" information

The length of the Ps:java variable name is not limited , but the Java language is case-sensitive , so price and price are two completely different variables Oh!

2.5 data types in Java

In general, in order to facilitate the storage of goods, we will specify the type of items each box can store, like in the "Smelly socks box" We will not put "bread"! Similarly, the storage of variables also pay attention to "categorize"!

The Java language is a strongly typed language. The popular point is that the data stored in Java is typed and must be determined at compile time. There are two kinds of data types in Java:

In the Java domain , the basic data type variable is stored in the data itself , whereas the reference type variable holds the spatial address of the saved data. Frankly, the basic data type variables are stored directly in the drawer, and the reference data type variable is stored in the drawer key, key and drawer one by one corresponding.

The common basic data types are:

You may have noticed:

As an example,
public class helloworld{
public static void Main (string[] args) {
String name= "Adoration class";
Char sex= ' male ';
int num=18;
Double price=120.5;
Boolean isok=true;
SYSTEM.OUT.PRINTLN (name);
SYSTEM.OUT.PRINTLN (Sex);
SYSTEM.OUT.PRINTLN (num);
SYSTEM.OUT.PRINTLN (price);
System.out.println (IsOK);
}
}

2.6 Rules for using variables in Java

Had to accept the variable little temper :

1. Variables in Java need to be declared and then used

2, when the variable is used, you can declare the variable at the same time to initialize

,

You can also assign a value after declaring it first

3. Only one value can be assigned at a time in a variable, but multiple times may be modified

4. The variables defined in the main method must be assigned before the output

5, although there is no error in the syntax, but in the actual development, the variable name is not recommended to use Chinese, easy to create security risks, such as the late cross-platform operation garbled and so on

About string: Represents the type of data stored in a variable as a string, which is described in detail in the next section, Little anticipation ~ ~

2.7 Automatic type conversion in Java

In Java programs, it is often necessary to convert data between different basic data types. For example:

,

The int variable score1 in the code can perform the assignment directly for the double type variable score2, and the result is: 82.0

This conversion is called automatic type conversion .

Of course automatic type conversions are required to meet specific conditions :

1. The target type can be compatible with the source type, such as type double compatible int, but char type cannot be compatible with int

2. The target type is greater than the source type, such as the double type is 8 bytes long, the int type is 4 bytes, so the double type of the variable can directly hold the int type of data, but the reverse is not possible

2.8 Forced type conversions in Java

I believe that the small partners have found that, although automatic type conversion is very convenient, but does not meet all the programming needs.

For example, how do you do this when you need to assign the value of a double variable to a variable of type int?

Obviously, this conversion is not automatic! Because the int type has a smaller storage range than a double type. This is done by forcing type conversions .

Syntax:(data type) values

Operation Result:

As you can see, by forcing the type conversion to assign 75.8 to the int variable, the result is 75, and the number is not rounded, but the decimal bit is truncated directly.

See, forced type conversions can result in loss of data Oh , the small partners in the application must be cautious Oh!

2.9 Application of Java constants

The so-called constant, which we can understand as a special variable, whose value is set, is not allowed to change during program Operation .

Syntax:final constant name = value;

Using Constants in your program can improve the maintainability of your code. For example, in project development, we need to specify the user's gender, at this time can be defined as a constant sex, assigned to "male", in the need to specify a user-specific place to call this constant directly, to avoid the user's non-standard assignment caused the program error situation.

Guys, watch out.: Constant names generally use uppercase characters

2.10 How to use annotations in Java

When you write a program, you often need to add some comments to describe the role of a piece of code.

In general, for a specification of the program source code, the note should be accounted for more than 1/3 of the source code. Therefore, note is an important part of the program source code, we must pay attention to Oh!

There are three types of annotations in Java: single-line comments, multiline comments, document annotations

Run Result: Hello imooc!

Look: The annotated code block will not be executed while the program is running ~ ~

We can use the javadoc command to extract the content from the documentation comments and generate the program's API help documentation.

Open the home page to view the generated API documentation

PS: You can also use the Javadoc tag When using document annotations to generate more detailed document information:

@author identify the author of the development of such modules

@version indicate the version of the module

@see reference steering, i.e. related topics

@param a description of a parameter in the method

@return Description of the return value of the method

@exception description of the exceptions that the method might throw

Third, operator-related

3.1 Arithmetic operators

public class helloworld{

public static void Main (string[] args) {

int age1=24;

int age2=18;

int age3=36;

int age4=27;

int sum=age1+age2+age3+age4;

Double AVG=SUM/4;

int minus=age1-age2;  

int newage=--age1;

System.out.println ("Sum of the Ages:" +sum);

System.out.println ("average age:" +avg);

System.out.println ("Age Difference:" +minus);

System.out.println ("Age after self-reduction:" +newage);

} }

Note that the difference between the two ways of adding self-subtraction is good.
3.2 Comparison Operators

public class helloworld{
public static void Main (string[] args) {
int a=16;
Double b=9.5;
String str1= "Hello";
String str2= "IMOOC";
System.out.println ("A equals B:" + (a== b));
SYSTEM.OUT.PRINTLN ("A greater than B:" + (a > B));
System.out.println ("A is less than or equal to B:" + (a<= b));
System.out.println ("str1 equals str2:" + (STR1==STR2));
}
}

Output to

A equals B:falsea greater than B:truea less than or equal to B:FALSESTR1 equals Str2:false


In short, it returns a Boolean, and = = ...

3.3 Conditional operators in Java

The conditional operator (? :) also known as the ternary operator.

Syntax form: boolean expression? Expression 1: Expression 2

Operation procedure: Returns the value of expression 1 if the value of the Boolean expression is true , otherwise returns the value of expression 2

For example:

Because the value of the expression 8>5 is true, the return: 8 is greater than 5

public class helloworld{
public static void Main (string[] args) {
int score=68;
String mark = (score>=60)? " Pass ":" Failed ";
System.out.println ("How the Exam results:" +mark);
}
}

The others are relatively basic, and they are not detailed.

On the priority level, remember one thing: () max.

PS: Expression (11+-8)/4 3 results are ()--2

Iv. about Circulation

Master the following example

public class HelloWorld {
public static void Main (string[] args) {

System.out.println ("Print right triangle");

Outer loop control Number of rows
for (int i = 1;i<=3;i++) {

Inner Loop controls the * numbers per line
The maximum value of the inner loop variable is equal to the value of the outer loop variable
for (int j = 1; j<=i;j++) {

System.out.print ("*");
}

Line wrapping after each line is printed
System.out.println ();
}
}
}

And then it's about for, judging the number of digits.

public class helloworld{

public static void Main (string[] args) {

int num = 999;

int count = 0;

for (int i=0;i<10;i++) {

int test=10^i;

if (num%test==0) {

Break

}

count=count+1;

}

System.out.println ("It is a number of" +count+ "bits!) ");

} }

Getting Started with Java (ii)--it's all about being punished ...

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.