Java data types and operators

Source: Internet
Author: User

1. Data type:
The Java language is a strongly typed language that defines specific data types for each data and allocates different sizes of memory space in memory
1.1 Classification of data types:
Basic data types
Reference data type: The following describes
The basic data types are described below: four classes of eight
Integer type
Byte occupies one byte -128----127
Short occupies two characters -2^15----2^15-1
int accounted for four bytes -2^31----2^31-1
Long occupies eight bytes -2^63----2^63-1
Floating point Type
Float accounted for four bytes -3.403E38----3.403E38
Double is eight bytes -1.798E308----1.798E308
Character type
Char accounted for two bytes 0----65535
Boolean type
Boolean
Note: Note the float type when assigning values, long type
When an integer is assigned a long value, the data is appended with L
When a decimal value is assigned to float, the data is appended with an F
As follows:
long x = 8888888888L;
float F = 12.3F;
The integer default data type is int type
The default data type for decimals is double

1.2 Scope of variables:
is the valid use range of the variable
This divides the variables into:
Local variables
Global variables: Variables that are defined outside of a method in a class
Local variables: Variables that are defined in a method or on a method

1.3 Data type conversions:
Divided into:
Implicit conversions
Cast: Forces a large data type to be converted into a small range of data
Implicit conversions: A data type with a small range of values and a data type with a large range of values will first be promoted to a large, and then the result of the operation is the latter

Implicit conversions:
The code looks something like this:
int x = 3;
byte B = 4;
x = x + b;
SYSTEM.OUT.PRINTLN (x);
The code is interpreted as follows:
Define a variable to be of type int, assign 3
Defines a variable that is of type Byte, assigned a value of 4
Computes the result of the sum of the two, and assigns the value to the INT type X
Because the implicit conversion, the variable B is first promoted to the int type, so that the result of the operation is 7 (int type), assigned to an int type X, no problem
And look at the following code, there are no problems:
int x = 3;
byte B = 4;
b = x + b;
System.out.println (b);
The result of the operation is 7 (int type), assigned to the byte type X, is not assignable, there is a problem, only a small range of data types can be assigned to a large range of data types
Code compiled upstairs will be error-

Cast:
Format: (data type) data
int x = 3;
byte B = 4;
b = (byte) x + B;
System.out.println (b);
Attention:
If the data exceeds the value range of the assigned data type, the result will be an error

The following interview questions:
BYTE B1 = 3;
byte b2 = 4;
BYTE B3 = b1 + b2;
byte B4 = 3 + 4;
Is there a problem with defining B3 and B4?

During the operation of a variable, as long as the operation between the data type (including int) is smaller than the range of int, the operation is promoted to int (because the integer default data type is int), so the first computed int type cannot be assigned to byte, which is wrong
The wrong

The second is the operation of the constant, the Java compiler has a constant optimization mechanism, that is, at the time of compiling the 3+4 results calculated, obviously 7 is in the range of byte type, so it is no problem

1.4 Data Range Comparison:
When mixed operations are performed, the Byte,short,char does not convert to each other, the automatic type is promoted to the int type, and the other types are mixed and the small data types are promoted to large
Range Comparison:
Byte,short,char < int < Long < float < double

Look at the following code to write the result:
float F = 12.3f;
long x = 12345;
f = x;//implicit conversion long rotary float
System.out.println (f);//output Result: 12345.0

float F = 12.3f;
long x = 12345;
x = (long) f;//cast
SYSTEM.OUT.PRINTLN (x);//output Result: 12

The 1.5char type participates in the operation:
The char type can participate in the +-operation, which can be traced to the value of the char type in the ASCII table, and then the arithmetic
Look at the following code:
System.out.println ("Hello" + ' a ' + 1);//Result: HELLOA1, string and other data type operations, result or character count
System.out.println (' A ' +1+ "hello");//Result: 98hello,a corresponds to 97, results are calculated sequentially

2. Operators and common face questions
2.1 See Code Write Result:
System.out.println (10/3);
Integer divide result can only be integer
System.out.println (10/3.0);
If you want a decimal, turn one number into a decimal, and the other number automatically type up when the operation is made
SYSTEM.OUT.PRINTLN (-5% 2);
The symbol for the result of the% operator is only related to the left, not to the right
SYSTEM.OUT.PRINTLN (13-5);
Results:
3
3.3333333333333335
-1
3

2.2 Arithmetic operators + + and--usage:
Used alone:
int a = 3;
a++;//a = a + 1;
System.out.println (a);
is placed before and after the operand

Participate in the operation:
Look at the following code:
int a = 3;
int b;
b = a++;//when + + is behind a variable, the value in the variable is first removed for assignment and then added by itself by 1
System.out.println ("a =" + a);
System.out.println ("b =" + B);
Result of Operation:
A = 4
b = 3

Look at the following code:
int a = 3;
int b;
b = ++a;//when + + is in front of the variable, it adds 1 itself and then assigns the result to the value
System.out.println ("a =" + a);
System.out.println ("b =" + B);
Result of Operation:
A = 4
b = 4

2.3-Side questions:
byte B = 10;
b++;
b = B + 1;
Ask which sentence will be an error, why?
b++; actually equivalent to B = (byte) (b + 1), no problem
is actually similar to b+=1; This shorthand operator is internally forced to type conversion processing, so it's no problem.
b = b + 1; it's an error, it's been analyzed.

Java data types and operators

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.