Java from getting started to giving up: variables

Source: Internet
Author: User

What is a variable?

Variable, as the name implies, is the amount of change--

All right, give me a chestnut.

The various tableware on the picture is a variable, because the same plate can be different at different times, the table can be loaded with shredded potatoes, the next table can be filled with fried cucumber (of course, this plate should be washed clean drops).

So what are the main points of the variables in Java, and what are their syntax?

Let's write a piece of code to save the fried cucumber on top of it ...

1 String pan9527 = "清炒黄瓜";

This is the usage of the standard variable, its syntax format: data type variable name = value;

Three points:

1. Data type

Data types in Java fall into two main categories: value types, reference types.

Value types include:

1) integer type: Byte, short, int, long

2) Float type: float, double

3) Character type: Char

4) Boolean type: Boolean

There are a lot of reference types, the most common one:

1) Strings: string

These types of differences and so on are not detailed, or become a waiver of the article ·

Why should there be so many types? Very simple, because need AH!!!

Look at that picture above, we'll put rice in our bowls, dish with dishes, soup with bowls or pots, and a bamboo bowl with a cake. There are also many types of tableware, you Sir can think about it, if you want to use a cake of bamboo bowl soup is what effect? Conditional can go home to try, hey hehe ...

Give me another chestnut, for example, we want to keep an employee's information:

1234567     publicstaticvoidmain(String[] args){        String name = "李大爷";   //姓名        intsalary = 8600;      //薪水        charsex = ‘女‘;       //性别        doubleheight = 1.83;     //身高        booleanisDangYuan = false; //是否党员    }

Different information is saved using different types.

2. Variable name

Naming rules for variable names:

1) can only be preceded by letters and underscores (_)

2) consists of letters, numbers, underscores (_), and dollar signs ($)

3) cannot be a Java keyword or a reserved word (the simplest way to differentiate: an int or if or for a color-changing word cannot be used as a variable name).

Naming conventions for variables: to make sense, such as saving names, it's best to use name instead of N1 or XM or any other combination of characters that you can't see well-known.

The rule is like the law in the real , the violation of the end of the (in Java will be a syntax error, the program can not be executed), the specification is equivalent to the moral of the reality , the violation will not put you in prison, at most will be beaten (if the name of the variable is not called ABC , the program works just as well, but other programmers see that you have no morals. So we must be a law-abiding and four graces and programmer!!!

3. Value

Assigning values to variables is a matter of compatibility, such as the int type cannot be assigned to "haha". For the specific assignment syntax, look at the example that holds the employee information above.

Summarize:

characters, floats, and integers are numeric types. They can be converted, except that they have a different range of values.

123456     publicstaticvoid main(String[] args){        charc = ‘哈‘;    //最小        inti = c;        //稍大        float f = i;      //较大        doubled = f;     //最大    }

In this case, put the small value into the large type, which we call the automatic type conversion.

For example, we can transfer the water from a 500cc bottle to a 2000cc bowl is sure to be fine, but the 2000cc Bowl (full) of water in a 500cc bottle will definitely overflow.

123456     publicstatic voidmain(String[] args){        doubled = 86.5;        floatf = (float)d;        int i = (int)d;        charc = (char)i;    }

So if you put large types of data into small types, you have to cast them like this (that is, throw away the extra parts ...).

Finally, the string type and other types of conversions.

12345678910     publicstatic voidmain(String[] args){        String s = "10";        //字符串类型转数值类型最常用的方式        inti = Integer.parseInt(s);                 //数值型转字符串类型最常用的有种方式        String s1 = String.valueOf(i);        String s2 = Integer.toString(i);        String s3 = ""+ i;    }

Let's introduce a little bit of content today, the more detailed content, you crossing download a java_api_1.x version of the Help manual can be done.

Java from getting started to giving up: variables

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.