(i) Data types, variables and constants

Source: Internet
Author: User

Data type
The Java programming language defines eight basic data types (see), divided into four classes: integer classes (byte, short, int, long), text Class (char), floating-point class (double, float), and Logical Class (Boolean).


1. Integer class

(1) Three types are used-decimal, octal, and hexadecimal.
2--decimal value is 2;
077--The first 0 indicates that this is an octal value;
0xbaac--The first 0x indicates that this is a hexadecimal value.
(2) has a default int.
(3) Define long with the letter "L" and "L".
(4) integer types in all Java programming languages are signed numbers.

2. Text class

(1) Represents a single-bit Unicode character.
(2) You must include text that is quoted in single quotation marks (' ').
(3) Use the following symbols:
' A '--one character.
' \ t '--a tab.
' \u???? '--a special Unicode character,???? The four hexadecimal digits should be used strictly for substitution.

3. Floating-point classes
The default is a double type, which is a floating-point number if a number includes a decimal or exponential portion, or if the number is preceded by a letter F or f (float), D, or D (double).


4. Logic classes
There are two values for the Boolean data type: True and false.
For example: boolean flag = true;
The above statement declares that the variable flag is of type Boolean and that it is given a value of true.


Variables and constants
A constant is a quantity that is no longer changed during the entire run, such as π= 3.1415 in mathematics ..., which needs to be set as a constant in the program. A variable is the amount of change in the course of a program's operation, typically used to store intermediate results, or to output temporary values.
The declaration of a variable also refers to the creation of a variable. When executing a variable declaration statement, the system creates the appropriate storage space in memory based on the data type of the variable and assigns the initial value. A variable has a scope that is not valid beyond the block where it declares the statement.

In Java, we describe variables by three elements: variable type, variable name, and variable value

In Java, all variables must be declared before they can be used.

1. Basic Declaration of variables

Declaration format: Type variableName [, Variablename[=value] ...];
Type can be one of the basic types of java.
Example String s1= "Hi, everyone.";
String is the type of the variable, S1 is the name of the variable, "Hi, everyone." is the initialization value for S1. The value of the S1 is variable. This is the charm of the variable, depending on the situation, there are different values of variables.

here = means assignment, giving the variable a new value, not the equal number in mathematics.

Yes Note: The punctuation in Java is in English. For example, the end of the statement of the semicolon, string assignment of double quotation marks, are English symbols.

Good programmer's variable naming habits:
1, the variable name consists of multiple words, the first letter lowercase, followed by the first letter of the word capitalization, commonly known as camel-named method (also known as hump name), such as MyAge
2, when the variable name, as short as possible and can clearly express the role of variables, do see the name of understanding. Such as: Define variable name stuname save "student name" information
The length of the Ps:java variable name is not limited, but the Java language is case-sensitive, so price and price are two completely different variables Oh!


Here is an example of declaring and changing a variable using various types of variables. The pi in the program is constant, S1, I1, L1, Ch1, F1, D1, and B1 are global variables that can be changed in the method change and then output in the method main. While S2, I2, L2, CH2, F2, D2, and B2 are local variables of the method main, their scope is limited to the method main.
"Example" tests variables of different data types, program output.

The source program code is as follows:

program file name is Setvariable.java
public class SetVariable
{
Global variables

static double pi = 3.141592654;//mathematical constant static short S1;
static int i1;
static long L1;
static char ch1;
static float F1;
static double D1;
Static Boolean B1;
public static void Main (String args)
{
Local variables
Short S2 = 35;
int i2 =-32;
Long L2 = 34555L;
Char CH2 = ' A ';
float F2 = 897.89F;
Double D2 = 34.345;
Boolean b2 = false;
Output constants
System.out.println ("Mathematical constant pi =" + pi);
Output Local Variables
SYSTEM.OUT.PRINTLN ("****** local variable ******");
System.out.println ("short integer variable s2 =" + s2);
System.out.println ("integer Variable i2 =" + I2);
System.out.println ("Long integer variable l2 =" + L2);
System.out.println ("character variable CH2 =" + CH2);
System.out.println ("floating-point type F2 =" + F2);
System.out.println ("double type variable d2 =" + D2);
System.out.println ("boolean Variable b2 =" + b2);
Calling a method to modify the value of a global variable
Change ();
The value of the output global variable
SYSTEM.OUT.PRINTLN ("****** global variable ******");
System.out.println ("short integer variable s1 =" + S1);
System.out.println ("integer Variable i1 =" + I1);
System.out.println ("Long integer variable l1 =" + L1);
System.out.println ("character variable ch1 =" + ch1);
System.out.println ("floating-point type F1 =" + F1);
System.out.println ("double type variable d1 =" + D1);
System.out.println ("Boolean Variable B1 =" + B1);
}
Method: Modify the value of a global variable
public static void Change ()
{
S1 = 125;
I1 = 88;
L1 = 987654321L;
CH1 = ' B ';
F1 = 3.2590F;
D1 = -1.04E-5;
B1 = true;
}
}

(i) Data types, variables and constants

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.