Java Basics (SHIFT, function)

Source: Internet
Author: User
Tags arithmetic operators bitwise float double modulus

Review:
Java: Virtual Machine sandbox
JRE:JVM + Core Class Library
Jdk:jre + Tools, Javac Java

Set Variable: java_home
Path= search Order. For example (Notepad calc mapaint binary)

Classpath=java the order of the search classes.
Java-classpa directory (d:\java;d:\;.)

Key words
------------
Words that are given special meaning by the Java language. (All letters are lowercase)
Such as: Class interface byte return .....

BYTE//Byte
Short//shorter integer 2 bytes
int//Integral type 4 bytes
Long//L-Integer 8 bytes
float//Float type
Double//Dual precision floating point type
[Access rights control]
Private//Private
protected//Protected
Public//Common

{class function Modifier} attempted
Abstract//Abstraction
Final//FINAL
Static
Synchronized//Sync
Extends//Extensions (Applied in class)
Implements//implementation (implemented in class)
New//Create
This//current
Super//Hyper class, parent class
instanceof//Is XXX instance (object)
Try//attempt
Catch//Capture
Finally//FINAL
Throw//Throw
Throws//when defining a class declaration, an exception is thrown.
Package//Bag
Import//Importing <--> Export
Native//Local
Transient//temporary, momentary, temporary <--> persistent
Assert//Assert

Identifier (name)
----------------
English alphabet
Digital
_   $

Strictly case-sensitive

Naming format:

Package Name: aaa.bbb.ccc.ddd (file-level relationship)
Class name and excuse name: First letter capitalization (hump nomenclature) xxxyyyzzz
Variables and functions: convertstr2int (first letter lowercase, future words capitalized)
Constant: Final String BRAND = "Benz" xxx_yyy_zzz (all uppercase)
Note: Single line//(SQL Comment--)
Multi-Line/* * *
Document Comments (Javadoc extract all document comments)
/**
*
*/

