Java Notes (iii)

Source: Internet
Author: User

Windows shortcut keys

Win + R run
Win + L Lock current Account
Win + D display desktop
ALT + TAB multi-task switching
F2 renaming

MyEclipse Common shortcut keys

F11 Run
CTRL + M maximizes the current window
CTRL + Z Undo
CTRL + Y restore Undo
CTRL + O opens the outline view of the class, equivalent to outline
CTRL + SHIFT + H Quick Search for a class (api+ workspace)
CTRL + SHIFT + R Quick Search for a class (in the current project)
Ctrl + D Delete when moving forward
CTRL +/Add/Remove single line comments
CTRL + SHIFT +/Add multiline comment
CTRL + SHIFT + \ Remove Multiline comments
CTRL + SHIFT + O Import all classes used
CTRL + S Save
CTRL + SHIFT + F formatting code
CTRL + F Find/replace
ALT + SHIFT + R Unify rename selected variable name (or right-click Refactor-Rename)

Attention:
1. Only compiled code can be formatted
2. Not only the class can be formatted, the package can also be formatted, and even the project can be formatted
3. Set formatting code without formatting document comments
Windows--Preferences, Java---Code Style--Formatter
Click Edit, click the Comments tab, remove all of the tick-and rename configuration save

ALT + Left and right keys to switch the Open class back and forth
SHIFT + up or down key to select a block of code
CTRL + ALT + up and down keys to copy the cursor in the row

Focus:
Alt +/Smart Editing assistant (the code and the corresponding tips, the use of very high frequency!)
Main Method Main
Output Statement Syso

Each time we finish writing a piece of code, we have to perform the trilogy:
1.ctrl + S
2.ctrl + SHIFT + F
3.ctrl + S
In this way, both the correctness of the code and the format specification of the code can be ensured.
--------------------------------------------------------

There are two ways to generate random numbers in Java:
1.math.random ();
The return type is double and the range is [0,1]
such as: Generate a random number between 1-100
int result = (int) (Math.random () *100 + 1);

Java.lang.Math, a mathematical class that provides a lot of mathematical computing methods (static methods),
such as absolute value, maximum, random number, square root, power operation ...


2. Through the Java.util.Random class
Guide Package
Import Java.util.Random;
Creating objects
Random rd = new random ();
Calling methods
Rd.nextint ();//Generate a random number in the INT range
such as: Generate a random number between 1-100
int result = Rd.nextint (100);

Note: Absolute randomness does not exist, and Java produces random numbers that are pseudo-random numbers!

-----------------------------------------------------------------

Formatted output for Java

Standard syntax:
%[argument_index$][flags][width][.precision]conversion
Note:
Width indicates the length of the variable
Flags represents the marker bit,-the left alignment, the default is right-justified
. Precision represents the precision of floating-point numbers, which is determined by width

System.out.printf (String Format,object...args);
SYSTEM.OUT.PRINTLN (name + "s birthday is" + birthday + ");
System.out.printf ("The birthday of%s is the%s number. \ n", name,birthday);

\ n = line break
%s stands for string
%d represents integers
%f representing floating-point numbers
%b represents a Boolean value

Note the point:
1. Sorting Issues for variables
2. The type of variable determines which placeholder to use
--------------------------------------------------------

Definition of a method

Steps to write a class
Package xxx.xxx.xxx;

import xxx;

public class Xxx {
Method
}

Grammar:
Modifier returns the type method name ([parameter list]) [The exception that can be thrown]{
Method body;
}

The current format of our methods:
public [static] return type method name ([parameter list]) {}


Normal modifier: Static,final,abstract ...

Access Control Modifiers:
Public means publicly, public, and this method can be used by all classes
Protected
[Default] defaults, nothing to write
Private privately owned


return type:

1) void indicates that the return type of this method is void, but no return value
2) can be a basic type (8 kinds), can be an object type


How do I choose the return type of a method?
A: If the method is called to output or handle something, it is defined as void;
If the method is called in order to obtain a specific value or object, without concern for the value or
The subsequent processing of the object is defined as the appropriate type.


Note:
A. The return value and return type must be consistent (except for automatic type conversions)
B. In the method body, return the value by return
If the return type of a method is not void, then there must be a return statement in the method body
If a method has a return statement, then this method is not necessarily void.


Method Name:

Identifiers--variables, method names, class names
(1) Naming rules for identifiers (must be followed):
<1> must consist of letters, numbers, underscores, and $ symbols. (The first letter cannot be a number)
<2> identifiers are case sensitive
<3> identifiers cannot be Java keywords + 3 literals (Null,true,false)
+ 2 reserved Words (goto,const)
(Special case: string is not a keyword)
The length of the <4> designator is unlimited
<5> identifiers support Chinese characters, but not recommended, garbled problems may occur

