Java Basics-Operator

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators logical operators

1. Operators

Operators in Java have the following categories:

    • Arithmetic operators
    • Relational operators
    • logical operators
    • Bitwise operators
    • Assignment operator
    • Ternary operator

1.1 Arithmetic operators

Common operators are: +,-,*,/,%,++,--

1.1.1 Basic Subtraction

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {        int i = ten;         int j = 5;         int A = i+j;         int B = i- j;         int c = i*j;         int d = i/j;    }}

1.1.2 reading data from the keyboard

Need to input data from the console, so you need to use the scanner class, this class in the Java.util package.

You can use the methods in scanner:

    • Next (): reads a row of data
    • Nextline (): reads a row of data
    • Nextint (): reads an integral type of data
    • The same can be other types such as nextfloat () and so on

Example: Enter two numbers from the keyboard and output their and

 import  java.util.*;  public  class   Addnum { public  static  void   main (string[] args) {System.out.print ( "First num:" );  int  num1=new          Scanner (system.in). Nextint ();        System.out.print ( second num: " int  num2=new          Scanner (system.in). Nextint ();    System.out.println ( "The sum is:" + (Num1+num2)); }}

1.1.3 Modulus% and rounding/

int i=13%2; // I=1
int j=13/2;//j=6

1.1.4 self-increment and decrement (++,--)

1 increase or 1 reduction on the original basis

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {           int i = 5;        I+ +;        System.out.println (i); // Output is 6      }}

But the difference between the self-increment and the self-reduction is the pre-and post-position:

Front + +: First +1, after value

Post + +: First value, after +1

 Public classHelloWorld { Public Static voidMain (string[] args) {inti = 5; System.out.println (i++);//Output 5System.out.println (i);//Output 6                 intj = 5; System.out.println (++J);//Output 6System.out.println (j);//Output 6    }}

1.1.6 Small program BMI

BMI is calculated as weight (kg)/(Height * height)

 import   Java.util.Scanner;  public  class   BMI { public  static  void   main (string[] args) {System.out.print ( "Please enter your weight:"         );  float  weight=new   Scanner (system.in). Nextfloat ();        System.out.print ( "Please enter your height:"  float  height=new   Scanner (system.in). Nextfloat ();    System.out.println ( "Your BMI is:" +weight/(Height*height)); }}

1.2 Relational operators

Common relational operators are:, >=,<, <=,, = =,!=

 Public classHelloWorld { Public Static voidMain (string[] args) {intA = 5; intb = 6; intc = 5; System.out.println (A&GT;B);//returns falseSystem.out.println (A&GT;=C);//returns TrueSystem.out.println (A==B);//returns falseSystem.out.println (A!=B);//returns True        }}

1.3 Logical operators

The common logical operators are: &,|,! , &&,| |,^

1.3.1 and, or, non-

And: Only true when they are true

Or: As long as one is true

Non: Take counter

 Public classHelloWorld { Public Static voidMain (string[] args) {//The long road and the value of the first expression, whether true or false, will be evaluated        inti = 2; System.out.println (i= = 1 & i++ ==2);//Anyway, the i++ will be executed, so the value of I becomes 3.System.out.println (i); //Short circuit with the value of the second expression as long as the value of the first expression is false, no operation is required.        intj = 2; System.out.println (J= = 1 && j + ==2);//because J==1 returns false, the J + + on the right is not executed, so the value is 2 .System.out.println (j); }}

1.3.2&&,| |

&&: As long as the first expression is false, then the latter is not calculated, directly false

|| : As long as the first expression is true, it is true and the back is not calculated

 Public classHelloWorld { Public Static voidMain (string[] args) {//A long road or the value of the first expression is either True or false, and the second value will be evaluated        inti = 2; System.out.println (i= = 1 | i++ ==2);//Anyway, the i++ will be executed, so the value of I becomes 3.System.out.println (i); //A short-circuit or the value of the second expression, as long as the value of the first expression is true, does not need to be evaluated.        intj = 2; System.out.println (J= = 2 | | J + + ==2);//because j==2 returns True, the right J + + is not executed, so the value of J is 2 .System.out.println (j); }}

1.3.3 or ^

The difference is true, the same is false.

 Public class HelloWorld {    publicstaticvoid  main (string[] args) {          Booleantrue;         Boolean false ;                 System.out.println (a// different return true        // same return false      }}

1.4-bit operator

The common bitwise operators are: &,|,! ,^,<<,>>

1.4.1-bit or |

0|0=0, the others are 1.

 Public class Wiehuo {    publicstaticvoid  main (string[] args) {        int i =2,j=3;        System.out.println (integer.tobinarystring (i));//10        System.out.println (integer.tobinarystring (j));//11        System.out.println (i| j);//3        System.out.println (integer.tobinarystring (i|  j));//11            }}

Integer.tobinarystring (): Can convert a number to 2 binary

1.4.2-bit and &

1&1=1, other 0

 public  class   Weiyu { public  static  void   main (string[] args) { int  i=5,j=6; System.out.println (integer.tobinarystring (i));  // 101  System.out.println (integer.tobinarystring (j)); // 110  System.out.println (integer.tobinarystring (i&j)); // 100  System.out.println (i&j); // 4  }}  

1.4.3 or ^

The same is 0, the difference is 1.

 public  class   Yihuo { public  static  void   main (string[] args) { int  i=5,j=6; System.out.println (integer.tobinarystring (i));  // 101  System.out.println (integer.tobinarystring (j)); // 110  System.out.println (integer.tobinarystring (i^j)); // 011  System.out.println (i^j); // 3  }}  

1.4.4 left shift, right shift

Shift left: Based on the binary representation of an integer, each of its bits is moved to the left, and the rightmost one is 0
Shift right: Each bit is moved to the right according to the binary representation of an integer

 Public class Weiyi {    publicstaticvoid  main (string[] args) {        int i= 5;        System.out.println (integer.tobinarystring (i)); // 101        System.out.println (integer.tobinarystring (i<<1)); // move left one 1010        System.out.println (integer.tobinarystring (i>>1)); // move right One ten             }}

1.5 Assignment operators

1.6 Ternary operator

Java Basics-Operator

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.