Constants: Classification of constants in Java
1. All integers
byte short int long default is int
2. All decimals
Float double Default double
3. Boolean-type constant,
4. Character Constants (')
Char
5. String Constants ("")
6.NULL constant: Only one value: null

Four representations of integers:
Binary: 0 1 full 2 in 1
Octal: 0-7 full 8 in 1 starts with 0 (usually not, means the method is easy to confuse)
Decimal: 0-9 full 10 in 1.
Hex: 0-9,a-f full 16 in 1 is indicated with the beginning of 0X

2^10=1024 (Byte) =1k
2^20=1024k=1m
2^30=1024m=1g
2^40=1024g=1t

Negative numbers are stored in complement mode: positive negation +1;
Principle: The sum of positive and negative numbers equals 0. Put the 9th bit of the 1 overflow
For example:
0000 0101
1111 1011

For example-61 bytes of representation:
0000 0110 (6)
^
1111 1001
+ 1
1111 1010 (-6)

-1 Negation +6 (Verify positive: Use negative-1 negation)
1111 1010 (-6)
-1
1111 1001
^
0000 0110 (6)

-1 of the expressions:
0000 0001 (1)
^
1111 1110
+ 1
1111 1111 (-1)

-128 Forms of expression

1000 0000 (128)
^
0111 1111
+1
1000 0000 (-128)
Basic data types in Java (primitive)
Default integer int default floating-point number double
Upgrade Automatic conversion Hermit conversion
Degraded cast display conversion

The data type of an expression is automatically promoted
All byte type, short and char values will be promoted to int type
If an operand is long, the result is a long type
If an operand is of type float, the result is a float type
If an operand is of type double, the result is a double type

Example:
Class typedemo{
public static void Main (String [] args) {
int i = 128;
byte B3 = (byte) i;//is not cast here. compiler hint missing precision.
The result is-128???
SYSTEM.OUT.PRINTLN (B3);
}

}
The above uses 2 points of knowledge:
The type of 1:B3 is Byte, which is 1 bytes. The type of i is int is 4 bytes.
Assigning I to B3 requires a cast.
2: The precision after strong turn is missing.
int i:0000 0000 0000 0000 0000 0000 1000 0000 (integer 128)
byte b3:1000 0000 (-128)

Intercept the remaining: 1 bytes or 1000 0000
1000 0000 value =?
1000 0000
^
0111 1111
+1
1000 0000 (-128)
Arithmetic operators: note the problem.
1. Modulus:% 5%-2 = 1; (negative modulus, modulo minus is ignored)
2. "/" division sign, its integers except and decimals are distinguished: divide between integers: leave the fractional portion of the integer part discarded.
For example: int x = 3510; x = x/1000*1000; X results: 3000;
3. "+" In addition to the string to add functionality, but also to convert a non-string into a string.
System.out.println ("5+5=" +5+5)//printing results?
5+5=55
Short S = 3;
s = s +3;//compile pass, type conversion required
s+=3; Can be passed, there is a built-in type mechanism

&& | | There is a short circuit phenomenon. Boolean1 && (| |) boolean2 if boolean1 satisfies the condition, boolean2 does not participate in the operation

^: boolean1 ^ Boolean2 true ^ false true
! !boolean: Reverse operation.
~: Bitwise reversed.

Note: Java can not be written 3<x<6, should be written x>3 & x<6;

The difference between "&" and "&&":
Single &, on the left whether true or false, the right side of the operation.
Double & If the left is true, the right side participates in the operation, if the left is false, then the right side does not participate in the operation.
"|" and "| |" The difference is the same, dual or time, the left is true, the right does not participate in the operation.
XOR (^) with or (|) The difference is that when both left and right are true, the result is false.


Assignment of & Operations permissions: 1111 & 0100 1111 & 1111 & 0010 1111 & 0001

There is no reason for a short circuit in a logical operation:
Generally used in permissions: With maximum permissions (yes (|) or arithmetic rather than simply adding: avoid repetition. ) & Individual permissions. If True (true), then there is permission. If False (false) does not have permissions.
No matter. There are no permissions, both sides are involved in the operation.
Bitwise operations on the binary operation.

Judging structure: if Else
Switch
Supported types byte short int char
End: Encounter break; Or the program runs to the switch end.
Short Case: (Demand judgment month belongs to that season)

Class Seasondemo
{
public static void Main (string[] args)
{
int x = 7;
Switch (x)
{
Case 12://system.out.println ("Winter"), can be omitted
Case 1://system.out.println ("Winter"), can be omitted
Case 2:
System.out.println ("Winter");
Break
Case 3:
Case 4:
Case 5:
System.out.println ("Spring");
Break
Case 6:
Case 7:
Case 8:
System.out.println ("Summer");
Break
Case 9:
Case 10:
Case 11:
System.out.println ("Autumn");
Break
Default
SYSTEM.OUT.PRINTLN ("Non-existent month");

}
}
}
Here, a simple algorithm is used to further simplify the algorithm:
int x = 12;
if (x <1 | x>12) {
SYSTEM.OUT.PRINTLN ("Non-existent month");
Return
}
int y = (x+2)/3;//essence part algorithm.
System.out.println (y);
Switch (y) {
Case 1:
System.out.println ("1 quarter");
Break
Case 2:
System.out.println ("2 Quarter");
Break
Case 3:
System.out.println ("3 quarter");
Break
Case 4:
System.out.println ("4 Quarter");
Break
Default
System.out.println ("No value calculated");
}
int y = (x+2)/3;//essence algorithm?

Cycle
while () {} VS do{}while ();(executed once before judging the condition)

for (initialization expression; cyclic conditional expression; post-loop action expression)
{
Execute the statement; (Loop body)
}

While and for are interchangeable, the difference is that the for variable defined for the loop is released in memory at the end of the For Loop,
A while loop uses a variable that can continue to be used after the end.
Practice using 2 to cycle the print 99

Class Seasondemo
{
public static void Main (string[] args)
{
for (int i = 1;i<10;i++) {
for (int J =1;j<=i;j++) {
System.out.print (j+ "x" +i+ "=" +j*i+ "\ t");
}
System.out.println ();
}
int a=1;
while (A&LT;10) {
The int b =1;//position is important, and the first and int a= 1 are written together. The result: every time b ==a no loops.
In order to cycle every time need to initialize the value of B, can start from 1 cycle, or will always be B ==a;
while (B<=a) {

System.out.print (A + "x" +b+ "=" +a*b+ "\ t");
b++;
}

System.out.println ();
a++;
}
}
}

Functions: (function)
1. A separate procedure
2. Modifier return value function name (parameter type 1 parameter name 1, parameter type 2 parameter name 2,.....) {
function body;
return; Only return values of type void can not write return
}
3. Formal parameters are only valid within functions (like for)
Note: Only functions can be called, and functions cannot be defined inside a function. (cannot be nested)
When defining a function, the result of the function should be returned to the caller, which is handled by the caller.

Overloading of functions (overload)
In the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of the parameter are different.
The return value type is not required, only the type is seen.



Java Basics (SHIFT, function)

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.