Java Basic Concepts

202 阅读3分钟

A Hello World Program

Instance

class MyClass {
    public static void main(String[ ] args) {
        System.out.println("Hello World");
    }
}

In the example above
class: MyClass
method: main
keywords: public, static

  • Every program in Java must have a class.
  • Every Java program starts from the main method.

The main Method

To run our program, the main method must be identical to this signature:

public static void main(String[] args)

public: anyone can access it
static: method can be run without creating an instance of the class containing the main method
void: method doesn't return any value
main: the name of the method
For example, the following code declares a method called test, which return nothing and has no parameters:

void test()

The method's parameters are declared inside the parentheses that follow the name of the method.For main, it's an array of strings called args.

System.out.println()

Next is the body of the main method, enclosed in curly braces:

System.out.println("Hello World");

The println method prints a line of text to the screen.
The System class and its out stream are used to access the println method.

In classes, methods, and other flow-control structures code is always enclosed in curly braces{}.
In Java, each code statement must end with a semicolon.
Remember: do not use semicolon after method and class declarations that follow with the body defined using the curly braces.

Comments

Instance

// a single-line commment
/* 
a multi-lines comment
*/
/* a nested comment:
  // a single-line comment
*/
/** a documentation comment
*/

Note that Java does not support nested multi-line comments.
However, you can nest single-line comments within multi-line comments.

Variables

Instance

You can declare a variable of a type and assign it a value.

String name = "Han";
// This creates a variable called name of type String, and assign it the value "Han".
int age = 27;
double score = 90;
char group = 'Z';
boolean online = true;

Getting User Input

Instance

While Java provides many different methods for getting user input, the Scanner object is the most common, and perhaps the easiest to implement. Import the Scanner class to use the Scanner objectm as seen here:

// Import the Scanner class to use the Scanner object
import java.util.Scanner;
// create an instance of the class by using the folowing syntax
Scanner myVar = new Scanner(System.in);

You can now read in different kinds of input data that the user enters.
Here are some methods that are avaiable through the Scanner class:
Read a byte - nextByte()
Read a short - nextShort()
Read an int - nextInt()
Read a long - nextLong()
Read a float - nextFloat()
Read a double - nextDouble()
Read a boolean - nextBoolean()
Read a complete line - nextLine()
Read a word - next()

Example of a program used to get user input:

import java.util.Scanner;
class MyClass {
    public static void main(String[ ] args) {
        Scanner myVar = new Scanner(System.in);
        System.out.println(myVar.nextLine());
    }
}

The Math Operators

Java provides a rich set of operators to use in manipulating variables. A value used on either side of an operator is called an operand.

Instance

int x = 6 + 3;

Java arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulo
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebraic equations.

Instance

int x = 2; int y = 4;
int result = x + y;
System.out.println(result);

Increment Operators

An increment or decrement operator provides a more convenient and compact way to increase or decrease the value of a variable by one.
For example, the statement x=x+1; can be simplified to ++x;\

Instance

int test = 5;
++ test; // test is now 6
int test = 5;
-- test; // test is now 4

Two forms, prefix and postfix, may be used with both the increment and decrement operators.
With prefix form, the operator appears before the operand, while in postfix from, the operator appears after the operand. Below is an explanation of how the two forms work:
Prefix: Increments the variable's value and uses the new value in the expression.

Instance

int x = 34;
int y = ++x; // y is 35

The value of x is first incremented to 35, and is then assigned to y, so the values of both x and y are now 35.
Postfix: The variable's value is first used in the expression and is then increased.

Instance

int x = 34;
int y = x++; // y is 34

x is first assigned to y, and is then incremented by one. Therefore, x becomes 35, while y is assigned the value of 34.

Strings

A String is an object that represents a sequence of characters.
For example, "Hello" is a string of 5 characters.

Instance

String s = "SoloLearn";
String str = ""; // You are allowed to define an empty string.

String Concatenation

The +(plus) operator between strings adds them together to make a new string. This process is called concatenation.
The resulted string is the first string put together with the second string.

String firstName, lastName;
firstName = "David";
lastName = "Williams";
System.out.println("My name is " + firstName + " " + lastName);

The char data type represnets a single character.