Exercise 1: need to statically import relevant knowledge
Operators are mostly similar to c + +.
1. Priority
Multiplication and division before addition and subtraction, nothing to say.
Here is the operation + sign of a String. If the + sign after the String class is followed by a non String type, the non String will be converted to String type
2. Alias problem
Constant cannot be an lvalue.
When you assign a value to a basic type, you store the actual value. It should be noted that assignment to an object manipulates the reference of the object.
Exercise 2:
class AFloat{ float num; } public class Test2 { public static void main(String[] args) { AFloat sample1=new AFloat(); AFloat sample2=new AFloat(); sample1.num=42.42f; sample2.num=42f; System.out.println(sample1.num); System.out.println(sample2.num); sample1=sample2; System.out.println(sample1.num); System.out.println(sample2.num); sample2.num=32.9f; System.out.println(sample1.num); System.out.println(sample2.num); } }
Alias in method call
Exercise 3:
class Num{ float a; } public class Test3 { static void f(Num x) { x.a=42.0f; } public static void main(String[] args) { Num temp = new Num(); temp.a=10f; System.out.println(temp.a); f(temp); System.out.println(temp.a); } }
3. About operators
Note: 1. Integer division will directly remove the decimal places of the result rather than rounding.
There is a random class in the book. Random will generate seeds as random numbers according to the current time of the system.
Here, write a question with Rando.
Exercise 4:
import java.util.Random; public class Test4 { public static void main(String[] args) { Random rand=new Random(42); int distance=rand.nextInt(100)+1; int time=rand.nextInt(100)+1; System.out.println(distance); System.out.println(time); System.out.println(distance/(float)time); } }
Here nextInt is left open and right closed.
Next, judge equivalence. For = =, it is to judge whether the references are the same. If you want to compare the actual content of the object, you need to use the equals () method. Most java class libraries implement the equals () method, but their own classes override the equals method.
Exercise 5:
class Dog{ String name; String says; } public class Test5 { public static void main(String[] args) { Dog a=new Dog(); Dog b=new Dog(); a.name="spot"; a.says="Ruff!"; b.name="scruffy"; b.says="Wurf!"; System.out.println(a.name); System.out.println(a.says); System.out.println(b.name); System.out.println(b.says); } }
Exercise 6:
class Dog{ String name; String says; } public class Test5 { public static void main(String[] args) { Dog a=new Dog(); Dog b=new Dog(); a.name="spot"; a.says="Ruff!"; b.name="scruffy"; b.says="Wurf!"; System.out.println(a.name); System.out.println(a.says); System.out.println(b.name); System.out.println(b.says); Dog c=new Dog(); c.name="spot"; c.says="Ruff!"; System.out.println(a==c); System.out.println(a.equals(c)); } }
Logical operators should pay attention to that java and or not can only be applied to Boolean values, and c + + non-0 is really not good. give an example:
i&&j i||j ! I can't
Exercise 7:
class Coin{ boolean isHead; } public class Test7 { public static void main(String[] args) { Coin yuan=new Coin(); int isHeadCount=0; Random rand =new Random(42); for(int i=0;i<100000;i++) { yuan.isHead=rand.nextBoolean(); if(yuan.isHead) { isHeadCount++; } } System.out.println(isHeadCount); } }
Pay attention to the short circuit. For conjunction, if the first one has been judged as false, the latter will not continue. For disjunctions, if the first is true, the subsequent will not run.
4. About constants
Hexadecimal has prefix 0x (or 0x), octal is 0.
Exercise 8:
public class Test8 { public static void main(String[] args) { long a=0xfff; long b=0763; System.out.println(Long.toBinaryString(a)); System.out.println(Long.toBinaryString(b)); } }
Exponential counting method
Exercise 9:
public class Test9 { public static void main(String[] args) { float a=Float.MAX_VALUE; float b=Float.MIN_VALUE; double c=Double.MAX_VALUE; double d=Double.MIN_VALUE; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } }
Bitwise operator
A Boolean value can be treated as a single bit value. It can be bitwise and, or, XOR, but not bitwise non. For Boolean values, bitwise operators have the same effect as logical operators, but short circuits are missing.
Exercise 10:
public class Test10 { public static void main(String[] args) { int a=0xaaaa; int b=0x5555; System.out.println(Integer.toBinaryString(a)); System.out.println(Integer.toBinaryString(b)); System.out.println(Integer.toBinaryString(a&b)); System.out.println(Integer.toBinaryString(a|b)); System.out.println(Integer.toBinaryString(a^b)); } }
Shift operators
< < shift left operator, fill 0 in the low order. > > Signed shift right operator: if the sign is positive, the highest bit will be filled with 0, otherwise it will be 1. > > > unsigned shift right operator. No matter positive or negative, 0 will be inserted in the high bit.
Note here that using unsigned right shift combined with assignment to byte and short operations may get - 1 results. The reason is that they have to be converted to int type, then moved to the right, truncated and assigned to the original type.
Exercise 11:
public class Test11 { public static void main(String[] args) { int a=0xaaaa; while(a!=0) { a>>=1; System.out.println(Integer.toBinaryString(a)); } } }
Exercise 12:
public class Test12 { public static void main(String[] args) { int a=0xffff; while(a!=0) { a>>>=1; System.out.println(Integer.toBinaryString(a)); } } }
Exercise 13:
public class Test13 { static void change(char a) { System.out.println(Integer.toBinaryString((int)a)); } public static void main(String[] args) { change('a'); change('b'); change('c'); } }
String Operators
If the expression starts with a string, all subsequent operands must be of string type.
type conversion operator
Pay attention to narrow conversion and expand conversion. Except Boolean, other basic types can be converted to other basic types
Truncation and rounding
In java, the floating-point number is converted to integer, and the number will be truncated. If you want to round, you need the round () method.
promote
The largest data type in the expression determines the final result data type.
Exercise 14:
public class Test14 { static void contrast(String a,String b) { System.out.println(a==b); System.out.println(a.equals(b)); } public static void main(String[] args) { contrast("hello","hello"); contrast("nihao","hello"); String a=new String("hello"); String b=new String("hello"); contrast(a,b); String c="ok"; String d="ok"; contrast(c,d); } }