Java Learning Notes (ii)----BASIC data type Application

Source: Internet
Author: User

& and && and difference,&& if the equation is not set up on the short circuit, and no matter the left of the formation, the right equation is executed
&& short circuit with, | | Short Circuit or
| Or, both sides of the expression are operating regardless of the result

Float f=3.5 wrong, because 3.5 is double type
Float f=3.5f to
All of the byte, short, and char values are promoted to int in an expression

Note: In an expression, whichever is the highest type of variable or number, its next variable or value is converted to the type of the variable or value

BYTE A=1;
a=a+1; Wrong because the result of A+1 has been converted to int, so a can't receive int type
System.out.println (' a ' + 1); The output is 98, because the ' a ' is automatically promoted to integral type.
System.out.println ("" + ' a ' + 1); The output is A1 because in an expression, if a string is added with the + number and other data types, the string is actually concatenated with the other content to generate a new string.

int x=5,y;
Y=x%-2; Y=1
y=-x%2//Y=-1


Switch
The selector in the switch must be an int or a char type, such as:

float i = 2;
switch (i)//will be faulted because I is not one of the int or char

The output range of math.random () is [0, 1]

Algorithm 1:
    a training center to arrange a room for the new students, assume that there are X students, each room can live 6 people, let you use a formula to calculate the number of rooms they want to live.
      Answer: (x+5)/6. This algorithm can also be used to view the paging display of the message board: where x is the total number of messages, 6 is the number of messages per page, the result is the total number of pages.
//If it is x/6+1, then if the problem is 30, there should be 5, but this algorithm is 6.

Algorithm 2:
x=0;
while (true)
{x= (x+1)%10
SYSTEM.OUT.PRINTLN (x);
}
The algorithm realizes 0~9 continuous cyclic output
--------------------------------------
instanceof Check if it is an object of the class
----------------------------------------
<< left to right 0 (left 1 bit is *2)
>> Right If the left is moved out of place, the original is 0 to make up 0, the original 1 on the 1 (right to move one is/2)
>>> unsigned Right Move no matter what the top number is, always fill 0
operands to int type will be automatically converted to int type and then shifted (Byte,short,char)
The displacement of int cannot exceed 32 digits, a>>33 and a>>1 result is same, a>>32 result is same as original
Long shift cannot exceed 64 digits
y=10
System.out.println (integer.tohexstring (y));
Integer.tohexstring (y) converts an integer y to a hexadecimal number

System.out.println (Float.intbitstofloat (0x50000000)); Prints a floating point number with a 16 in. 50000000.

Integer type, the maximum can move 31 bits, if greater than 31 bits, the shift of the number of modulo, and then move, if the a>>32 bit equivalent to a shift 0 bit, and a>>33 bit, the equivalent of a shift 1 bit, because the plastic has 32
Long plastic shift cannot exceed 64 digits
The shift does not change the value of the variable itself. If a>>1; exist alone in a single line of statements, meaningless

X<<2 equivalent to x* (2 of 2 times)
X>>2 equivalent to x/(2 of 2 times)

---------------------------
for (i=0;i<10;i++)
{System.out.println (i);}

The For statement can be written as
int i=0;
for (;i<10;)
{System.out.println (i);
i++;
}
can also be written
int i=0;
for (;;)
{if (i>=0)
Break
System.out.println (i);
i++;
}
-------------
St:while (True)
{while (true)
{Break St; Jump out of the while loop
}
}

-----------------------------
After the array is defined, the default initial value is 0
int x[]=new int[5];
int x[]={3,4,5}//To assign an initial value to an array
int X[]=new int[]{3,4,5}

int a[5]; When declaring an array, you cannot specify its length, and an error occurs at compile time

------------
Int[] x;
X=new int[100];
X=null;
x[0]=2; The run-time error occurs because X is already pointing to null, so X is no longer pointing to heap space and cannot assign a value to X[0]
System.out.println (x.length); To find the length of an X array

Int[][] Xx={{3,2,7},{1,5},{6}}; The initial initialization of two-dimensional arrays

------------------
Int[] X=new int[]{3,1,2,4};
Java.util.Arrays.sort (x); To sort an array of x
Int[] Y=new int[10]; This y must be first made, below to test the beta
System.arraycopy (x,0,y,0,4); Starting with the No. 0 element of X, the No. 0 element of y begins, and the 4 elements in X are examined.

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.