Basic data types and operations for Java

Source: Internet
Author: User

Coding

ascii--0~127 65-a 97-a

Western European Code Table---iso-8859-1---0-255---1 bytes

GB2312----0-65535---GBK---2 bytes

Unicode encoding System---utf-8---3 bytes

Medium F

Bit bit byte byte 1Byte = 8bit 1kb=1024b MB GB TB---storage unit in computer

Constant

Integer constants---all integers 3,99,107

Decimal constant---All decimals 3.5 100.9

Character constants---a single quotation mark to identify a letter, number, and symbol ' a ' = '

String constants---to identify one or more characters with double quotation marks "abc" "234" "Q2" ""

Boolean Constants---Used to represent logical values---true/false

EMPTY constant---NULL

5-Integer, 5.0-decimal ' 5 '-character "5"-string ' 5.0 '-Error "5.0"-string

In-process

Binary: Full binary one, 0~1 1+1=10 0b10011 0b0011, starting from JDK1.7, allow 0b as the beginning to identify a number is a binary number

Octal: Full eight in one, 0~7, 7+1=10 requirements must begin with 0 06 015

Decimal: Full 10 in one, 0~9

Hex: Full 16 in one, 0~9,,a~f,9+1=a f+1=10 requires 0x as the beginning 0x5 0xad

Conversion of the binary

Decimal to binary: continuously divided by 2, then the remainder is inverted

Binary conversion to decimal: From the low, multiply by the order of precedence by 2, and then sum

Binary conversion to octal: Starting from the low, each three bits divided into a group, less than three bit 0, the generation of an octal number, the numbers are ordered by order

Octal into binary: One change three---an octal number produces three-bit binary digits

Binary conversion to 16 binary: Four process of changing one

Variable

System.out.println (i);

int i = 5;---Not---variables must be declared before using the

int i;

System.out.println (i);---not----variables must be initialized before they can be used

Data type base data type

Numeric type

Integer type

BYTE---byte---1-byte----2^7~2^7-1----128~127

byte B = 5; byte b2 =-128;

Short------2-byte----2^15~2^15-1----32768~32767

Short S = 54; Short s =-900;

int---integer---4-byte----2^31~2^31-1

int i = 100000;

Int J = 100_000_000;--is allowed starting from JDK1.7. These _, Int j = 100000000 are automatically ignored at compile time;

int i = 00001111;---octal

Integer default type in Java is int

Long---Length---8 bytes----2^63~2^63-1---with L as the end identifies the number as a long type

Long L = 3L;

Floating point Type

FLOAT---single precision---4 bytes---must end with F

float F = 3.2f;

Double---doubles---8 bytes

Java decimal Number default is double type

Double d = 3.5;

Double d = 4.6D;---can

Double d = 3.9e4; is the scientific notation of the decimal

Double d = 0x3p2; is the hexadecimal science notation--12

Character type

Char---2 bytes---0 ~65535

char c = ' a ';

char c = ' Medium ';

Boolean type

Boolean---True/false

Boolean B = false;

Reference data type

Classes---class interface---interface array---[]

Conversion of data types

Implicit conversion/automatic type conversion

byte B = 100;

int i = b;

Long L = N;---can---when the value of an integer is within the range of type int, you can not add the end of L

Rule one: Small types can be converted to large types---byte->short->int->long float->double

int i = 5;

float F = i;

Long L = 6;

float f = l;

Rule two: integers can be converted to decimals, but may result in loss of precision

char c = ' a ';

int i = C;

Rule three: Character type can be converted to integer type

Short s = ' a ';---can

char c = n;---can

char c = ' a ';

Short s = C;---can not

Defines the char type of variable c, the stored data is a character, do not need to check the specific character encoding, when assigned to the short type, short needs to check whether the encoding of the character is within the range of short type, this time can not determine the character corresponding to the specific code, Because the range of values for the short type is not fully coincident with the char type, the assignment is not allowed to prevent the case from exceeding the range.

Short S = 97;

char C = s;--not possible

Explicit conversion/coercion type conversion

Long L = 54;

int i = (int) l;

Double d = 3.5;

