Java Basics Review the next day

Source: Internet
Author: User

Keywords: variable constant operator

Keywords in 1.JAVA: Words that are given special meanings, which are characterized by all lowercase letters. For example: Class,interface,byte,int,short,void,if,while,break ...

Identifiers in 2.JAVA: Some names that are customized in the program, such as Class name, method name, variable name, and so on.

A. Composition: 26 Letters, Numbers, _ and $.

B. Note: Numbers may not start, and keywords cannot be used. Using keywords will cause an error. Strictly case-sensitive.

C. Specific classifications:

Package name: All letters lowercase, eg. Com.sina.weibo.demo2

Class name/Interface name: First letter capitalized, multiple words, the first letter of each word should be capitalized, eg. Demo2, Arraytest ...

Variable name/method name: First word lowercase, followed by the first letter of each word capitalized, eg. Getmax, etc.

Constant name: All letters are capitalized, the words are connected by _, eg. Xxx_xxx_xxx

Constants in 3.JAVA: values that cannot be changed.

Variables in Java: Changed values.

The use of a variable is a value that must be initialized to it, or has a default initialization value.

Format: variable name of data type = initialization value;

Categories of variables in 4.JAVA:

A. Data type:

Basic data type: Numeric type (integer type: byte/short/int/long; floating-point type: float/double)

Character type (char)

Boolean Type (Boolean)

Reference data types: Classes (Class)/interfaces (interface)/arrays ([])

Integer defaults to: int decimal default: Double

B. Type auto-boost: High end Double/flaot/long Intàbyte/short/char

5. Arithmetic operator: +-/*% + +--

Note the point:

A. When a negative number is taken, the minus sign can be ignored, since the modulus is positive

b.+= such a connected operator, can automatically improve the data type, eg, short s = 3; S=s+2;s+=2: There is a big difference between the two. The above code compilation does not pass, and the following is automatically converted, is possible.

6. Logical operators in Java:& (and) | (or) ^ (XOR) && (short circuit) | | Short Circuit

If the results are the same on both sides of the ^:^, the result is false. True if the results are different on both sides.

&&: When the left result is false, the right side does not participate in the operation, and the result is false. (one side participates in the algorithm)

| |: When the left result is true, the right side does not participate in the operation, and the result is true. (one side participates in the algorithm)

^ Special points: When a number is different or the same number two times, the result is the number itself. Eg, (a^b) ^b=a

7. Move left and right to:<< and >>, multiply by 2, divide by 2

8.for and while:

A. Main difference: When a variable exists as a loop increment, the volume in the For loop dies from memory as the loop ends, but it can continue to exist and be used in the while loop, that is, it is not extinct in memory.

B. The simplest form of an infinite loop: while (ture) {} for (;;) {}

C. Ternary operators are shorthand forms of if-else. But what's the difference?

Not all if else can be simplified. Why is it? Because the ternary operator must have the result after the operation!

The difference between 9.If and switch:

If can be used to determine the value, you can also judge the interval, as long as the result of the operation is a Boolean type, can be judged.

Switch is used to judge a fixed number of values. The type of the value being judged is limited.

10. Practice Exercises:

A. Use the while loop to calculate the odd and, even, and the numbers within 100, respectively

public static void Getsum () {

int i = 0;
int sumodd = 0;
int sumeven = 0;
While (i<=100) {
if (i%2==0) {
Sumeven + = i;
}else{
sumodd + = i;
}
i++;
}
System.out.println ("Odd in 100" and "+sumodd");
System.out.println ("Even in 100" and "+sumeven");
}

B. Output 99 multiplication table

public static void getcfb (int num) {

for (int i=1;i<=num;i++) {
for (int j=1;j<=i;j++) {
System.out.print (j+ "*" +i+ "=" +i*j+ "\ t");
}
System.out.println ();
}
}

C. Programming: 1+ (1+2) + (1+2+3) +......+ (1+2+3+...+100)

Public static void getsum (int num) {

int sum = 0;
for (int i=1;i<=num;i++) {//2
for (int j=1;j<=i;j++) {
sum + = j;
}
}
System.out.println ("sum =" +sum);
}

D. Programming: 1!+2!+3!+4!+......+15!

//1*1+1*2+1*2*3+1*2*3*4+...+15!
Public static void getadd (int num) {
//A variable argument is not enough
int result = 0;
for (int i=1;i<=num;i++) {
int sum = 1;
for (int j=1;j<=i;j++) {
sum *= J;
}
result + = sum;
}
System.out.println ("sum =" +result);
}

Java Basics Review the next day

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.