The first season of getting started with Java-from now on Java ??, Getting started with java

Source: Internet
Author: User

The first season of getting started with Java-from now on Java ??, Getting started with java

Looking for a job has come to an end.

The final work is closely related to java, which is also a negative positive, but it is good for software development. However, in the future, the focus must be on the java side. php has the opportunity to continue learning, so beautiful ~

 

Java Development Season 1

I. Introduction

1.1 Java SE-basic core;

Based on this, it is further divided into Java EE (enterprise application development, JSP, etc.) and 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;

Link: JDK contains JRE and JVM;

1.2 Environment

1. Install JDK on the official website;

2. Environment Configuration;

Configure the JDK installation path for JAVA_HOME. (installation path)

PATH: Specifies the JDK command file location. (bin directory)

CLASSPATH configuration class library file location; (lib folder)

Iii. Verification: Enter java or javac In the cmd command window

1.3 use NotePad to write java programs

Write. java in Notepad -- generate. class in javac -- and explain it with java commands;

Switch to the file directory -- javac hello. java -- java hello (Note that the java command is not followed by a suffix !!);

1.4 eclipse

I only know that e is a green file. You can download and understand compression...

General process: Create a java project -- create a package -- compile a source program -- and run the program;

 

Note: The package name is used to reverse the domain name;

1.5 Use export and import for program porting

2. Variables and constants

2.1 keywords in Java

Some special-purpose words in Java are called keywords. Keywords have special significance for Java compilers. Be careful when using them in programs !!

Common keywords in Java:

Q: You can't remember that much ...... -_-|

A: Now you don't need to remember everything. You just need to get familiar with it. During the learning process, you will gradually find the feeling and remember them.

By the way, I almost forgot big things,Java keywords are case sensitive.Therefore, void is a keyword, but Void is not ~~

2.2 Java identifier Recognition

Q: What is the identifier?

A: An identifier is a symbol used to name variables, classes, and methods in a Java program.

When using identifiers, follow the following rules:

1. An identifier may consist of letters, numbers, underscores (_), and dollar signs ($), but it cannot contain other special characters such as @, %, and space. It cannot start with a number. For example, 123 name is invalid.

2. The identifier cannot be a Java keyword or a reserved word (a reserved keyword in Java, which may be used as a keyword in later versions), but can contain keywords and reserved words. For example, void cannot be used as an identifier, but Myvoid can

3. The identifier is case sensitive. Therefore, it must be clear that imooc and IMooc are two different identifiers!

4. It is recommended that the name of the identifier reflect its role, so that you can understand the name.

What is the 2.3 variable?

Simply put, we can regard variables as a box where keys, mobile phones, drinks, and other items can be stored, you can also replace it with the new items we want to store as needed.

In Java, three elements are used to describe variables: variable type, variable name, and variable value.

If we compare variables to the hotel room, the data to be stored is like the guests who want to stay, we can arrange for the guests to stay in the "Standard Room" or "presidential suite" according to their requirements ", you can quickly find the information of the guests Based on the room name. Similarly, in a Java program, we can save the data in the specified variable space based on the format of the data to be saved, and quickly locate it using the variable name!

For example, we have defined a variable "love" to save a string "imooc". Once the variable "love" is found in the program, we can find the "imooc" stored in it "! Of course, we can also replace the value in love with the new string "I love imooc "!

Running result:

Dear friends, please note that the punctuation marks in Java are in English. For example, the end of a statement is a semicolon (;), which is an English symbol. If you write tens of millions of tables in Chinese ~~

2.4 how to name Java Variables

Just as the hotel will give a personalized name to each room, variables in the program also need to be managed with a reasonable name-variable name!

Note that the name of a hotel room can be a number, such as "802" or an interesting name, such as "Peony", "President of the United States", and "Water Curtain Cave, however, when naming a variable, the following rules must be met:

The names of the following variables comply with the specifications:

But please refer to the following code, you know:

Habits of excellent city siege teachers:

1. When a variable name is composed of multiple words, the first letter of the first word is lowercase, and the last word is capitalized.Camel naming method (also known as the camper naming method), Such as myAge

2. When naming variables, express the role of variables as short and clear as possible to achieveMingzhiyi. For example, define the variable name stuName to save the "Student name" information.

PS: there is no limit on the length of the Java variable name, but the Java language is case sensitive, so price and Price are two completely different variables!

2.5 Data Types in Java

In general, to facilitate the storage of items, we will specify the types of items that can be stored in each box. Just like in the box with smelly socks, we will not put "Bread! Similarly, variable storage also focuses on "classification "!

Java is a strongly typed language. In general, the data stored in Java is of a type and must be determined at compilation. Java has two types of data:

In the Java field, the basic data type variables are stored in the data itself, while the reference type variables are stored in the space address of the data. To put it bluntly, the basic data type variables store things directly in the drawer, and the reference data type variables store the keys of the drawer. The keys correspond to the drawers one by one.

