Java Interview-Java Overview and Basics (2)

Source: Internet
Author: User
Tags arithmetic operators bitwise operators case statement

Basic data types

In data types, the most common and basic data types are called basic data types. You can use these types of values to represent some simple states.

There are 8 basic data types for the Java language, the following are the 4 categories that are divided by purpose:

Fixed-point type:
An integral type is a class of types that represent integer values. When you need to represent a value for an integer, you can choose from 4 types as needed, and if there is no special requirement, the int type is generally selected. 4 types of integers differ mainly in the amount of space each data occupies in memory and the range of values represented.

Floating-point types:
A decimal type is a class of types that represent small values. When you need to represent a decimal value, you can pick the appropriate one from the following 2 types as needed. If there is no special requirement, the double type is generally selected.
Because decimals are stored in different ways than integers, decimals have a certain precision, so they are not accurate in computing. Two types of decimals are designed according to the difference between precision and storage interval.

Character type:
The character type represents a specific character, as described earlier, the computer is in the form of character sets to save characters, so the value of the character type is actually just the number in the character set, rather than the actual character represented by the computer to complete the work of converting from number to corresponding character. For more convenient internationalization in the Java language, the Unicode character set is used as the default character set, which contains characters that are common in various languages. In program code, characters use a pair of single quotes plus characters that need to be expressed to identify, for example
such as ' A ', ' a ' and so on, of course, can also directly use the character encoding, that is, a non-negative integer to represent.

Boolean type:
Boolean represents the formation and the non-establishment of logic. The use of the keyword true in the Java language means that the false representative is not valid. Boolean is the type of stored logical values, in fact, many programs have the concept of logical values, Java to the logical value of the Boolean type to express.

14, the basic data type conversion of the upward transformation and down conversion

Convert up:

Integer, character, and floating-point data are converted to each other in a mixed operation, and the following principles are followed when converting:

Small-capacity types can be automatically converted to large data types;

Byte,short,char→int→long→float→double

Byte,short,char do not convert to each other, they are first converted to int type when calculating.

The Boolean type is not convertible to other base data types.

Eg:

int i = 123;

Long L = i; Automatic conversion, no need for strong turn

float F = 3.14F;

Double d = f;

Down conversion:

Integer, character, and floating-point data are converted to each other in a mixed operation, and the following principles are followed when converting:

Small-capacity types can be automatically converted to large data types;

Byte,short,char→int→long→float→double

Byte,short,char do not convert to each other, they are first converted to int type when calculating.

The Boolean type is not convertible to other base data types.

Eg:

Long L = 123L;

int i = (int) l;//must be strongly turned

Double d = 3.14;

float f = (float) D;

My Summary: Type conversions

Small turn big, automatic! Automatic type conversion (also known as implicit type conversion)

Big turn small, strong turn! Coercion type conversions (also called explicit type conversions)

15. Java operator

An operator is a special symbol that represents the operation, assignment, and comparison of data in a number of ways:

Arithmetic operators (+ 、— 、 *,/,%)

Assignment operators (=, + =,-=, *=,/=,%=)

Relational operators (>, >=, <, <=,! =)

Conditional operators (&&, | |,!) &, |, ^)

Bitwise operators (&, |, ^, ~, >>, <<, <<<, >>>)

My summary:

Logical operators are used to concatenate Boolean expressions that cannot be written as 3<x<6 in Java and should be written in X>3 & X<6.

"&" and "&&" difference: Single and when the left, whether true or false, the right side of the operation; double and when, if the left is true, the right to participate 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.

"^" and "|" The difference is that when both left and right are true, the "^" result is false.

16. Expressions and three-mesh operators

is a sequence of meaningful permutations of numbers, operators, numbers, and so on, in order to be able to derive values;

A + b

3.14 + A

(x + y) * z + 100

Boolean b= i < && (i%10! = 0)

The type and value of an expression:

The result of operations on operands in an expression is the value of the expression.

The data type of an expression value is the type of the expression.

The order of operations of an expression

Should be in the order of precedence of operators from high to low;

Operators with the same precedence are in accordance with the prior agreed binding direction;

"Trinocular operator", syntax format:

x? y:z;

where x is a Boolean expression, the value of x is evaluated first, and if true, the result of the entire trinocular operator is the value of the expression Y, otherwise it is the value of Z.

Package Reviewdemo;

Public class Demo2 {

Public Static void Main (string[] args) {

String s = "LZ";

s = (s = = "LZ"?) "Correct": "Error");

System. out. println (s);

}

}

Output: Correct

17. Program Flow Control

Sequential structure

Branching structure (conditional structure)

Loop structure

Control loop structure

Sequential structure:

If there is no process control in the code, the program is supreme and the next line is executed, and the next statement is executed until the end of the program.

If statement:

Basic syntax: if (expression) {method body}else if (expression) {method body}else (method body)

Several forms:

Three different formats:

if (conditional expression) {EXECUTE statement;}

if (conditional expression) {EXECUTE statement;}else{EXECUTE statement;}

if (conditional expression) {EXECUTE statement;}else if (conditional expression) {EXECUTE statement;}......else{EXECUTE statement;}

