public class Demo{
static void main(String[] args){
System.out.println("Hello, world!");
}
}Questions:
- What's the name of this java file?
- How to use java and javac in command line?
- What should you do if you want to output "Hi, world!"?
public class Demo{
static void main(String[] args){
int a = 1; // assign 1 to variable a
System.out.println(a);
}
}Questions:
- What does void, int, double stand for?
- Why a is a variable, System.out.println() is a method?
- How can you tell if one single line is a statement or not?
- How should you name a class, a variable and a method?
- What's the difference between int and Integer? How to tell if a variable type is reference or primitive?
- When shoule we use comment?
- What is local variable? Is a in main method a local variable?
public class Demo{
static Integer sumTwo(int a, int b){
return a+b;
}
public static void main(String[] args){
System.out.println(sumTwo(1,2));
}
}Questions:
- What does the key word return stand for?
- Why we put Integer before sumTwo, void before main?
- Can we difine a method in the body of another method?
- Do we need to input the exact value of input variable when defining the method?
Questions:
- How to create a static array?
- What does String[] args stand for in main method?
- Please write a java file, every time we put two integers in argument, the sum will be printed out.
public class Demo{
public static void main(String[] args) {
for (int i = 1; i < 10; i=i+1){
System.out.println(i);
}
}
}Questions:
- What's the difference between = and ==?
- Must else occur in pair with if?
- What it would be if for (int i = 1; true; i=i+1)? What if for (int i = 1; false; i=i+1)
- How to use key word for?
- What does !, <= and != stand for?
public class Demo{
public static void main(String[] args) {
try {
int[] myNumbers = {1,2,3};
System.out.println(myNumbers[10]);
} catch (Exception e) {
System.out.println("Something went wrong.");
} finally {
System.out.println("The 'try catch' is finished.");
}
}
}Questions:
- Must catch occur in pair with try?
- What does the key word finally stand for?