Java variables, data types, and operators

Source: Internet
Author: User

1. Variables

Computers process data, variables are used to store processed data, which is called variables because you can change the stored values. Rather, a variable points to an address that stores a particular type of value, in other words, a variable has a name, type, and value. A variable has a name, such as radius, area, age, and height, and the name of each variable is unique, and this way we can set and get the value of the variable.

We simply take the hotel of our lives to illustrate the room variables in the hotel

Room Name Variable name

Room type Variable type

The value of the guest variable for occupancy

The variable name makes it easy and quick to find the data he stores. Assigning data to a variable is storing the data in the same room as the variable name of the alias, and invoking the variable is to take the data out of that room and use it. As can be seen, variables are a basic unit for storing data, and different variables are independent of each other.

There is a type for a variable, and here is an alternative to the Java type:

int: represents integers such as 123 and 456

Double: indicates floating-point numbers such as 3.1416,-55.66, 1.2e3, and -4.5E-6

String: Represents text such as "Hello", "Good morning!", text is usually embedded in double quotation marks

Char: Represents a single character such as ' A ', ' 8 ', and a single character is usually embedded inside a single quotation mark.

Variables store specific types of values, especially in programming, for example: an int variable stores an integer 123, but cannot store a floating-point number of 12.34, nor can the text "Hello" be stored.
Early programming languages introduced the concept of a type to interpret binary 01 data, which defines the structure, size, scope, and sequence of operations for that type of data.

2. Name

Identifies a sequence of characters of any length, such as uppercase and lowercase letters, numbers, underscores, and $.

Whitespace (spaces, tabs, newline characters) and other special characters (such as +-*/@ &, etc.) are not allowed, and the names of the errors are: Max value and Max-value.

Identifiers cannot begin with a number (0-9) and must be preceded by a letter (A-Z, A-Z), underscore (_), and $, and the system retains an identity that begins with $.

Identifiers cannot use keywords and reserved fields (for example: Class,int,double,if,else,for,true,false,null).

Identifiers are case-sensitive, and Rose, rose, and Rose are 3 different variables.


Eight common data types in Java:

Int,double,short,float,boolean,byte,long,char

Bytes:

Numbers, English (both uppercase and lowercase), English punctuation takes one byte
Man, Chinese punctuation takes two bytes

Memory:

The computer uses memory to store the data that is needed for the calculation

There are two ways to create a variable:
1. Declaration--assignment--value
declaring variables
int money;
Saving
=: assignment operator (assigns the right value to the left)
Assignment variables
money=1000;
+: For connection
Calling variables
SYSTEM.OUT.PRINTLN ("Storage" + ":" +money);

2. Declaration + Assignment-value

Double price=99.99;
SYSTEM.OUT.PRINTLN (price);

Constant:

Constant final: One but the assignment cannot be changed
final int sum;
sum=3;

3. Operators

1. Assignment operators:

=: Represents the assignment operator: assigns the right value to the left

2. Arithmetic operators:

+: Addition a+b
-: Subtraction A-B
*: Multiplication a*b
/: Division A/b
%: Take-over a%b

3. Relational operators:

: Greater than 10>13 result is false
<: Less than 10<13 result is true
>=: greater than or equal to
<=: Less than or equal to
= =: equals
! =: Not equal to 0

Scanner:
Scanner input=new Scanner (system.in);
Guide Package: Java.lang.util

Boolean type:
Value:
False: False
True: True

Precedence of operators: arithmetic operators > Relational operators > Logical operators, that is, in an expression, the arithmetic operator has the highest precedence, the relational operator is the second, and then the logical operator. The logical operators will learn in the next chapter. If an assignment operator is included in an expression, it has the lowest precedence.

Java variables, data types, and operators

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.