Common basic data types include:

2.6 rules for using variables in Java

Some variables have to be accepted:

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

2. When using a variable, you can declare the variable and initialize it at the same time.

,

You can also declare and assign values first.

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

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

5. Although no errors are prompted in the syntax, it is not recommended to use Chinese characters for variable names during actual development, which may cause security risks, such as garbled Characters During later cross-platform operations.

2.7 automatic type conversion in Java

In Java programs, data of different basic data types often needs to be converted to each other. For example:

,

In the code, the int-type variable score1 can directly assign values to the double-type variable score2. The running result is: 82.0.

This type of conversion is called automatic type conversion.

Of course, automatic type conversion must meet specific conditions:

1. The target type can be compatible with the source type. For example, the double type is compatible with the int type, but the char type is not compatible with the int type.

2. the target type is larger than the source type. For example, the length of the double type is 8 bytes and the int type is 4 bytes. Therefore, variables of the double type can directly store int type data, but in turn cannot.

2.8 forced type conversion in Java

I believe our friends have also discovered that, although automatic type conversion is very convenient, it cannot meet all programming needs.

For example, how can I implement the function of assigning the value of the double variable to an int variable in a program?

Obviously, this conversion will not be performed automatically! Because the storage range of the int type is smaller than that of the double type. In this case, the type conversion is required.

Syntax: (data type) Value

Running result:

It can be seen that after 75.8 is assigned to the int type variable through forced type conversion, the result is 75, and the numeric value is not rounded, but the decimal place is truncated directly.

Understand,Forced type conversion may cause data loss

2.9 application of Java Constants

The so-called constant can be understood as a special variable. After its value is set, it cannot be changed during the program running.

Syntax: final constant name = value;

Using constants in a program can improve the maintainability of the Code. For example, during project development, we need to specify the user's gender. In this case, we can define a constant SEX with a value of "male". You can directly call this constant where the user's gender needs to be specified, this avoids program errors caused by user non-standard assignment.

Note: constant names generally use uppercase characters.

2.10 how to use annotations in Java

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

In general, for a standard program source code, comments should account for more than 1/3 of the source code. Therefore, annotations are an important part of the program source code and must be paid attention!

There are three types of annotations in Java: single-line comments, multi-line comments, and document comments.

Running result: Hello Imooc!

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

Open the home page to view the generated API documentation

PS: You can also use the javadoc tag to generate more detailed document information when using document Annotations:

@ Author indicates the author of The development class module

@ Version indicates the version of the module.

@ See references, that is, related topics

@ Param description of a parameter in the other method

@ Return description of the return value of the Method

@ Exception describes possible exceptions thrown by methods.

 

4/23

 

Er, I don't want to write any of these records...

It's just the work. Here it's the work of taking notes.

1. Use the Arrays class to operate Arrays in Java

// Import the Arrays class import java. util. arrays; public class HelloWorld {public static void main (String [] args) {// define a String array String [] hobbys = {"sports", "game ", "movie"}; // use the sort () method of the Arrays class to sort the array Arrays. sort (hobbys); // use the toString () method of the Arrays class to convert the array to a string and output the System. out. println (Arrays. toString (hobbys ));}}

 

Tm Note: Arrays is not Array

 

2. foreach Problems

Foreach is not a keyword in Java. It is a special simplified version of the for statement. It is easier and more convenient to traverse arrays and collections.

Import java. util. arrays; public class HelloWorld {public static void main (String [] args) {// defines an integer array and saves the score information int [] scores = {89, 72, 64, 58, 93}; // sort the array in the Arrays class. sort (scores); // use foreach to traverse the elements in the output array for (int score: scores) {System. out. println (score );}}}

 

 

3. Two-dimensional array usage example

Public class HelloWorld {public static void main (String [] args) {// defines two-dimensional arrays of three columns in two rows and assigns values to String [] [] names = {"tom ", "jack", "mike" },{ "zhangsan", "lisi", "wangwu "}}; // outputs the element value for (int I = 0; I <names. length; I ++) {for (int j = 0; j <names [I]. length; j ++) {System. out. println (names [I] [j]);} System. out. println ();}}}

 

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

 

4. Method overloading in Java

Q: What is method overloading?

A: If the same class contains two or more methods with the same name, number of method parameters, sequence, or type, it is called method overload, this method can also be called overloaded. The names of the four methods are show, but the parameters of the methods are different. Therefore, all methods are overloaded:

Q: How do I differentiate which overload method is called?

A: When you call a method that is overloaded, Java determines which method to call based on the number and type of parameters. The method that completely matches the parameter will be executed. For example:

Running result:

Basis for determining method overloading:

1. It must be in the same class

2. The method name is the same

3. The number, sequence, or type of method parameters are different.

4. It is irrelevant to the modifier or return value of the method.

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.