Today, let's talk about the basic data types and the transformation between them;
1, Basic data types
Integer floating point character Boolean
1. integer
In the operation of integer
To make it easier for you to understand, let's lift a chestnut
public class Test01{ public static void main(String[] args){ byte a1=1; byte a2=2; byte a3=a1+a2; System.ot.println(a3); } }
Try to run this code, and you will find that its content is incompatible type, and there may be loss in converting from int to byte. Why does this happen? 1 and 2 default to int type, while a1,a2,a3 is of byte type, so it will report an error. How can we solve this problem? How can we force conversion? Add byte in front of a1+a3. Let's take a look at the modified code.
public class Test01{ public static void main(String[] args){ byte a1=100; byte a2=100; byte a3=(byte)(a1+a2); System.ot.println(a3); } }
After running the code once, you can run the result;
The result of the operation is
-56
We analyze why it is not 200. The value range of Byte is - 128-127200, which has exceeded its range, but it still outputs, which is the result of forced conversion;
200 = 1100 1000, then - 1 = 1100 0111, then reverse = 0011 1000 to get - 56
In the same way, Short and long are the same. Let's elaborate
public class Test02{ public static void main(String[] args){ Short a1=1; short a2=2; short a3=a1+a2; System.ot.println(a3); } }
After running, it still reports an error, and the reason for the error is still: incompatible; to change it to the correct code, the reason is the same as byte, adding short in front of a1+a3 as a whole for forced conversion;
public class Test02{ public static void main(String[] args){ Short a1=1; short a2=2; short a3=(short)(a1+a2); System.ot.println(a3); } }
Therefore, we can sum up the following truth: the default of integer type is Int type. When other types want to output, they must perform forced conversion;
2. floating point type
float type 4 bytes
double8 bytes
The default of floating-point type is Double. When outputting, if it is not Double, it needs to be cast
class test3{ public static void main(String[] args){ float f=3.14; System.out.println(f) } }
This code, obviously, can't be output; the reason for the error is that it's also incompatible. If you want to output it, you need to + F after the constant of float type;
class test3{ public static void main(String[] args){ float f=3.14F; System.out.println(f) } }
So it can be output;
3. character type
char 2 byte 0-65536 why does it have no negative number because the character has no negative
How do computers store characters? Letters, numbers, punctuation;
public class Test4{ public static void main(String[] args){ char c1='a'; System.out.println(c1+1);//A character encoding after 98 C1 System.out.println(c1-1);//96 character encoding before C1 System.out.println((char)('I'+1));//In the dictionary, the word ring behind me } }
4. boolean type
Its running results are only true and false
2, i + + and + + i
For i + + and + + i, i believe we all know that i + + is running first and then + 1, and + + i is running first and then + 1
Let's talk about i + +. i + + is a wolf in sheep's clothing. Before he meets sheep, he is a wolf. After he meets sheep, he shows its original appearance. For example, when running a program, i + + is already 2, but it still has a skin of 1. After any one of the operations (assignment, call, output) is performed, His skin is off. 2 shows it. 2 participates in the calculation.
The academic point is:
1. Open a temporary storage area first
2. Copy the value of I to the store
3.i self +1
4. The value of temporary storage area is waiting to be called (assignment, output, call);
Here, for example
public class Test5{ public static void main(String[] args){ int i=3; i++; System.out.println(i);//4 System.out.println(i++);//4 int j=i++; System.out.println(j);//5 System.out.println(i);//6 i=i++; System.out.println(i);//6 i=i++; System.out.println(i);//6 } }
The first output must be understood by everyone. The second output is 4. When i output, i becomes 5. When j=i + +, i gives 5 to J. therefore, the third output is 5. i also participates in the operation, so the value of i becomes 6 when i output the fourth. i=i + +, i assign 6 to i, so the fifth and sixth outputs are 6;
3, Practice
3.1 (convert Celsius temperature to Fahrenheit temperature) write a program, read in the double type Celsius temperature from the console, then convert it to Fahrenheit temperature, and display the results. Conversion formula: Fahrenheit temperature = (9 / 5) * Celsius temperature + 32;
Steps:
1. Input Celsius temperature
2. Transform according to the formula given by the title
import java.util.Scanner; public class Test1{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please enter the temperature in centigrade:"); double z = input.nextDouble(); double z1 = (9.0/5)*z+32; System.out.println("The temperature is:"+z+"Convert to Fahrenheit at:"+z1); } }
3.2 (calculate the volume of the cylinder) write a program, read in the radius and height of the cylinder, and use the following formula to calculate the volume of the cylinder:
- Area = radius * radius * p
- Volume = area * height
- Steps:
- 1. Enter the radius and height of the cylinder
- 2. Calculate the bottom area according to r*r*h
- 3. Calculate the volume according to the calculated bottom area
-
3.3 write a program to read an integer between 0 and 1000, and add the digits of the integer. For example: the integer is 932, and the sum of the digits is 14.import java.util.Scanner; public class Test2{ public Static void main(String[] args){ Double pi = 3.1415926; Scanner input = new Scanner(System.in); System.out.println("Please enter the radius of the cylinder:"); Double r = input.nextDouble(); Scanner input1 = new Scanner(System.in); System.out.println("Please enter the height of the cylinder:"); Double h = input1.nextDouble(); Double area = r*r*pi; Double V = area * h; System.out.println("The area of the cylinder is:"+area); System.out.println("The volume of a cylinder is:"+v); } }
- Steps:
- 1. Enter an integer
- 2. Integer% 10 to get the number of digits
- 3. Integer / 10% 10 to get tens
- 4. Integer / 10 / 10 to get hundreds
- 5. Add the ones, tens and hundreds
import java.util.Scanner; public class Test3{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Please enter a 0-1000 Integer between"); int N = in.nextInt(); int N1 = N % 10; int N2 = N /10 %10; int N3 = N2%10; int sum = N1+N2+N3; System.out.println("The sum of the numbers is:"+sum); } }
3.4 (current time) program listing 2-7 shows the program that displays the current Greenwich mean time. Modify this program, prompt the user to enter the time zone offset for GMT, and then display the time in this specific time zone.
Step: 1. Get the total number of milliseconds from January 1, 1970 to now
2. Total milliseconds / 1000 get total seconds% 60 get current seconds
3. Total seconds / 60 get total minutes% 60 get current minutes
4. Total minutes / 60 get total hours (total hours + other areas)% 24 get local time
import java.util.Scanner; public class Test4{ public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Please enter the offset of the time zone:"); int timePianyi = in.nextInt(); Long totalMilliseconds = System.currentTimeMillis();/*From 1970 to The number of milliseconds now;*/ long totalseconds = totalMilliseconds / 1000;/*Get the total seconds*/ long currenSeconds = totalseconds % 60;/*Get the current seconds*/ long totalMinutes = totalseconds / 60;/*Get the total minutes*/ long currentMinutes = totalMinutes % 60;/*Get the current minutes*/ long totalHours = totalMinutes /60;/*Get the current total hours*/ long currentHour = (totalHours + timePianyi) % 24; System.out.println("The current time is:"+currentHour+":"+currentMinutes +":"+currenSeconds); } }
3.5
(financial application: compound interest value) suppose you deposit $100 a month into a bank account with an annual interest rate of 5%, then the monthly interest rate is 0.05/12 = 0.004 17.
After the first month, the value on the account becomes:
100 * (1 + 0.00417) = 100.417
After the second month, the value on the account becomes:
(100 + 100.417) * (1 + 0.00417) = 201.252
After the third month, the value on the account becomes:
(100 + 201.252) * (1 + 0.00417) = 302.507
And so on. Write a program to show the amount of money on your account after six months. (in programming exercise 5.30, you will use the loop to simplify the code here and display the account value after any month. )
Data: monthly interest rate is 0.00417. Monthly deposit amount is 100, and it is required to output the total amount of deposit 100 every month
Step: 1. Calculate the first total amount
2. The rest of each month is (amount of this month + total amount of last month) × monthly interest
import java.util.Scanner; public class Test5 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your monthly deposit amount:"); double Z = in.nextDouble(); double rLiXi= 1 + 0.00417; double Month1 = Z * rLiXi; double Month2 = (Z + Month1) * rLiXi; double Month3 = (Z + Month2) * rLiXi; double Month4 = (Z + Month3) * rLiXi; double Month5 = (Z + Month4) * rLiXi; double Month6 = (Z + Month5) * rLiXi; System.out.println("6 After three months, the balance on the account is:" + Month6); } }
3.6 (geometry: distance between two points) write a program to prompt the user to input two points (x1,y1) and (x2,y2), and then display the distance between two points. The formula for calculating the distance between two points: (x2-x1)^2 + (y2-y1)^2 is the square root of the result. Note: you can use Math.pow(a,0.5) to calculate.
Data: coordinates of two points, solving the distance between two points
Step: 1. Input the coordinates of two points
2. Distance formula between two points (x-x1)^2+(y-y1)^2
3. Square root. Here we use Math.pow(, 0.5). This is the square root sign, which is Math.pow(, 2)
import java.util.Scanner; public class Test6{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter x1 and y1: "); System.out.print("Enter x2 and y2: "); double b=input.nextDouble(); double d=input.nextDouble(); double a=input.nextDouble(); double c=input.nextDouble(); double y= (b-a)*(b-a)+(d-c)*(d-c); double distance = Math.pow(y, 0.5);//power square (0.5) System.out.print("The distance the two points is " + distance); } }
3.7 (geometry: area of triangle) write a program to prompt the user to input three points (x1,y1), (x2,y2) and (x3,y3) of the triangle, and then display its area. The formula for calculating triangle area is:
s = (side 1 + side 2 + side 3) / 2
Area: (s * (s-side 1)(s-side 2)(s-side 3)) ^ 0.5
Data: coordinate of three points to find the volume of triangle
Step: 1. Input the point coordinates of three points
2. Find the distance between two points and get the length of three sides
3. Calculate the side length of the triangle
4. Calculate the area of triangle according to the formula between side length and area
import java.util.Scanner; public class Test7{ public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter x1 and y1: "); System.out.print("Enter x2 and y2: "); System.out.print("Enter x3 and y3: "); double b=input.nextDouble(); double d=input.nextDouble(); double a=input.nextDouble(); double c=input.nextDouble(); double e=input.nextDouble(); double f=input.nextDouble(); double y= (b-a)*(b-a)+(d-c)*(d-c); double y1= (b-e)*(b-e)+(d-f)*(d-f); double y2= (a-e)*(a-e)+(c-f)*(c-f); double dis1 = Math.pow(y, 0.5); double dis2 = Math.pow(y1, 0.5); double dis3 = Math.pow(y2, 0.5); double S = (dis1+dis2+dis3)/2; double are = S*(s-dis1)*(S-dis2)*(S-dis3); double area = Math.pow(are,0.5); System.out.println("The area of the triangle is:"+area); } }
Ah, show your mood with a picture