int i = (int) d;---decimal is strongly turned into an integer, the fractional part is discarded directly

Double type cannot store decimals accurately

hexadecimal--Hex

decimal--Decimal

Octal---octal

binary--binary

Operator arithmetic operators

+ Plus-minus * multiply/divide% modulo + + self-increment-subtract + string concatenation

int i = 5210/1000 *,--->i = 5000;

Attention:

1. The result must be an integer after the integer operation is complete

2. Integer divided by 0, compile pass, run error---arimeticexception---arithmetic exception

3. The result of dividing the decimal by 0 is infinity

4. The result of 0/0.0 is Nan---not a number---non-numeric

5. The Byte/short type is automatically promoted to int type at the time of operation

% take remainder operation

-5%3=-2-4%3=-1-3%7=-3

5%-3=2 7%-2=1 2%-8=2

-5%-3=-2-9%-3=0

For the negative number of the remainder, first in accordance with the positive of the remainder operation, to see the remaining symbol to the left of the symbol, if the left side is negative, then the result is negative

5%1.2=0.2 6%1.3=0.8

4.3%1.4=0.1

++/--

For + + on the original basis of the 1 self-increment

int i = 5;

int j = ++i;---> I self-increment 1, then assign the value of I to j---increment first, then calculate

int j = i++;---> Get the value of I first 5,i increment to 6, then assign the obtained value 5 to J---first, then increment

int i = 3;

Int j = ++i * 2;-> j = 8;

Int j = i++ * 2;->j = 6

int i = 6;

Int J = i++ + ++i;->i = 8; j = 14;

Int J = ++i + i++;->i = 8; j = 14

byte B = 5;

b++;---The JVM will cast the result in the underlying type, converting the result to byte type

char c = ' a ';

System.out.println (c + 4);--Can

char c2 = ' d ';

SYSTEM.OUT.PRINTLN (c + C2),---promoted to int type before operation

+ string concatenation operation

"A" + "B"---> "AB"

"A" + 3---> "A3"

"A" + true-> "Atrue"

2 + 4 + "F", "6f"

"F" + 2 + 4-> "F24"

Assignment operators

= + = = *=/=%= &= |= ^= <<= >>= >>>= ~=

int i= 5;

i + = 3; i = i + 3; -i = 8;

I-= 2;-> i = i-2;-> i = 3;

Int J;

j + = 4;---No.

int i = 5;

i + = i = i *= 5;--> i =-15;

i = 5 + (5-(5 * 5));

i + = i-= i *= ++i;--->i = 20;

i + = i*= i-= (i++ +-i);---> i =-20;

i = 5 + (5 * (5-(5 + 5)));

byte B = 5;

B + = 3;---can

byte B = 125;

B + = 3;---can----128

comparison/relational operators

= = equal! = unequal > < >= <= instanceof

3 = = 4;-> false

instanceof---Judge the relationship of an object to a class--can only be used for reference data types

String s = "abd";

System.out.println (S instanceof String);---True

System.out.println ("def" instanceof String);---True

logical operators

Used for arithmetic logical values

& with | or! Non-^ XOR && short circuit with | | Short Circuit or

True&true=true True&false=false False&true=false False&false=false

True|true=true true|false=true false|true=true False|false=false

!true=false!false=true

True^true=false true^false=true false^true=true False^false=false

For &&, if the value of the previous expression is false, then you can determine that the value of the entire expression is false,&& and the operation behind it is no longer

Ternary/three mesh/conditional operator

Logical value? Expression 1: Expression 2

If the logical value is true, the expression 1 is executed, whereas the expression 2 is executed.

int i = 5, j = 7;

i > J? System.out.println (i): System.out.println (j);---not! The ternary operator must have the result after the operation is complete!

Double d = i > J? I * 2.5:J;---The return value type of two expressions is either consistent or compatible

Getting data from the console

Import Java.util.Scanner; Written under the package, above class

Scanner s = new Scanner (system.in);

int i = S.nextint ();

Double d = s.nextdouble ();

String str = s.nextline ();

String str2 = S.next ();

Basic data types and operations for 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.