Skip to content

Latest commit

 

History

History
103 lines (85 loc) · 2.45 KB

File metadata and controls

103 lines (85 loc) · 2.45 KB

Java Basic Concept

Start From HelloWorld

public class Demo{
	static void main(String[] args){
		System.out.println("Hello, world!");
	}
}

Questions:

  1. What's the name of this java file?
  2. How to use java and javac in command line?
  3. What should you do if you want to output "Hi, world!"?

Variable, Method, Statement and Comment

public class Demo{
	static void main(String[] args){
		int a = 1; // assign 1 to variable a
		System.out.println(a);
	}
}

Questions:

  1. What does void, int, double stand for?
  2. Why a is a variable, System.out.println() is a method?
  3. How can you tell if one single line is a statement or not?
  4. How should you name a class, a variable and a method?
  5. What's the difference between int and Integer? How to tell if a variable type is reference or primitive?
  6. When shoule we use comment?
  7. What is local variable? Is a in main method a local variable?

Define a Method

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:

  1. What does the key word return stand for?
  2. Why we put Integer before sumTwo, void before main?
  3. Can we difine a method in the body of another method?
  4. Do we need to input the exact value of input variable when defining the method?

Array

Questions:

  1. How to create a static array?
  2. What does String[] args stand for in main method?
  3. Please write a java file, every time we put two integers in argument, the sum will be printed out.

Boolean and Condition Control

public class Demo{
	public static void main(String[] args) {
		for (int i = 1; i < 10; i=i+1){
			System.out.println(i);
		}
	}
}

Questions:

  1. What's the difference between = and ==?
  2. Must else occur in pair with if?
  3. What it would be if for (int i = 1; true; i=i+1)? What if for (int i = 1; false; i=i+1)
  4. How to use key word for?
  5. What does !, <= and != stand for?

Try and Catch

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:

  1. Must catch occur in pair with try?
  2. What does the key word finally stand for?