The first season of Java-start with Java??

Source: Internet
Author: User
Tags java keywords java se

The job search is over.

The final work, and Java closely related, but also quirks, but are software development, are good, but the focus is definitely in the Java side, PHP has the opportunity to continue to learn, then the United States said ~

First quarter of Java development

First, Introduction

1.1 Java se--Foundation core;

Based on this, also divided into Java EE (Enterprise application development, JSP, etc.), Java ME (embedded development, Android, etc.);

JVM (Java Virtual machine);

Java Virtual machine, so that Java code can run across platforms;

. java--"Compiler--" class--"interpreter-" display;

JDK Java Development Kit;

JRE Java Runtime Environment;

Relationship: The JDK contains the JRE containing the JVM;

1.2 Environment

One: Install JDK, official website, corresponding configuration installation;

Second: Environment configuration;

Java_home Configuring the JDK installation path; (Installation path)

PATH configures the location of the JDK command file; (Bin directory)

CLASSPATH the location of the Configuration class library file; (Lib folder)

Three: Verify: cmd command Window input java or javac

1.3 Writing Java programs in Notepad

Notepad writes. java--"Javac generation. class--" is explained by Java commands;

Switch to the directory where the file is located--"Javac hello.java--" Java Hello ( note that the Java command is not followed by the suffix name!!) );

1.4 Eclipse

I know that E is a green file, download to understand the compression can be used ...

General process: Create a Java project-"Create a Package"-"Write the source program--" run;

Note: The name of the package is customary to use the reverse of the domain name;

1.5 porting programs using export and import

Second, variable and constant

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, I almost forgot about the bigthing. The Java keyword is case-sensitive. So void is the keyword, but void is not the ~ ~

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:

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

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, with the result of: 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.

You see, forcing type conversions can cause data loss .

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!

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

4/23

Well, these are all for the time being, don't want to write ...

The direct is to do, this side is to make notes with good.

1. Using the Arrays class to manipulate arrays in Java

//Import Arrays ClassImportjava.util.Arrays; Public classHelloWorld { Public Static voidMain (string[] args) {//defines a string arrayString[] Hobbys = {"Sports", "Game", "movie" }; //Use the sort () method of the arrays class to sort the arrayArrays.sort (Hobbys); //Use the ToString () method of the arrays class to convert the array to a string and outputSystem.out.println (arrays.tostring (Hobbys)); }}

TM Notice, it's arrays, not an array.

2. Foreach Related

foreach is not a keyword in Java, it is a special simplified version of A for statement, which is simpler and easier to iterate through arrays and collections.

Importjava.util.Arrays; Public classHelloWorld { Public Static voidMain (string[] args) {//define an integer array and save the score information        int[] scores = {89, 72, 64, 58, 93 }; //sorting an array of arrays classesArrays.sort (scores); //iterating through the elements in the output array using foreach         for(intscore:scores)        {System.out.println (score); }    }}

3. Examples of two-dimensional array usages

 Public classHelloWorld { Public Static voidMain (string[] args) {//define two-dimensional arrays of three columns and assign valuesString[][] names={{"Tom", "Jack", "Mike"},{"Zhangsan", "Lisi", "Wangwu"}}; //outputs the values of elements in a two-dimensional array through a double loop         for(inti = 0; i < names.length; i++) {                         for(intj = 0; J < Names[i].length; J + +) {System.out.println (names[i][j]);        } System.out.println (); }    }}

Note: definition, names.length,names[i].length

4. Overloading of methods in Java

Q: What are the overloads of a method?

A: If the same class contains two or more than two methods with the same name, the number of method arguments, the order, or a different type of method, it is called an overload of the method, or it can be said that the method is overloaded. The 4 method names shown below are show, but the parameters of the methods are different and therefore belong to the overloads of the method:

Q: How do you differentiate which overloaded method is called?

A: When calling overloaded methods, Java determines which overloaded method should be called based on the number and type of parameters, and the method that exactly matches the parameters is executed. Such as:

Operation Result:

The basis for judging method overloading:

1. Must be in the same class

2. Same method name

3, the number of method parameters, order or type is different

4, not related to the modifier or return value of a method

The first season of Java-start with Java??

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.