JAVA basics/Lesson 9: What are variables in variables/JAVA and java Variables

Source: Internet
Author: User

JAVA basics/Lesson 9: What are variables in variables/JAVA and java Variables

1. What is a variable?

The variable is defined as a data identifier.
1949 this is a number, representing a year
To name this number, it will be written in java as follows:

int year = 1949;

Int Is a data type, indicating an integer.
Year is an identifier.
= Is the value assignment operator.
1949 is a numeric value.
; Indicates that the row ends
Year is a variable, which represents the value of 1949.

public class HelloWorld {    public static void main(String[] args) {        int year = 1949;    }}
The type of a variable determines the value that the variable can contain.
There are eight basic types in Java, which are pre-defined by the Java language and are keywords.
These eight basic types are:
INTEGER (4 types)
Balanced (1 type)
Floating point type (2 types)
Boolean (1 type) 1. Integer

An integer variable is used to store integers.
Byte short int long
The difference is that different types of integer, maximum, and minimum values are different.
If you try to assign a value out of its range to a byte type variable, a compilation error will occur. type conversion is required at this time, which will be explained in the following sections.

Public class HelloWorld {public static void main (String [] args) {byte B = 1; short s = 200; int I = 300; long l = 400; /* if you try to assign a value out of its range to a byte type variable, a compilation error will occur */byte b2 = 200 ;}}
2. Balanced type

The char type is used to store one character. The value is enclosed in single quotation marks (double quotation marks)
Its length is the same as that of short, and it is also 16 bits.
Only one character can be stored. If you exceed one character, a compilation error occurs.

Public class HelloWorld {public static void main (String [] args) {char c = '中'; // char can only store one character, if you exceed one character, a compilation error occurs. char c2 = 'China'; // The error code is char c3 = 'AB'. // The error code is returned }}
3. Floating Point Type

Two types: float length: 32-bit double length: 64-bit
Note: The default small value is of the double type.
Therefore, a compilation error occurs when float f = 54.321, because the default type of 54.321 is double, and its type length is 64, exceeding the float length of 32
Add a letter f to the end of the number to declare the number as a float type.
Float f2 = 54.321f.

Public class HelloWorld {public static void main (String [] args) {double d = 123.45; // This row has a compilation error because 54.321 is a double float f = 54.321; float f2 = 54.321f ;}}
4. Boolean

Boolean is used to indicate true or false, and its length is 1.

boolean b1 = true;boolean b2 = false;

True or false
Although boolean data is actually stored 0 (false) 1 (true)
However, values cannot be assigned directly using 0 1.

Public class HelloWorld {public static void main (String [] args) {boolean b1 = true; boolean b2 = false; // although the boolean Type truly stores 0 (false) data) 1 (true) // However, values of boolean b3 = 1 cannot be directly assigned with 0 1 ;}}
5. String type

The String type is not a basic type, but it is widely used and is often mistaken as a basic type.
The String type is Immutable and cannot be changed once created.

public class HelloWorld {    public static void main(String[] args) {        String str = "Hello 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.