Basic Java Basics Essay

Source: Internet
Author: User

The difference between 1.& and &&

Same: Both can be used as logical and (and) operations , and both sides of the operator are true when the expression is true.

Different:&& have a short-circuit function, that is, when the first expression is false, the second expression is no longer judged, such as an if (Str!=null &&!str.equals ("")) expression,                When STR is null, the subsequent expression will not be executed due to a short-circuit, so nullpointerexception is not reported. Conversely, if you use &, you will throw nullpointerexception anomalies.

& can be used as a bitwise operator , and,& represents a bitwise operation when the expression is not on either side of the Boolean type. For example 0x31 & 0x0f result is 0x01.

How do I jump out of the current multiple nesting loops in 2.java?

Break: Jump out of the current loop

Continue: Jump out of this cycle

To jump out of multiple loops, you can define a label before the entire loop statement, and then break to the label when a condition is met. For example

OK:

for (int i=0;i<10;i++) {

for (int j=0;j<10;j++) {

if (i==5) break OK;

}

}

It also allows the result of the outer loop condition expression to be controlled by the inner Loop Body code. For example

int arr[][] ={{1,2,3},{4,5,6,7},{9}};
Boolean found = false;

for (int i=0;i<arr.length &&!found; i++) {
for (int j=0;j<arr[i].length;j++) {
if (arr[i][j]==5) {
Found=true;
Break
}
System.out.println (Arr[i][j]);
}
}

Can the 3.switch statement function on a byte? Can you function on long? Can you work on a string?

Switch (EXPR1), EXPR1 can only be an integer expression or enumeration constant. An integer expression can be an int primitive type or an integer wrapper class. Byte,short,char can be implicitly converted to int, and all of these types can be

Long,string obviously does not meet the requirements.

Special: After JDK 7, switch supports string types. For example:

    

String str= "123";
Switch (str) {
Case "ABC":
SYSTEM.OUT.PRINTLN ("abc");
Break
Case "123":
System.out.println ("123");
Break
Default
SYSTEM.OUT.PRINTLN ("Default");
}

4.short s1=1;s1=s1+1; what's wrong? Short s1=1;s1+=1; what's wrong?

The s1+1 operation result is of type int, then the assignment to S1 is short, and the compiler will report the coercion type conversion error.

+ = is an operator in the Java language, and the compiler makes special processing so that it can be compiled correctly.

Can I store a Chinese character in a 5.char variable? Why?

OK. Because char-type variables are used to store Unicode-encoded characters, Unicode contains Chinese, so you can store Chinese characters

Added: Unicode encoding is two bytes, so the char type also occupies two bytes

  

Basic Java Basics Essay

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.