public class If3

{

publicstatic void Main (String args[])

{

Inti = 3;

if (i > 5)

{

SYSTEM.OUT.PRINTLN ("Variable i is greater than 5");

}

ElseIf (i > 4)

{

SYSTEM.OUT.PRINTLN ("Variable i is less than 4");

}

Else

{

System.out.println ("other");

}

}

}

Switch control statements

Format:

switch (expression)

{

Possible result value of CASE expression 1:

Execute the statement;

Break

Possible result value of CASE expression 2:

Execute the statement;

Break

...

Default

Execute the statement;

break;//the last one is not written.

}

Note:

There is no order between case and default. Executes the first case first, with no matching case value executing default.

Two cases of ending the switch statement: The end of execution of the Break,switch statement is encountered.

If the matching case or default does not have a corresponding break, then the program proceeds from the first match with a case statement and runs the statement that can execute until a break is encountered or the switch ends.

Class Switch2

{

publicstatic void Main (string[] args)

{

INTA = 7,b = 5;

Switch (A-B)

{

case3://the possible values of the expression;

{

SYSTEM.OUT.PRINTLN ("33!");

}

Break

CASE4:

{

System.out.println ("44!");

}

Break

DEFAULT://is not executed then execute the statement!

System.out.println ("other");

}

}

}

NOTE: The switch statement can only use Byte, char, short, int four basic types, and their wrapper classes and enumerations

18, the three major cycle structure:

Used to handle operations that require repeated execution;

Depending on whether the condition is established or not, the number of execution of the procedure paragraph is determined, and this procedure is called the loop body;

While : There is no need to know how many loops are executed beforehand;

Do While : Ibid., only at least once ( first done, then judged) ;

For : need to know the number of cycles;

Loop structure (while& do While)

While statement

Format:

while (conditional expression value is true)

{

Execute the statement;

}

Do While statement

Format:

Do

{

Execute the statement;

}

while (conditional expression value is true);

My summary: Do and The feature is that the loop body is executed at least once, regardless of whether the condition is satisfied.

conditional expressions in loops cannot be written directly to False or direct write result is false the expression, However, you can use variables to pass false value;

Loop structure (for)

Format:

For (initialization expression (1), loop-conditional expression (2), post-loop action expression (3))

{

Execution statement; (4)

}

Order of Execution: (1) → (2) → (4) → (3) → (2) → (4) → (3)

Note:

1, for the 3 expression in the order of the run, the initialization expression is read only once, to determine the loop condition, for the real execution of the loop body, and then execute the loop after the operation of the expression, and then continue to judge the loop condition, repeat to find a process, until the condition is not satisfied.

2, while and for are interchangeable, the difference is that variables defined for the purpose of the loop are freed in memory at the end of the For loop. While the variables used by the while loop can continue to be used after the loop ends.

3, the simplest infinite loop format: while (true), the for (;;), the reason that an infinite loop exists is not knowing how many times the loop is, but depending on some conditions, to control the loop.

Eg:

The number of multiples of the first 5 3 in 100 is calculated by using three kinds of cyclic control.

Class while

{

publicstatic void Main (string[] args)

{

Inti = 1,k = 0;

while (i<=100)

{

if (i%3==0)

{

if (k<5)

System.out.println (i);

k++;

}

i++;

}

}

}

Class Dowhile

{

publicstatic void Main (string[] args)

{

Inti = 1,k = 0;

do{

if (i%3==0)

{

if (k<5)

System.out.println (i);

k++;

}

i++;

}

while (i<=100);

}

}

Class for

{

publicstatic void Main (string[] args)

{

Inti = 1,k = 0;

for (; i<100;i++)

{

if (i%3==0&&k<5)

{

System.out.println (i);

k++;

}

}

}

}

19. Nested Loops and Process Control

Nested loops: Loop inside loop

Assuming that the number of cycles in the outer loop is M, the number of cycles in the loop is n times, then the number of cycles in the inner loop needs M * N times.

Eg: The multiplication table is printed using the nested FOR Loop statement

Class Break1

{

publicstatic void Main (string[] args)

{

for (inti=1;i<=10;i++)//define I and J values must be inside the for loop, otherwise the value can not be evaluated every time you jump out of the loop

{

for (intj=1;j<=10;j++)

{

if (j<=i)

System.out.print (j+ "*" +i+ "=" +i*j+ "");//Small Note: print () parentheses inside

Parameters must be passed, println () Unlimited!

}

System.out.println ();

}

}

}

Process Control

Break statement, continue statement;

Break: Terminates the layer loop;

Continue: Skipping this layer loop

Note:

①: If these two statements leave the application scope, there is no point in existence.

②: This two statement cannot be followed by a statement because it cannot be executed.

The ③:continue statement skips this loop and continues the next loop.

④: The appearance of the label allows the two statements to function on the specified loop.

Eg:

Package Reviewdemo;

Public class Demo2 {

Public Static void Main (string[] args) {

int i = 1;

for (; i < ten; i++) {

System. out. println (i);

if (i = = 8) {

break;//flow control, not fully executed!

}

}

}

}

Java Interview-Java Overview and Basics (2)

Related Article

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.