Java Getting Started Guide

Java is a high-level programming language that has the ability to be independent of any platform, which makes Java a cross-platform programming language. Developed by Sun Microsystems and released in 1995, Java is characterized by safety, ease of learning, strong portability, and object orientation. This article will detail the basics of Java.

First, the characteristics of Java

  1. Cross-platform: Java programs can be compiled into bytecodes that can run on any operating system that supports the Java Virtual Machine (JVM).

  2. Object-Oriented: Java is a fully object-oriented programming language that supports features such as encapsulation, inheritance, and polymorphism.

  3. Easy to learn: The syntax of Java is very similar to C++, and Java does not support complex language features such as pointers and multiple inheritance.

  4. Security: Security in Java is a very important feature. Because Java code runs in the JVM, Java programs can be restricted to run in a sandbox, thereby avoiding the risk of accessing system resources.

Two, Java program structure

Java programs are mainly composed of classes and methods. A Java program must contain a class, and the class name must be the same as the file name. Each class has its own properties and methods, which define the behavior of the class.

  1. class definition

A class definition consists of access modifiers, class name, inheritance relationship, implemented interface and class body, as follows:

public class MyClass extends ParentClass implements MyInterface {
    //class body
}

javaCopy code

  1. method definition

A method definition consists of access modifiers, return type, method name, parameter list, and method body, as follows:

public int sum(int a, int b) {
    return a + b;
}

javaCopy code

  1. package use

Packages in Java are used to organize classes and interfaces. Every Java program must belong to a package, and the name of the package should be the same as the path of the Java folder. Classes in other packages can be imported using the import statement.

package com.example;
import java.util.ArrayList;

public class MyClass {
    //class body
}

javaCopy code

3. Java basic data types

Basic data types in Java include integers, floats, booleans, and characters.

  1. integer

There are 4 types of integers in Java: byte, short, int, and long. Their value ranges are -128~127, -32768~32767, -2147483648~2147483647, -9223372036854775808~9223372036854775807 respectively.

byte b = 100;
short s = 10000;
int i = 1000000;
long l = 100000000L; //注意要加L表示为长整型

javaCopy code

  1. floating point

There are two types of floating point types in Java: float and double. The numerical precision of the float type is 7 significant figures, while the numerical precision of the double type is 15 significant figures.

float f = 3.14f; //注意要加f表示为浮点型
double d = 3.1415926;

javaCopy code

  1. Boolean

The Boolean type in Java has only two values: true and false.

boolean b1 = true;
boolean b2 = false;

javaCopy code

  1. character type

The character type in Java is represented by single quotes, and each character occupies 2 bytes.

char c = 'A';

javaCopy code

4. Java Operators

Operators in Java include arithmetic operators, assignment operators, comparison operators and logical operators, etc.

  1. arithmetic operator

Arithmetic operators include addition, subtraction, multiplication, division, remainder, and increment and decrement operators.

int a = 10;
int b = 5;
int c = a + b; //加法运算
int d = a - b; //减法运算
int e = a * b; //乘法运算
int f = a / b; //除法运算
int g = a % b; //求余运算
int h = ++a; //自增运算
int i = --b; //自减运算

javaCopy code

  1. assignment operator

Assignment operators are used to assign values ​​to variables.

int a = 10;
int b = a;
a += 5; //等价于a = a + 5

javaCopy code

  1. comparison operator

Comparison operators are used to compare the values ​​of two variables.

int a = 10;
int b = 5;
boolean c = a > b; //大于
boolean d = a >= b; //大于或等于
boolean e = a < b; //小于
boolean f = a <= b; //小于或等于
boolean g = a == b; //等于
boolean h = a != b; //不等于

javaCopy code

  1. Logical Operators

Logical operators are used to combine multiple conditions.

boolean a = true;
boolean b = false;
boolean c = a && b; //与运算,false
boolean d = a || b; //或运算,true
boolean e = !a; //非运算,false

javaCopy code

Five, Java process control statement

Flow control statements in Java mainly include conditional statements and loop statements.

  1. Conditional statements

Conditional statements include if statement, if-else statement, if-else if-else statement, and switch statement.

int a = 10;
if (a > 5) {
    System.out.println("a > 5");
}

if (a > 20) {
    System.out.println("a > 20");
} else {
    System.out.println("a <= 20");
}

if (a > 20) {
    System.out.println("a > 20");
} else if (a > 15) {
    System.out.println("a > 15 and a <= 20");
} else {
    System.out.println("a <= 15");
}

int grade = 80;
switch (grade) {
    case 60:
        System.out.println("及格了");
        break;
    case 70:
        System.out.println("良好");
        break;
    case 80:
        System.out.println("优秀");
        break;
    default:
        System.out.println("其他得分");
}

javaCopy code

  1. loop statement

Loop statements include for loop, while loop and do-while loop.

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

int i = 0;
while (i < 10) {
    System.out.println(i);
    i++;
}

int j = 0;
do {
    System.out.println(j);
    j++;
} while (j < 10);

javaCopy code

Six, Java object-oriented

Java is an object-oriented programming language, therefore, it is very important to understand Java object-oriented concepts.

  1. concept of object

Objects are a fundamental concept in object-oriented programming. Objects have both behavior and properties. The properties of an object refer to the state or characteristics of the object, while the behavior of an object refers to the operations or methods that the object can perform.

  1. class definition

Classes are another basic concept in Java object-oriented. A class describes the common characteristics of a set of objects, including properties and methods.

public class MyClass {
    private int x;
    private int y;
    
    public MyClass(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
    
    public int sum() {
        return x + y;
    }
}

javaCopy code

  1. Object creation and use

To create a Java object, you need to use the new keyword.

MyClass myObj = new MyClass(10, 20);
int x = myObj.getX();
myObj.setY(30);
int sum = myObj.sum();

javaCopy code

Fourth, Java exception handling

The exception handling mechanism in Java allows the program to tell the user what went wrong and take corresponding measures. An exception is an event that occurs during program execution that can be caught and used to fix program errors.

  1. abnormal classification

Exceptions in Java are mainly divided into two categories: checked exceptions and runtime exceptions. Checked exceptions are detected at the compiler, while runtime exceptions are detected during program execution.

  1. exception handling statement

Exception handling statements in Java mainly include try-catch statement, finally statement and throw statement.

try {
    //可能会发生异常的代码
} catch (Exception e) {
    //捕获异常
} finally {
    //无论是否发生异常,都会执行的代码
}

throw new Exception("异常信息");

javaCopy code

Seven, Java case

Below is a simple Java program that calculates the sum of two numbers.

import java.util.Scanner;

public class Calculate {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("请输入第一个数字:");
        int num1 = input.nextInt();
        System.out.print("请输入第二个数字:");
        int num2 = input.nextInt();
        int sum = num1 + num2;
        System.out.println("计算结果为:" + sum);
    }
}

javaCopy code

This program realizes the user enters two numbers by entering the Scanner class, then calculates their sum and outputs the result.

Guess you like

Origin blog.csdn.net/qq_60870118/article/details/131146691