Java Basics-variables, data types

Source: Internet
Author: User
Tags java keywords

One, variable

1. The computer's memory is similar to the human brain, and the computer uses memory to memorize a large number of operations to use the data. Memory is a physical device, how to store a data? It is simple to think of memory as a hotel, and the data to be stored is like the guest who wants to stay.

First, the hotel's service staff will ask what kind of room, such as: Single room, double room, Presidential Suite, then, depending on the type of room selected, the waiter will arrange the appropriate room. "Open room, post-live" describes the process of storing data in memory.

First, it allocates a space in memory based on the type of the data type (i.e. to find a suitable room), then the data can be put into this space (i.e. check-in).

So why are there requirements for storage space? Imagine there are 3 guests, the waiter arranged a single room, this can stay? Allocating too little space can result in data not being stored.

The data is stored in memory for ease of use when needed, or if the data is changed, the values stored in memory are updated accordingly so that the new data can be used the next time.

A variable is a representation of a data storage space

The correspondence between variables and rooms

                  room in the hotel                   variable
Room name variable name
room type variable type
Check in variable values

Storing data can be easily and quickly found through variable names. assigning to a variable is storing the data in the same room as the variable name of the alias, and calling the variable to take the data out of that room. As can be seen, variables are a basic unit for storing data, and the variables are independent of each other.

Ii. data type 1. Common Java Data types
Data type Description Example
Int Integral type

Used to store integers, for example, number of students, number of employees in a company, number of days in a year, hours of the day

Double Double-precision floating-point type Used to store numbers with decimals, for example, the price of a commodity, the amount the World Bank loans to other countries, the wages of employees
Char Character type Used to store single characters, for example, gender ' male ', ' female ', ' excellent ' or ' good '
String String type Used to store a string of characters, for example, employee name, product model, product introduction information

As mentioned above, "it allocates a piece of space in memory based on the type of data", the data gets whether the type is an integer, a decimal, or a character, the different data in the storage is required for the space is not the same, for example, the value of int type to occupy 4 bytes, and the double value of 8 bytes, Therefore, different types of data require a different size of memory space to store, where int, double, char are the Java-defined keywords.

2. Declaration and use of variables

(1) Declare a variable, that is, "request a piece of space in memory based on data type", where you need to give the variable name

Syntax: data type Variable name

1 double score;     // declaring a double-precision floating-point variable score storing fractions 2 String name;      // declaring a String variable name stores student names 3 char sex;           // declaring a character type variable sex storage gender

(2) Assigning a value to a variable that "stores the data in the corresponding memory space"

Syntax: Variable Name value

1 score = 98.5     // storage 98.52 name = "Zhang San"    // storage "Zhang San"3 sex = ' male '       // store ' male '

This decomposition step is a bit cumbersome, you can also combine the steps (1) (2) and assign a value to a variable while declaring a variable.

Syntax: data type variable name = value

1 double score = 98.5; 2 String name= "Zhang San"; 3 char sex = ' male ';

(3) Call the variable. Using stored variables, we call them "call variables".

1 1 System.out.println (score);       // value stored from the console output variable score 2 2 System.out.println (name);      // the value stored by the variable name from the console output 3 3 System.out.println (sex);         // output variables from the console sex store values

In addition, both methods are theoretically beneficial. Note that " variables must be declared and assigned before they can be used ."

3. Variable Naming conventions
Serial number Conditions Legal variable Name Illegal variable name
1 The variable name must begin with a letter, an underscore "_" or a "$" symbol

_mycar

Score1

Smycar

Graph_1

*myvariable1//Cannot start with *

9vaeible//Cannot start with a number

Variable%//cannot contain%

A+B//cannot include +

My Variable//cannot include spaces

T1-2//cannot include hyphens

2 Variables can include numbers, but cannot start with a number
3 Variable names cannot contain any special characters other than "_" or "$" characters
4 You cannot use the Java language keywords, such as int, class, public, and so on

(1) There is no limit to the Java naming length, but the Java language is case-sensitive, so price and price are two completely different variables

(2) Java keywords, such as public, int, class, Boolean, void, static, etc., cannot be used as variable name, class name, package name

(3) The variable name is short and can clearly indicate the role of the variable, can be composed of one or more words, usually the first letter of a lowercase, followed by the first letter of the word capital

For example: int ageofstudent; Student Age

int ageofteacher; Age of teacher

(4) When defining variable names, try not to use some simple letters as variable names, such as a, B, C and so on, although correct, but later found that if there are 100 variables later, when using it is not clear what the meaning of

So try to use some meaningful words.

Java Basics-variables, data types

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.