Variables and calculation
System.out.println("Hello World!"); Scanner in = new Scanner(System.in); System.out.println(in.nextLine());
1. First Java program
output
package hello; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); } }
- System.out.println()
- Statement ends with
- Use Alt + / autocomplete
- Package name convention starts with uppercase
input
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hello World!"); Scanner in = new Scanner(System.in); System.out.println(in.nextLine()); } }
- Scanner in = new Scanner(System.in)
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub int price = 0; int amount = 100; Scanner in = new Scanner(System.in); amount = in.nextInt(); price = in.nextInt(); System.out.println(amount + " - " + price + " = " + (amount-price)); } }
Here, two numbers are read in consecutively. The following two input methods are OK:
String connection:+
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("10 + 20 = " + (10+20)); } }
- Parentheses mean that the expression inside is evaluated first
- Attention and distinction
System.out.println("10 + 20 = " + 10+20);
2. Variables
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub int price = 0; Scanner in = new Scanner(System.in); price = in.nextInt(); System.out.println("100 - " + price + " = " + (100-price)); } }
- Java is a strongly typed language. When defining variables, you should specify the type
- Java does not allow using variables without initialization (it needs to be assigned before the first use)
- Clear nouns: identifiers, keywords
- The rules for identifier construction of Java variables are the same as those of C
- Note the following statement:
int price, sum = 100;
This statement only assigns an initial value to sum, but price does not. It should be written as follows:
int price = 50, sum = 100;
From the perspective of software engineering, it is better to define only one variable per line
3. Constant
Modifier final
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub int price = 0; Scanner in = new Scanner(System.in); price = in.nextInt(); final int amount = 100; System.out.println(amount + " - " + price + " = " + (amount-price)); } }
4. Integer and floating point
Related operation rules (int/int), operator priority, etc. are similar to C
- Monocular operators have the highest priority
- Most of the binding relationships (with the same priority to determine the direction of operation) are left to right, while the assignment is right to left
result = a = b = 3 + c
5. Cast
Similar to C, such as: (int)
package hello; import java.util.Scanner; public class hello { public static void main(String[] args) { // TODO Auto-generated method stub int foot = 0; int inch = 0; Scanner in = new Scanner(System.in); foot = in.nextInt(); inch = in.nextInt(); System.out.println((foot + inch/12.0)*0.3048); System.out.println((int)(foot + inch/12.0)*0.3048*100); System.out.println((int)((foot + inch/12.0)*0.3048*100)); } }
Note that the last two lines of output are different:
The reason is that (int), as a monocular operator, has the highest priority and is higher than *, so we first convert (foot + inch/12.0) to type (5.583 - > 5), and then do multiplication.