java-data types, variables, constants

Source: Internet
Author: User

Data type

There are eight basic types:

Integer: Byte (1), short (2), int (4), Long (8)

Float type: Float (4), double (8)

Character type: char (2)

Boolean type: Boolean (1)

Variable

Variables are used to store data. In Java, variables must be declared. In essence, a variable is a small area of memory, and we access it in the program by using the variable name, so each variable must be declared before it is used (that is, the memory area is requested to the JVM (virtual machine)), and then it must be assigned (that is, the content of the requested memory space is populated), and finally the

int x; Variable declaration

x=12; Initialization of variables

Can also be written as int x=12;

int x;

Double d=3.5

X=d; cannot be compiled by

x= (int) D; Can pass because the cast operator is used.

When you perform mathematical expression operations in Java, you have the following automatic promotion rules:

1. All Byte/short/char types will be automatically promoted to type int.

2. If one of the operands is a long type, the result is a long type.

3. If one of the operands is of type float, the result is a float type.

4. If one of the operands is of type double, the result of the calculation is a double type.

Long y=12345678987654321l; The end plus L indicates that he is a long type, and if not added, it cannot be compiled.

Float x=1.7f//Eight data types only long and float plus suffixes.

Variables in Java are either one of eight basic types or a reference to an object, and we only have two choices.

The reference does not contain the actual data of the object he refers to, but instead points to the location of the object in memory.

If the variable is a basic type, then his value is stored in the same place as the variable. If the variable is a reference type, the value of the variable is a memory address, which is the location of the reference object, and the object contains the actual data.

String name;

Name= "Rich"

Character type (char)

' A ' character

"A" string

.....

Char a= ' a ';

Char b= (char) (a+1);

System.out.println (A+B); That is, two int type and: 65+ (65+1) output 131

System.out.println (the value of "A+b:" +a+b); Output AB, this println () statement is a connection string

Constant

Final double pi=3.1415; It can't be changed after +final.

It is customary for programmers to capitalize all the letters of a constant name so that they can be seen at a glance in the source code.

java-data types, variables, constants

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.