Java basics 3: java Basics

Source: Internet
Author: User

Java basics 3: java Basics
1 operator 1.1 comparison operator

  • The result of the comparison operator is of the boolean type, that is, either true or false.
  • The comparison operator "=" cannot be written as "= ".

 

  • >
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (a> B); // false }}
  • <
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (a <B); // true }}
  • =
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (a = B); // false }}
  • ! =
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (! = B); // true }}
  • > =
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (a> = B); // false }}
  • <=
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {int a = 3; int B = 7; System. out. print (a <= B); // true }}
  • Instanceof
Package java003. * Note: */public class OperateDemo {public static void main (String [] args) {String str = "hello"; System. out. print (str instanceof String); // true }}

 

1.2 logical operators
  • & (And), | (OR ),! (Not), & (short and), | (short or), ^ (different or)
  • Logical operators are used to connect two boolean expressions.

 

  • Example
Package java003. * Description: logical operator */public class OperateDemo2 {public static void main (String [] args) {int x = 3; System. out. print (x >=2 & x <= 5 );}}

 

  • & Features:
  • As shown in: Result of expression 1 & Expression 2.
Expression 1 Expression 2 Result
True False False
True True True
False False False
False True False

 

 

 

 

 

  • & Rule of calculation: All false values are true and false values are true.

 

  • | Features
  • As shown in: expression 1 | result of expression 2.
Expression 1 Expression 2 Result
True True True
True False True
False True True
False False False

 

 

 

 

 

  • | Rule of operation: true or false.

 

  • Example:
Package java003. * Description: logical operator */public class OperateDemo2 {public static void main (String [] args) {int x = 5; System. out. print (x> 5 | x <6 );}}

 

  • ! Features
  • As shown in :! Expression
Expression Result
True False
False True

 

 

 

  • Non-calculation rules: Non-false is true, non-true is false.

 

  • Example:
Package java003. * Description: logical operator */public class OperateDemo2 {public static void main (String [] args) {int x = 5; System. out. print (x = 5); System. out. print (! (X = 5 ));}}

 

  • ^
  • As shown in: expression 1 ^ expression 2
Expression 1 Expression 2 Result
True True False
False True True
True False True
True True True

 

 

 

 

 

  • We know that in a computer, 0 indicates false, 1 indicates true. Of course, we need to declare that in java, 0 indicates false, and 1 indicates true. in java, the two are not equivalent.
  • An exclusive or rule: the same value is 0, and the difference is 1. That is, the expressions on both sides of an exclusive or exclusive side. If they are the same, they are false. If they are different, they are true.
Package java003. * Description: logical operator */public class OperateDemo2 {public static void main (String [] args) {System. out. print (true ^ true); // false System. out. print (true ^ false); // true System. out. print (false ^ true); // true System. out. print (false ^ false); // false }}

 

1.3-bit operator
  • <(Left shift),> (right shift),> (unsigned right shift), & (and), | (OR), ^ (exclusive or ),~ (Anti-code)

 

  • Example: 6 & 3, int type data is 32 bits in the memory. For convenience, there is no 32 bits.
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int a = 6; int B = 3; System. out. print (a & B); // 2 }}

  • Example: 6 | 3
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int a = 6; int B = 3; System. out. print (a | B); // 7 }}

 

  • Example: 6 ^ 3
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int a = 6; int B = 3; System. out. print (a ^ B); // 5 }}

  • Example :~ 3
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int B = 3; System. out. print (~ B); //-4 }}

 

  • Example: 3 <2, shifts one digit to the left, multiplied by 2.
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int B = 3; System. out. print (3 <2); // 12 }}

 

  • Example: 3> 2, right shift one digit divided by 2. If the first digit is 0, then when the right shift, the left blank fill 0. If the first digit is 1, when the right shift is performed, fill in 1 on the left. In the computer, 0 indicates a positive number, and 1 indicates a negative number.
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int B = 3; System. out. print (B> 2); // 0 }}

  • Example: 3> 2. whether the first position is 0 or 1, the left blank space is 0.
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int B = 3; System. out. print (B >>> 2); // 0 }}

 

  • Interview Question 1: the most efficient way to calculate 2*8 is equal?
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {System. out. print (2 <3 );}}
  • Interview Question 2: exchange the values of Two integer variables (the third variable is not required)
    • Train of Thought ①. there are drawbacks. If the two integers have too many values, they will be out of the int range and will cause precision problems.
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int a = 2; int B = 3; a = a-B; B = a + B; a = B-a; System. out. print ("a =" + a + ", B =" + B );}}
    • Train of Thought ②
Package java003. * Description: bitwise operator */public class OperateDemo3 {public static void main (String [] args) {int a = 2; int B = 3; a = a ^ B; B = a ^ B; a = a ^ B; System. out. print ("a =" + a + ", B =" + B );}}

 

1.4 ternary Operators
  • Format:
    • (Conditional expression )? Expression 1: expression 2;
    • If the conditional expression is true, the result of the operation is expression 1,
    • If the conditional expression is false, expression 2 is returned.

 

  • Example: calculate the maximum value of two integers.
Package java003. * Description: ternary operator */public class OperateDemo4 {public static void main (String [] args) {int a = 3; int B = 10; int c = (a> = B )? A: B; System. out. print ("maximum value:" + c );}}

 

 

2. Process Control
  • The Process Control of the program is divided:
    • Sequential Structure
    • Branch Structure
    • Loop Structure

 

2.1 ORDERED STRUCTURE
  • Example:
Package java003; import sun. applet. main. * Description: Sequence Structure */public class OperateDemo5 {public static void main (String [] args) {System. out. print ("Haha"); System. out. print ("Haha"); System. out. print (""); System. out. print ("stupid ");}}

 

2.2 branch statements
  • Format 1
If (condition expression) {execution statement ;}

 

  • Format 2
If (condition expression) {execution statement 1;} else {execution statement 2 ;}

 

 

  • Format 3
If (condition expression 1) {execution statement 1;} else if (condition expression 2) {execution statement 2;} else {execution statement 3 ;}

 

  • Example:
Package java003. * Note: if */public class IfDemo1 {public static void main (String [] args) {int x = 3; if (x> 1) {System. out. println ("correct");} System. out. println ("end ");}}

 

  • Example:
Package java003. * Note: if */public class IfDemo2 {public static void main (String [] args) {char gender = 'male'; if (gender = 'male') {System. out. println ("male gender");} else {System. out. println ("female ");}}}

 

  • Example 3:
Package java003. * Note: if */public class IfDemo3 {public static void main (String [] args) {int x = 3; if (x> 1) {System. out. print ("a");} else if (x> 2) {System. out. print ("B");} else if (x> 3) {System. out. print ("c");} else {System. out. print ("d ");}}}

 

  • Exercise 1: Enter the specified number, and then output the corresponding day of the week?
Package java003. * Note: */public class IfTest1 {public static void main (String [] args) {int I = 5; if (I = 1) {System. out. print ("Monday");} else if (I = 2) {System. out. print ("Tuesday");} else if (I = 3) {System. out. print ("Wednesday");} else if (I = 4) {System. out. print ("Thursday");} else if (I = 5) {System. out. print ("Friday");} else if (I = 6) {System. out. print ("Saturday");} else if (I = 7) {System. out. print ("Sunday");} else {System. out. print ("input error ");}}}

 

  • Exercise 2: output the corresponding season Based on the specified month
Package java003. * Note: */public class IfTest2 {public static void main (String [] args) {int month = 3; if (month = 3) | (month = 4) | (month = 5) {System. out. print ("Spring");} else if (month = 6) | (month = 7) | (month = 8) {System. out. print ("Summer");} else if (month = 9) | (month = 10) | (month = 11) {System. out. print ("Autumn");} else if (month = 12) | (month = 1) | (month = 2) {System. out. print ("Winter");} else {System. out. print ("input error ");}}}

 

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.