Assignment Operators
the assignment operator is to assign the value to the right of the equal sign to the left of the equals sign.
the assignment operators have the following types of values:
= assigns the value to the right of the equal sign to the left of the equal sign,int i = 5; Assigns a value of 5 to the variable i.
+ = int i = 5;i+=5 equivalent to int i = 5,i = i +5; The first operation requires only one operation, and the second operation requires two operations, first calculating the value of the I+5 in the assigned value to i.
-= the same as + = Calculation procedure
*=
/=
%=
It is important to note the difference between the two ways:byte B = 5; b=b+5; This method requires a strong b=b+5 operation, which needs to be written as B = (byte) (b+5); and if it is written directly as byte B = 5;b+=5; There is no need for the first way of the strong turn action, because this way will be automatically cast, its prototype is B = (byte) (b+5), but do not need us to do other things to him.
comparison Operators
also called the relational operator, the data on both sides of the symbol is compared, the data after it is Boolean type, only two results, either true or false.
There are several comparison operators:
= = equals 5==5 true 5==4 false
! = does not equal 5!=4 true 5!=5 false
< less than 5<4 false 5<6 True
> is greater than 5>4 true 5>6 false
<= less than or equal to 5<=4 false 5<=6 True
>= greater than or equal to 5>=4 true 5>=6 false
logical Operators
logical operators, where the data on either side of the operator is Boolean , and the result is necessarily the Boolean type, either true or False .
There are several logical operators:
& with both sides results as long as there is a false, the result is false
| or Both sides result as long as there is a true, the result is true
^ xor or Both sides result the same as false, different is true
! take reverse !true = False!false = True
&& Short and left result false, the result is False, the right does not operate
|| Short Circuit or The left result is true, theresult is true, the right does not operate
Ternary operators
format of ternary operator:
conditional expression ? result 1: result 2;
the result of the conditional expression is true, and the result is 1.
the result of the conditional expression is false, and the result of the operation is 2.
Case: Print out the number of items in stock
The following case is a specific application of the knowledge points in the previous study.
print out the following data:
————————————— Commodity inventory List —————————————
Product Name Dimensions Price Number of stocks
Strip table 1.56 150.5 20
Square Table 1.2 100.0 30
Round Table 1.3 145.5 30
—————————————————————————————————
Total Stock:80
Total amount of merchandise in stock:10375.0
The implementation code is as follows:
/* Case Analysis: 1, The fixed information of the table can be printed directly by the output statement in a format &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;2, product name, size, price, inventory define each variable according to the type of data to assign values 3, total stock count, and the total amount of inventory items can be calculated with the data */ public class inventoryofgoods{ public static void main (String[] args) { // Print header pinning Information system.out.println ("———————————— Commodity inventory List ——————————————"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Product name \ t size \t\t price \t\ T Stock Number "); //define each data variable, product name, size, price, stock count //printing data for long tables String oblongTable = "Long Table"; double oblongsize = 1.56; double oblongprice = 150.5; int oblongStock = 20; system.out.println (oblongTable+ "\t\t" + Oblongsize+ "\t\t" +oblongprice+ "\t\t" +oblongstock); //printing data for square tables String squareTable = "Square Table"; double squaresize = 1.2; double squarePrice = 100.0; int squarestock = 30; system.out.println (squaretable+ "\t\t" +squareSize+ "\t\t" +squarePrice+ "\t\t" +squarestock); //printing data for a round table String circulartable = "Round Table"; double circularsize = 1.3; double circularprice = 145.5; int circularStock = 30; system.out.println (circulartable+ "\t\t" +circularsize+ "\t\t" +circularprice+ "\t\t" + Circularstock); //Print Split Line system.out.println ("—————— —————————————————————————— "); //Total inventory count and total amount int stocksum = oblongstock+ squarestock+circularstock; double pricesum = oblongprice*oblongstock +squareprice*squarestock+circularprice*circularstock; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Total number of stocks:" +stocksum); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Total stock amount:" +pricesum); }} Reference Data Type
l Scanner class
The reference data type differs from the base data type in the definition format of the reference data type: Type variable name = new type ();
How to define a What about variables of type Scanner?
Scanner sc = new Scanner ();
Each reference data type has its own specific function, how do I invoke its function?
Sc. method name ();
The role of the Scanner class: Receiving keyboard input on the console
Implement Steps to Scanner features:
1. Import Scanner package,importing Java.util.Scanner;
2. Create A variable of the Scanner class,Scanner sc = new Scanner ();
3. use variables . method name to invoke the the functionality in the Scanner class.
here is the code implementation of specific methods in the Scanner class:
function implementation of the/*scanner class */import java.util.scanner;public class scannerdemo{ public static void main (String[] args) { //creating variables for the scanner class Scanner sc = new scanner (system.in); //uses variables. Method name invocation function, Nextint (); Receive integer input method and use a variable to receive input data int num = sc.nextint (); //print out the input data &NBSP;&NBSP;SYSTEM.OUT.PRINTLN (num); //next (); Receives a function of a string data and receives it with a variable of type string &nbSp; string s = sc.next (); system.out.println (s); }}
l Random class
the role of the random class: randomly generating random numbers.
steps to implement the Random class feature:
1. Import The Random class package,import java.util.Random;
2. Create A variable of the random class,random ran = new random ();
3. Pass the variable . method name in the way called functions in the Random class.
Here's a piece of code to learn exactly the specific implementation of the Random class feature:
/* Create the Random class, and the implementation of the specific function */import java.util.random;public class randomdemo{ public static void main (String[] args) { //Create a variable for the Random class random ran = new random (); //the method of generating a random number by means of the name of the variable. Nextint (number to manipulate), range is 0 to the range of the number to be specified, Nextint (100), range 0 to 99 //use a variable to receive the resulting random number int Num = ran.nextint ( //); Print generated random number system.out.println (num); //produces a random number nextdouble () of a floating-point type, with a fixed range of 0.0 to 1.0 to 1.0 and receives with a variable of type double double num2 = ran.nextdouble ( ); //print generated random number system.out.println (num2); }}
Java Basics Overview