variables, data types, and operators in Java

Source: Internet
Author: User

1. The Java language is a strongly typed language that has a clear distinction between the various data types, while the computer uses memory to memorize the data that is needed for a large number of operations, and when a variable is declared, it divides a block of space in memory to store the data, and the variable type determines the size of the memory space.

2. basic data type:  

byte: The smallest data type, which occupies 8 bits (bit), or 1 bytes, in memory, with a range of values -128~127      short: Shorter integer, 2 bytes in memory, range of values -32768~32717int: integer for storing integers, 4 bytes inside, range of values -2147483648~2147483647Long: Integer, 8 bytes in memoryfloat: Floating point, 4 bytes in memory, used to store a number with a decimal point (the difference from a double is that the float type has a valid decimal point of only 6~7 bit)double: dual-precision floating point, used to store a number with a decimal point, which accounts for 8 bytes in memoryChar: Character type, for storing a single character, accounting for 2 bytesBoolean : Boolean type, 8 bytes, used to determine true or false (only two values, that is, true, false)3. Reference data type:Array    String : strings for storing a string of characters    

4. variable declaration and use:

    declaring variables

syntax: data type variable name;     
double score;     // declaring a double-precision floating-point variable score storing fractions
String name; // declare string variable name store student name
assigning values to variablesSyntax: variable name = value;eg:

  

Score = 98.5= "Zhang San";

Data type variable name = value;

Double score = 98.5= "Zhang San";

Variable name naming rules

1) The variable must start with a letter, an underscore "_", or a Dollar "$" symbol

2) variables can include numbers, but cannot start with a number

3) In addition to the "_" or "$" symbol, the variable name cannot contain any special characters

4) Keywords that cannot be used in the Java language

Constant

Constants represent values that cannot be changed during a program's run, when you define constants, you need the final keyword, the constant name is capitalized, and multiple words are separated by "_",

Syntax: Final data type constant name = value;

    

Final Double // defines a constant that represents Pi Final int // define constants that represent the largest number of people    

5. Operators

1) Assignment operator     

=: Assigns the right value to the left, example: int a = 1;     + =: Left and right side of the sum of the two, example: int a = 1; a+=2; result A has a value of 3- =: The left side minus the right margin to the left, example: int a =5;a-=2; result A has a value of 3*=: The value multiplied by both sides is assigned to the left, example: int a = 2;a*=2; result A has a value of 4/=: To the left by dividing the left by the right side, for example: int a = 6;a/=2; result A has a value of 3%=: Left divided by the remainder of the right, example: int a =7;a%=2; result A has a value of 1

2) Arithmetic operators

"+" two numbers added

"-" two number subtraction

"*" Multiply two numbers

"/" Divide two numbers

"+ +" self-increment 1

"--" self minus 1

(The difference between i++ and ++i)

1) Usage of ++i (take A=++i, i=2 as an example)

The I value is first added to 1 (that is, i=i+1) and then assigned to the variable a (that is, a=i),

then the final a value is equal to 3, and the I value equals 3.
  
so a=++i equivalent to i=i+1, A=i

2) Usage of i++ (take a=i++, i=2 as an example)

first assign the I value to the variable a (that is, a=i), then I value 1 (that is, i=i+1),

then the final a value is equal to 2, and the I value equals 3.

so a=i++ equivalent to a=i, i=i+1

3) ++i and i++

a=++i equivalent to i++, A=i

a=i++ equivalent to a=i, i++
  
4) When ++i and i++ are used alone, the equivalent of i=i+1

If you assign a new variable, the ++i first assigns the I value to 1 and the i++ first to the new variable.

Relational operators

: Greater Than, example: int a = 1;int B = 2; System.out.print (a > B); The result is false<: Less Than, example: int a = 1;int B = 2; System.out.print (a < b); The result is true>=: Greater than or equal, example: int a = 1;int B = 2; System.out.print (a >= b); The result is false<=: Less than Equals, example: int a = 1;int B = 2; System.out.print (a <= b); The result is true= =: equals, example: int a = 1;int B = 2; System.out.print (A = = B); The result is false! =: Not Equal, example: int a = 1;int B = 2; System.out.print (A! = b); The result is trueThe result is a Boolean type, either True or false      logical Operators&&: With, and (short circuit), two conditions are true at the same time, the result is true| | : Or, or (short circuit), two conditions have a true, the result is true!: Non, (!+ condition) when the condition is true, the result is false   6.data type conversions in Java 1: Automatic data type conversion (magnification conversion)satisfy automatic data type conversion conditions:1) Two types to be compatible: such as numeric type (integer and floating point type)2) The target type is greater than the source type: for example, int data can be automatically converted to double type 2: Force data type conversion (narrowing conversion)precede the variable with parentheses, specifying the type of the strong conversion in parenthesesExample: double A = 40.9;int b = (int) A;Note: Casting loses numeric precision, such as a double type variable a, which is converted to an int type after the value has been changed to

variables, data types, and operators 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.