1 Operator

1.1   Comparison operator

  • The result of the comparison operator is boolean type , That is, either true, Or false.
  • Comparison operator "==" Can not write "=".
  • >
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a>b);//false
}
}
  • <
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a<b);//true
}
}
  • ==
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a==b);//false
}
}
  • !=
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a!=b);//true
}
}
  • >=
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a>=b);//false
}
}
  • <=
package java003;
/**
* 2017/8/31.
* explain :
*/
public class OperateDemo {
public static void main(String[] args) {
int a = 3;
int b = 7;
System.out.print(a<=b);//true
}
}
  • instanceof
package java003;
/**
* 2017/8/31.
* explain :
*/
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 circuit and )、||( Short circuit or )、^( Exclusive or )
  • Logical operators are used to connect two boolean Expression of type .
  • Example
package java003;
/**
* 2017/8/31.
* explain : Logical operators
*/
public class OperateDemo2 {
public static void main(String[] args) {
int x = 3;
System.out.print(x >= 2 && x <= 5);
}
}
  • & Characteristics :
  • As shown in the figure below : expression 1& expression 2 Result .
expression 1 expression 2 result
true false false
true true true
false false false
false true false
  • & The law of operation : A false full holiday , The whole is true .
  • | Characteristics
  • As shown in the figure below : expression 1| expression 2 Result .
expression 1 expression 2 result
true true true
true false true
false true true
false false false
  • | The law of operation : True truth , If it's all false, it's all false .
  • Example :
package java003;
/**
* 2017/8/31.
* explain : Logical operators
*/
public class OperateDemo2 {
public static void main(String[] args) {
int x = 5;
System.out.print(x > 5 || x <6);
}
}
  • ! Characteristics
  • As shown in the figure below :! expression
expression result
true false
false true
  • The law of non operation : If it's not true, it's true , Either true or false .
  • Example :
package java003;
/**
* 2017/8/31.
* explain : Logical operators
*/
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 the figure below : expression 1^ expression 2
expression 1 expression 2 result
true true false
false true true
true false true
true true true
  • We know , In a computer, generally 0 Express false,1 Express true, Of course , What needs to be stated is java I don't use 0 Express false,1 Express true Of , stay java in , The two are not equivalent .
  • The law of XOR : Same as 0, Different for 1. That is, the expressions on the left and right sides of XOR , If the same , Namely false, Different , Namely true.
package java003;
/**
* 2017/8/31.
* explain : Logical operators
*/
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 An operator

  • <<( Move left )、>>( Move right )、>>>( unsigned right shift )、&( And )、|( or )、^( Exclusive or )、~( Inverse code )
  • Example :6&3,int The type of data in memory is 32 position , Here for convenience , There's no painting 32 Yes. .
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int a = 6 ;
int b = 3;
System.out.print( a & b);
}
}

  • Example :6 | 3
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int a = 6 ;
int b = 3;
System.out.print( a | b);
}
}

  • Example :6^3
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int a = 6 ;
int b = 3;
System.out.print( a ^ b);
}
}

  • Example :~3
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int b = 3;
System.out.print( ~ b);//-4
}
}
  • Example :3<<2 , The left one , multiply 2.
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int b = 3;
System.out.print( 3 << 2);
}
}

  • Example :3 >>2 , Move one bit to the right divided by 2, If the first number is 0, When you move right , There is a vacancy on the left 0,; If the first number is 1, When you move right , There is a vacancy on the left 1. Because in a computer 0 It means a positive number ,1 A negative number .
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int b = 3;
System.out.print( b >> 2);
}
}

  • Example :3 >>>2, Whether the first is 0 still 1, On the left, all the vacancies are made up 0.
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
int b = 3;
System.out.print( b >>> 2);
}
}
  • Interview questions 1: The most efficient way to figure out 2*8 Equal to several ?
