20165318 2017-2018-2 "Java program design" the second week to summarize the learning content of learning materials
This week, we learned the contents of chapters II and III, and mastered the knowledge of basic data types, arrays, operators, expressions, and statements in Java.
Summarizing the first week's learning experience, during this week's study, I read the textbook carefully and found a lot of small details that I had not noticed in my previous study of C. In contrast to the C language learning process, not only to consolidate the C language Foundation, and the convenience of Java has a new understanding, as if open a door, in writing code no longer feel dull, but will be active thinking, and different methods to solve the same problem.
The difference between Java and C language
1, there is no unsigned byte,short,int and long in Java, this is different from C language. Therefore, unsigned int m; is the wrong S-variable declaration.
2. Char data in Java must be unsigned, and unsigned is not allowed to decorate the declared char variable.
3. Java does not allow you to specify the number of array elements within the square brackets in the declaration array. For example, int a[12]; causes a syntax error.
4. Java allows you to specify the number of elements of an array using the value of the int variable. For example:
int size = 30;double number[] = new double[size];
Problems in teaching materials learning and the solving process
- Issue 1: Type conversion operations (P21)
When it comes to assigning the value of a high-level variable to a variable of low level, there is an example of a type conversion operation that must be used:
long y = (long)56.98F
I was wondering why a type conversion operation is used when a float variable is assigned to a long variable.
Resolution process: In the second reading of the textbook, I found that I confused the accuracy of float and long. Basic data type variables are sorted by precision from low to High:
byte short char int long float double
Issue 2: Loss of precision during type conversion (P22)
Examples of textbooks include:
byte a = (byte)128;byte b = (byte)(-129);
Given the values of A and B, respectively-128 and 127, I don't understand the operation of this code.
Solution: Solved in type conversion operation, and tried to calculate it by itself
byte a = (byte)255;
The result is indeed a=-1.
Problems and resolution during code debugging
- 1: I'm using git bash in Windows, but there's a problem with Chinese garbled in git, the default encoding in my git is UTF-8, I write code in vim and I run it in Chinese. I referred to the Windows Git bash in the Chinese garbled solution, but this does not match my git situation. After I have tried, I found that using GBK encoding will not appear in Chinese garbled, but in the Vim editor will appear in Chinese garbled.
I have not found a solution to the problem now.
- Question 2:
I'm having an error while debugging example 3.9 (P47)
Workaround: After checking the code, I found that I will System.out.printf
, input into System.out.println
, resulting println
in inconsistent with its contents %d
.
Issue 3: In debugging Example 3.7 (P45) output 100 of the z all primes, the result can only output 2 and 3 code as follows;
public class Example3_7 {public static void main (String args[]) { int sum =0,i,j; for(i=1;i<=10;i++) { if(i%2==0) { continue; } sum = sum + i; } System.out.println("sum="+sum); for(j=2;j<=100;j++) { for(i=2;i<=j/2;i++){ if(j%i==0) { break; } } if(i>j/2) { System.out.println(""+j+"是素数"); } }}}
WORKAROUND: Because I use the end-of-line style when I hit the code, and even if if
there is only one line of code in the statement that does not omit {}, I enclose the statements if
in the statement in break;
{}, but the output primes are only 2 and 3, but I get rid break;
The {} of the statement is running as expected.
Although I can output the correct result, but I still do not quite understand the problem where, is the break
statement only jump out of the if
statement but did not jump out of the for
loop? Hope to get answers.
Code Cloud Link: Https://gitee.com/BESTI-IS-JAVA-2018/20165318_sun_xiaoxuan
Script run:
Resources:
Windows git bash Chinese garbled solution
Type conversion Operations
20165318 2017-2018-2 "Java programming" the second week study summary