Java Basic Syntax----variables

Source: Internet
Author: User
Tags ranges

Variable

The concept of variables

A storage area in memory
The zone has its own name (variable name) and type (data type)
Each variable in Java must be declared first and then used
Data in this region can vary from one type to the same range
Define the format of the variable: data type variable name = Initialize value

1  Public classtest{2 3     //member Variables4     intc = 12;5      Public Static voidmain (String [] args) {6         7         //declares a variable, a local variable8         intA = 12;9A = 15;Ten          One System.out.println (a); A          -         //about the scope of a variable -         { the             intb = 20; - System.out.println (b); - System.out.println (a);  -         } +          -         //System.out.println (b); +     } A}

variable is used to access this area by using the variable name.
Java is a strongly typed language, and each variable must first declare a type and then use
Use variables Note:
Scope of the variable: valid between a pair of {}

Classification of variables

Divided by the declared location:
Member variables: Variables that are internally defined by methods outside of the class
Local variables: Variables defined inside a method or statement block
Note: Outside the class (outside the braces for the class) cannot have the declaration of the variable
By the data type that belongs to:
Basic Data type variables
Reference data type variables


classification of variables-by data type

Specific data types are defined for each type of data, and memory spaces are allocated in memory in different sizes.

integer type: Byte, short, int, long

Java integer types have a fixed number of table ranges and field lengths that are not affected by the specific OS to ensure portability of Java programs.
The integer constants in Java are by default int, declaring long constants to be followed by ' l ' or ' l '

// Integral type byte i1 = 124;  Short i2 = 3456; int i3 = 3456789; long i4 = 2345L;


Float type: float, double

Like integer types, Java floating-point types also have a fixed number of table ranges and field lengths that are not affected by the specific OS.
The Java floating-point constant defaults to type double, declaring a float constant, followed by ' F ' or ' F '.

//floating Point Type//the Java floating-point constant defaults to type double, declaring a float constant, followed by ' F ' or ' F '. floati5 = 12.34F; //Note: Assigning a variable of type byte directly to a value within a valid range can be compiled.//instead of assigning a variable of type float directly to a floating-point type in a valid range, you cannot compilebyteI6 = 12;floati7 = 12; //I6 = 9;//int i8 = 9;//A compilation error has occurred.//I6 = i8;

There are two types of expressions for floating-point constants:
Decimal number form: 5.12 512.0f. 512 (must have decimal points)
Scientific notation form: For example: 5.12e2 512E2 100E-2

character type: char

Char data is used to denote "characters" in the usual sense.
Three representations of character constants:
Character constants are single characters enclosed in single quotation marks ("'), covering all the characters of the world's written language. For example: char c1 = ' a '; char C2 = ' Medium '; char C3 = ' 9 ';
The escape character ' \ ' is also allowed in Java to convert subsequent characters into special character constants. For example: char c3 = ' \ n '; --' \ n ' indicates a newline character.
Use Unicode values directly to represent the character type constants: ' \uxxxx '. where xxxx represents a hexadecimal integer. such as: \u000a means \ n.

//Character TypeCharC1 = ' Shang ';        System.out.print (C1); //Escape CharacterCharC2 = ' \ n ';        System.out.print (C2); //System.out.println () wrap after printing, System.out.print () does not wrap after printingSystem.out.println (C1); //using Unicode encoding to mark charactersCharC3 = ' \u0061 '; System.out.println (C3); //' A 'System.out.println (C3+ 1);//' B '//using forced type conversionsSystem.out.println ((Char) (C3 + 1)); //String str = "ABCDE";

The char type is available for operation. Because it should have Unicode values.


Boolean type: Boolean

Boolean types are suitable for logical operations and are generally used for program flow control:
If condition control statement;
While loop control statement;
Do-while loop control statement;
For loop control statement;

// Boolean type Boolean true  false;         // Do not replace true and false with integers 0 or 0, which differs from the C language.  //b1 = 0;

Boolean data only allows values of true and false, and cannot replace true and false with integers of 0 or not 0, which differs from C language.

basic data type conversions

Automatic type conversions: small-capacity types are automatically converted to large-capacity data types. The data types are sorted by capacity size:

When there are multiple types of data blending operations, the system first automatically converts all data into the type of data with the largest capacity before it is calculated.

// 1. Automatic type conversion int I9 = 123; float i10 = I9;


Byte,short,char do not convert to each other, and the three of them are first converted to the int type when calculating .
When the value of any base type is concatenated with a string value (+), the value of the base type is automatically converted to the string type.


Forcing type conversions

The inverse process of automatic type conversion converts a large-capacity data type to a small-capacity data type. Use the cast character (()), but it may result in a decrease in accuracy or overflow, especially to be aware.

// to add a cast character () when used float i11 = 12F; int i12 = (int) i11;


In general, strings cannot be converted directly to basic types , but wrapper classes that correspond to basic types can be implemented to convert strings to basic types. such as: String a = "43"; int i = Integer.parseint (a);

//Note: Incompatible types cannot be converted between//1. String cannot be converted directly to base type//2. The Boolean type cannot be converted to another data type//3. When there are multiple types of data blending operations,//The system first automatically converts all data to the type of data with the largest capacity before it is calculated//The result of dividing two int types is still an int type.//The following code compiles an error.//int i13 = 12/5.0f;//System.out.println (I13); //4. Byte,short,char do not convert to each other, the three of them are first converted to the int type when calculating. byteI14 = 12;byteI15 = 13; //The following code compiles an error.//byte i16 = i14 + i15;

The Boolean type cannot be converted to another data type.

Java Basic Syntax----variables

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.