Variables and constants of Java

Source: Internet
Author: User
Tags java keywords

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 ~ ~

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.

 1  package    Com.imooc;  2  3  public  class  demo02{//  here Com.imooc and Demo02 are identifiers  4  public static  void   Main (sting[] args) { 5  System.out.printl N ("Hello World"  6   7  }

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 ABC and ABC are two different identifiers Oh!

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

What the variable is:

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 to "standard room" or "Presidential Suite" according to the guest's request,

And can quickly find the guest information according to the room name. Similarly, in Java programs, we can also use the format of the data that we need to save,

Save it in variable space of the specified type, and quickly locate it by 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"!

1  Public classdemo03{2      Public Static voidMain (sting[] args) {3         //String is the variable type love is the variable name IMOOC is the value of the variable4String love = "Imooc"; 5                                         //use "+" for concatenation of strings and variables6System.out.println ("The content of the variable Love is:" +love);//don't forget the semicolon at the end of the statement .7         8Love = "I love Imooc";//Variable Update Assignment9         TenSystem.out.println ("The content of the variable Love is:" +Love ); One     } A}

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 ~ ~

How to name a Java variable:

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 should be noted 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" and so on,

However, when you give a variable a name, you 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

PS: There is no limit to the length of the Java variable name, but the Java language is case-sensitive , so price and price are two completely different variables Oh!

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:

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. Variables defined in the Main method (function) must be assigned before they can be 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

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 and the int type is 4 bytes, so a variable of type double can hold data of type int directly.

But it's not going to turn out.

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, when developing a project, we need to specify the gender of the user, at which point you can define a constant sex, assign a value of "male",

Call this constant directly where you want to specify the user's nature, and avoid situations where a program error occurs because of a user's irregular assignment.

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

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!

Variables and constants of 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.