(2) Naming specification for identifiers (industry specifications, as far as possible)
<1> class name, interface name, first letter of each word is capitalized
<2> method name and variable name, first letter lowercase, first letter of each word capitalized
(Special case: except construction method)
<3> package name All in lowercase letters, in the middle. Separate
<4> constants are all in uppercase letters and are separated by _ in the middle
Final int row_each_page = 10;
<5> advocates for the famous know-how


Parameter list:
Provide the "raw material" required to complete the method

public class Calc {
Called by
public static void Main (string[] args) {
Calc cc = new Calc ();
int result = CC.PLUS2 (3,4);
SYSTEM.OUT.PRINTLN (result);
}

Common methods
public void Plus (int i,int j) {
System.out.println (i + j);
}
public int Plus2 (int i,int j) {
return i + j;
}
}

Formal parameters: The parameter defined in the method is the formal parameter, there is no real value
Arguments: When a method caller invokes a method, the passed argument has an actual value

Local variables:
A variable defined in a method body whose life cycle exists only in the code block in which it resides

Formal parameters are also local variables
----------------------------------------------------------

Condition Judgment:

If condition judgment
Grammar:

Conditional filtering
if (condition) {
Code
}

Branch judgment
if (condition) {
Code 1;
}else{
Code 2;
}

With a branch
if (condition 1) {
Code 1;
}else if (condition 2) {
Code 2;
}else if (condition 3) {
Code 3;
}
.......
}else{
Code N;
}

Attention:
1. The conditions are best mutually exclusive
The 2.else if statement can be any number of
3. The condition must be of type Boolean, it can be a variable, or it can be an expression
4.if Else statements can be nested using
5. When the execution statement has only one sentence, the if curly brace can be omitted
------------------------------------------------------------------

Switch statement
Grammar:
Switch (variable) {
Case value 1:
Statement 1;
[Break;]
Case Value 2:
Statement 2;
[Break;]
.....
[Default:]
Statement N;
}

Note:
A. Inside the parentheses is a variable, it has only 6 types (string is added when JDK7.0)
Byte,short,int,char,string,enum (enumeration)

B. When the value of the case statement is the same as the switch variable, enter the appropriate case
Statement block, and execute code, encounter break, exit switch, otherwise, always to
Run to the end of the code until it encounters a break or runs.

C. When the value of the case statement is not equal to the switch variable, the default statement block is executed,
The prerequisite is that the default statement block exists.

D.default code block can be omitted, and position can be arbitrarily placed


Summary of condition judgment:
If else and switch cases can be common in most cases, both
The choice is based on the business, but if else is more widely used.

---------------------------------------------------------

Looping statements
1.for Cycle
Grammar:
for (expression one; expression two; expression three) {
Circulation body;
}

Description
Expression One, initialize statement, execute only once
Expression two, conditional judgment statement, condition for loop termination, at least once
Expression three, variable factor, for loop each round and previous round change, may execute, may not execute

The three parts can be made up of multiple statements or omitted

such as: Cycle 10 times
for (int i = 0;i < 10;i++) {}

If all three expressions are omitted, it becomes a dead loop
for (;;) {} is exactly equivalent to while (true) {}

Again such as:
int i,j;
for (i=0,j=0;i<10&&j<5;i+=2,j++) {}


The For loop can be nested using the

The outer for loop executes once, and the inner for loop may execute all or be associated with the outer layer.

Note: The nesting of the For loop can be unlimited, but generally not more than 3 layers, because it is extremely
Impact efficiency.

-----------------------------------------------------------------
2.while Cycle
Grammar

while (condition) {
Circulation body;
}

Note:
A. The condition must be a variable of type Boolean, an expression or a compound statement
B. If the condition is never true, then add the break statement in the loop to exit the loop

Such as:
int i = 0;
while (I < 10) {
Circulation body;
i++;
}

If you cannot determine the number of cycles, you can write a dead loop
while (true) {
Loop body
...
if (condition) {
break;//Exit Loop
}
}

Note: For loops and while loops are post loops, which means that the condition is judged first
The loop body is then executed, and then the variable factor is changed.
---------------------------------------------------------
3.do while loop
Grammar:
bo=
Circulation body;
}while (conditions);

Note:
A.do While is a pre-loop, cycle first, then make conditional judgments
B. The condition must be of type Boolean
C. If the condition is always true, then join the break statement in the loop to exit the loop

Such as:
bo=
ShowMenu ();
int choice = Sc.nextint ();
...
}while (choice! = 4);
Get input from the keyboard as long as it is not 4,do while it does not exit

--------------------------------------------------------

Break statements and Continue statements

The break statement exits the current loop or switch statement, and if the loop is multiple nested,
Then break can only jump out of its own layer of the loop.

Use place: loop or switch statement
Break is encountered, the current loop is interrupted directly, and if the loop is outermost, the program stops


Continue means exiting the loop and continuing with the next loop, which can only be used in
In a loop, the statements following the continue are not executed until the next round of loops begins.

Use place: loop
Encountered continue, this round of the current loop skips, but will continue to perform the next round

Java Notes (iii)

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.