004 Variables in Java

Source: Internet
Author: User

This article is for you to understand three questions

    1. What is a variable?
    2. How do I use variables?
    3. What are the specifications for variable naming?
Variable

The computer's memory is similar to the human brain, and the computer uses memory to store the data needed for computing.

Memory is like a hotel, different room types correspond to different data types, and memory stores data in a suitable space for the data as needed. This space for storing data is a variable.

Variable name memory Address value
Name 0x5c2a0c3f. Tom
Age 0XCD4D72A ... 10

Variable names, memory, and stored values such as the table above ↑

You can see the memory address is long and inconvenient to remember, but the variable name can be like a nickname for a person, easy to remember

Steps to use a variable

Two dogs home after school, mother gave him a good meal, two dogs eat these meals need what steps?

    1. Need to prepare a bowl or plate first
    2. To put a dish or soup into a container.
    3. Pinch a dish or drink soup with a spoon

The steps for using variables are the same as for eating with two dogs:

  1. Declaring variables, requesting space in memory based on data type
  2. Assignment to store data in the corresponding memory space
  3. Use variables to remove data using

Use the following code to demonstrate the process of using variables:

public class DemoVariable{    public static void main(String[] args){        //1.声明变量        int a;        //2.赋值        a=10;        //3.使用变量(打印变量的值到控制台)        System.out.println(a);    }}

The above assignment statement looks much like the equation in math, but the assignment and the equals sign are completely different.

The function of the equals sign in programming is to put the value on the right side of the "=" into the variable on the left and remember the rule later.

You can also declare variables and assignments at the same time:

public class DemoVariable{    public static void main(String[] args){        //1.声明变量并赋值        int a=10;        //2.使用变量(打印变量的值到控制台)        System.out.println(a);    }}
Summarize
    1. Syntax for declaring variables:

      Data type variable name;

    2. Syntax for variable assignment:

Variable name = value;

    1. syntax to declare variables and variable assignments at the same time:

Data type variable name = value;

Naming rules for variables

1. In Java, the capitalization of variable names is strictly differentiated, like Java and java is two completely different variables

2. Allow characters of the following rules to be variable names

First character: 1. Letter 2. Underline 3.$ Symbol

The remainder: any number of 1. Number 2. Letter 3. Underline 4.$ Symbol

For example a _ $_ a1 : $auser_name

Variable names in 3.java cannot conflict with keywords

Life in the ancient people's name can not be casually up, if the names of conflicts with the emperor may be beheaded. The Java compiler is as overbearing as the ancient Emperors: "Don't follow my rules, hum, just wait for the error."

For example: public static void class These keywords and the type of data we're going to talk about later

4. To avoid meaningless variable names

For example: aa bbb z1haha

Give it a try.
    1. Which of the following variable names is wrong? Why?
      stringString a+b 9variablemy name
    2. Can you run the following code in eclipse? Why?
public class Test1 {    public static void main(String[] args) {        String book;        System.out.println(book);    }}
    1. Can you run the following code in eclipse? Why?
public class Test2 {    public static void main(String[] args) {        int num=20;        int num=15;    }}

Encourage you to post your answers in your message, communicate with others, and search for related questions on Baidu

004 Variables in 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.