package java003;
/**
* 2017/8/31.
* explain : An operator
*/
public class OperateDemo3 {
public static void main(String[] args) {
System.out.print(2 << 3);
}
}
  • Interview questions 2: Exchange the values of two integer variables ( You don't need a third variable )

    • Ideas ①, There are drawbacks , If the value of two integers is too large , It will go beyond int Range , It can cause precision problems .
package java003;
/**
* 2017/8/31.
* explain : An 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);
}
}
    • Ideas ②
package java003;
/**
* 2017/8/31.
* explain : An 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 operator

  • Format :

    • ( Conditional expression ) ? expression 1 : expression 2;
    • If the conditional expression is true, The result of the operation is an expression 1,
    • If the conditional expression is false, The result of the operation is an expression 2.
  • Example : Find the maximum of two integers .
package java003;
/**
* 2017/9/1.
* explain : 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(" The maximum is :"+c);
}
}

2 Process control of procedure

  • The process control of the program is divided into :

    • Sequential structure
    • Branching structure
    • Loop structure

2.1 Sequential structure

  • Example :
package java003;
import sun.applet.Main;
/**
* 2017/9/1.
* explain : Sequential structure
*/
public class OperateDemo5 {
public static void main(String[] args) {
System.out.print(" ha-ha ");
System.out.print(" ha-ha ");
System.out.print(" Hee hee ");
System.out.print(" Stupid ");
}
}

2.2 Branch statement

  • Format 1
if( Conditional expression ){
Execute statement ;
}

  • Format 2
if( Conditional expression ){
Execute statement 1;
}else{
Execute statement 2;
}

  • Format 3
if( Conditional expression 1){
Execute statement 1;
}else if( Conditional expression 2){
Execute statement 2;
}else{
Execute statement 3;
}

  • Example :
package java003;
/**
* 2017/9/1.
* explain :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;
/**
* 2017/9/1.
* explain :if
*/
public class IfDemo2 {
public static void main(String[] args) {
char gender = ' male ';
if(gender == ' male '){
System.out.println(" Gender is male ");
}else{
System.out.println(" Gender is female ");
}
}
}
  • Example 3 :
package java003;
/**
* 2017/9/1.
* explain :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 one : The user enters the specified number , Then output the corresponding day of the week ?
package java003;
/**
* 2017/9/1.
* explain :
*/
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 : According to the specified month , Output the corresponding season
package java003;
/**
* 2017/9/1.
* explain :
*/
public class IfTest2 {
public static void main(String[] args) {
int month = 3;
if((month == 3) || (month == 4 )|| (month == 5)){
System.out.print(" In the spring ");
}else if((month == 6) || (month == 7 )|| (month == 8)){
System.out.print(" In the summer ");
}else if((month == 9) || (month == 10 )|| (month == 11)){
System.out.print(" In the fall ");
}else if((month == 12) || (month == 1 )|| (month == 2)){
System.out.print(" In the winter ");
}else{
System.out.print(" Input error ");
}
}
}

java More related articles on foundation three

  1. java Basics ( 3、 ... and )

    1. Enumeration class , Use enum The defined enumeration class inherits by default java.lang.Enum, instead of Object class . All instances of an enumeration class must be displayed in the enumeration class , Otherwise, this enumeration class will never produce instances . There are many related contents , It needs to be followed up ...

  2. java The use and difference of the three basic cycles

    Abstract :Java Some of the new students' understanding when they first learn , Don't spray when the gods pass by , What's wrong? I hope I can give some advice , If you think it's ok , I hope you can like it , Hey , Thank you first here . What I'm talking about here is what I used at the beginning of my study Java Three cycles ...

  3. Java Basics ( 3、 ... and )-final Keyword analysis

    Let's talk about it today final The role of keywords , Although there are many blogs about final We've done a lot of research , But I still need to record and talk about my own opinions to deepen my impression . Let's go straight to the topic : One .final The role of keywords 1. By final The decorated class cannot ...

  4. java Basics ( 3、 ... and )-----java The three characteristics of polymorphism

    There are three characteristics of object-oriented programming : encapsulation . Inherit . polymorphic . Encapsulation hides the internal implementation mechanism of classes , You can change the internal structure of a class without affecting its use , It also protects data . It's just to the outside world. Its internal details are hidden , What's exposed is its access method . Inherit ...

  5. java Basics ( 3、 ... and ): Reflection 、 Deserialization cracking single column mode and solution

    Singleton pattern means that a class has only one object , Through some measures to achieve this goal . But reflection and deserialization can get multiple different objects . Let's start with a simple understanding of the single example pattern One : The singleton pattern Through private constructors , Declare a static object member of the class , Provide a ...

  6. Java Foundation three (Scanner Keyboard entry 、Random random number 、 Flow control statement )

    1. Reference type variable creation and use 2. Selection statement of flow control statement 3. Loop statement of flow control statement 4. Loop advanced ###01 Create reference type variable formulas * A: Create reference type variable formulas * a: What we're going to learn Scanner Class belongs to ...

  7. java Basics ( 3、 ... and ) Reinforced for Circulation and Iterator

    introduction from JDK1.5 rise , Added reinforced for Cyclic grammar , Also known as "for-Each loop ". Enhanced loops add a lot of convenience in manipulating arrays and collections . that , Reinforced for How does the loop work ...

  8. java Basics 3、 ... and Concepts and java Program structure .

    One .java Some of the concepts of : JRE(java  runtime environment):java Program running environment , If you want to run java Program , need jre Support .jre It contains jvm JDK(java  devel ...

  9. Java Summary of interview questions Java Basics ( 3、 ... and )

    1.JAVA How to deal with language exceptions , keyword :throws,throw,try,catch,finally What is the meaning of each of them ? stay try Can an exception be thrown in a block ? answer :Java Object oriented method of exception handling , ...

Random recommendation

  1. 【zz】matlab Difference set

    matlab Judge 2 Different elements in an array --setdiff c = setdiff(A, B)  Back in the A There is , and B There are no values in , The result vector will be returned in ascending order . In set theory ,c = A - B.A and B It can also be a string ...

  2. SQL Server - First knowledge of database

    In the Internet written test , We often encounter database problems , Here is a brief summary , Be careful , With Sql Server Database, for example . database Database system ,Database System, It consists of database and database management system . database ,Data ...

  3. spring Configure files for multiple data sources

    <?xml version="1.0" encoding="UTF-8"?><!-- Repository and Service layer ...

  4. scala vs java Similarities and differences

    This post is my excerpt from foreign websites , Used as a reminder , Also as a share ! Similarities between Scala and Java Following are some of the major similari ...

  5. POJ-2752 Seek the Name, Seek the Fame(KMP, Prefixes are equal to suffixes )

    The question :    Give a string str, Find out str How many substrings exist in , So that these substrings are str The prefix of , again str The suffix . Output the length of these substrings from small to large . This is next Application of array ,next Array is really profound . ...

  6. The new host doesn't have ifconfig,route Wait for the order , How to find the corresponding installation package

    The company has a new host recently , There are no common commands on the host , for instance ifconfig,route Wait for the order . Without these commands, the host is hard to work , So we need to install it The first method : Yes, you need to know the corresponding bag for instance ifc ...

  7. Linux System disk expansion - Physical machines are not virtual machines

    # Linux System disk expansion ## Learn about disk partitioning - `blkid` Check the zoning - `fdisk -l` Partition table ## System mount partition - `/etc/fstab` Start the mount partition ## Two systems down ...

  8. linux --- Deploy front and back end separation projects

    vue + uwsgi +nginx Deploy front and back end separation projects Prepare the project 1. Front end vue Project package and back end django Project package upload server , adopt lrzsz, Directly from windows Drag in linux in 2. Decompression operation front end ...

  9. vscode Shortcut key

    ctrl+/    Single-line comments :// To have multiple lines of comments ://, First select the lines you want to comment , Press again ctrl+/ shift+Alt+A Multiline comment Undo the previous operation :Ctrl+Z  Resume the operation :Ctrl+Shift+Z ...

  10. Docker Use jenkins Deploy java Project to remote linux( 3、 ... and )

    Realization function : From local commit code to gogs On , The local code will be checked   If you pass the test, you can commit success   And then you can go on push command  .push In the after gogs Upper use web The hook is automatically pushed to jenkins Trigger a build ,j ...