ตั้งค่าการอ่าน

ค่าเริ่มต้น

  • เลื่อนอัตโนมัติ
    Java

    ลำดับตอนที่ #2 : Java I - Variables

    • อัปเดตล่าสุด 27 ธ.ค. 61


    Variables

    Data Types

    So far, we have worked with two kinds of values, strings and integers. These are called data types. There are other data types as well but let's first cover the two types we've learned, String and int. Keep in mind that the first letter of String is capitalized while int is not.

      

    What is a Variable?

              From here, we'll learn about variables. A variable is like a box with a name, in which you can store a value.

    Declaring Variables

              You first need to declare a variable before storing a value in it. There are two steps to declaring a variable in Java:

    Specify the data type of the value.

    Decide the name of the variable.

     Assigning Values

              After declaring a variable, you need to assign a value. You can do this in the following format: variable_name = value. = in programming does not mean "equal". It means to assign the value on the right to the variable on the left. Note that variable names are not enclosed in quotes.

     Using Variables

              The value of a variable will automatically be extracted when you use it. In the image below, System.out.println(number) will print 10, which is the value stored in the number variable.


     Using Variables

    Initializing Variables

              You can also declare a variable and assign a value to it at the same time, like int number = 3; or String text = "Hello World"; .

     

    Calculation with Variables

              If a variable is an integer, you can do calculations with it, just like with plain integers.

    You can use it with other integers, or integer variables.

     Concatenating Variables

              Similarly, you can concatenate string variables in the same way that you concatenate strings. Just be careful not to put variables in double quotes. If you do, it will just print the variable name instead of the value stored.


               Variables can be updated with another value. This can be done simply by assigning a new value to a variable that already has a value, using =.In the images below, you can see how the name variable is updated.

     

     Common Mistakes with Variables

              When updating a variable, you don't need to specify a type, as it has been already declared. If you do specify a type, it will be interpreted as a new variable. This will be an error because two variables can't have the same name.


    To add a number to a variable, you can just assign the sum of the variable's current value and the number back to itself. This may seem strange, but remember that = means assign, not equal.

     

    You can use shorthand like in the image below when adding numbers to integer variables. The syntax is the same for any kind of calculation.

      

    Adding 1, Subtracting 1

              When you're adding or subtracting 1, you can shorten it further with ++ and --. x++ is the same as x += 1, and x-- is the same as x -= 1.

     

     

    Roles of Variables

    Why Use Variables?

              We've learned the basics of variables, but why do we need them? One benefit is that variables make it easier to tell what the values represent, making the code easier to read.  Another benefit is that you can use the same data in multiple places.

      

    Roles of Variables

              Let's take a look at some examples. Variables come in handy when you have a few strings that are similar. In the example below, if you want to change "Java" to "Python", you only need to change it once, not three times.

    Important Rules

              There are some rules you need to follow when declaring variables. When you use a variable name that has more than two words, you capitalize the first letter of each word after the first. This is called camelCase.

    Variable Names

              Always make sure to choose a variable name that clearly tells what kind of data the variable has.  If you cannot tell the content of the variable from its name, the code will be hard to read and you might make mistakes. Try to use variable names that are descriptive!

     

     

    Doubles

    Decimal Numbers

              So far, we've learned that we use the int type to deal with integers, but what if we want to work with decimal numbers? We use the double type for this purpose. There are lots of other data types in Java, so look up "Java data types" if you're interested!

     

    The Double Type

              Numbers like 3 and -39 belong to the int type, which is for integers. The double type is for decimals, like 3.28 and -5.7. 3 is an int while 3.0 is a double, even though they are the same amount.

    Calculating Decimals

              Since the double type is for decimal numbers, you can do calculations with them like you did with integers.




    ติดตามเรื่องนี้
    เก็บเข้าคอลเล็กชัน
    นิยายแฟร์ 2024

    ผู้อ่านนิยมอ่านต่อ ดูทั้งหมด

    loading
    กำลังโหลด...

    อีบุ๊ก ดูทั้งหมด

    loading
    กำลังโหลด...

    ความคิดเห็น

    ×