2020年面向初学者的终极Java指南

Java is a high-level programming language. It was initially designed for developing programs for set-top boxes and handheld devices, but later became a popular choice for creating web applications. The Java syntax is similar to C++ but is strictly an object-oriented programming language.

Java是一种高级编程语言。 它最初是为开发机顶盒和手持设备的程序而设计的,但后来成为创建Web应用程序的流行选择。 Java语法类似于C ++,但严格来说是一种面向对象的编程语言。

I hope with this article that you’ll learn and understand the fundamentals of Java.

我希望通过本文,您将学习和理解Java的基础知识。

入门 (Getting Started)

The first thing you need to do is install Java, and you can install an IDE such as Eclipse or IntelliJ.

您需要做的第一件事是安装Java ,然后可以安装IDE(例如Eclipse或IntelliJ)。

Now create a file and add the following code to check if Java is working on your pc: (Main.java)

现在创建一个文件,并添加以下代码以检查Java是否在您的PC上运行:(Main.java)

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

And to compile your file, run the command:

并编译您的文件,运行命令:

C:\Users\Your Name>javac Main.java

To execute your program, run the following command:

要执行程序,请运行以下命令:

C:\Users\Your Name>java Main

And if there are no errors your output will look like this:

如果没有错误,您的输出将如下所示:

Hello Medium

句法 (Syntax)

In Java, every piece of code must be written inside a class. The name of the class and everything is case-sensitive.

在Java中,每段代码都必须编写在一个类中。 类的名称以及所有内容均区分大小写。

The main method

主要方法

The main method is required in every java program to run it, and it looks like this:

每个Java程序都需要main方法来运行它,它看起来像这样:

public static void main(String[] args)

System.out.println()

System.out.println()

In java we use the System.out.println() to echo out text to the screen. It’s used a lot, and you’ll need it.

在Java中,我们使用System.out.println()将文本回显到屏幕上。 它已经使用了很多,您将需要它。

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

注释 (Comments)

Comments are commonly known around all programming languages to add notes to your code to explain it, in Java that works as follows:

注释在所有编程语言中都是众所周知的,它可以在Java中以如下方式在代码中添加注释以对其进行解释:

// This is a comment
System.out.println("Hello Medium");

Multi-Line Commenting

多行评论

/* Comment line 1 
Comment line 2 */
System.out.println("Hello Medium");

变数 (Variables)

A variable is like a little container to store data in. Variables are essential in Java. In Java, you need to specify the data type of a variable.

变量就像一个用于存储数据的小容器。变量在Java中是必不可少的。 在Java中,您需要指定变量的数据类型。

As in other programming languages, Java contains the following data types:

与其他编程语言一样,Java包含以下数据类型:

  • String — A string is a sequence of characters

    String —字符串是字符序列
  • Integer — An integer data type is a non-decimal number

    整数—整数数据类型是非十进制数
  • Float — A float is a number with a decimal point or a number

    浮点数—浮点数是带有小数点或数字的数字
  • Boolean — A Boolean represents TRUE or FALSE.

    布尔值—布尔值表示TRUE或FALSE。
  • Array — An array stores multiple values in one single variable.

    数组—数组将多个值存储在一个变量中。

Declaring & Initializing a variable

声明和初始化变量

type variable = value; //Pseudocode

You first name the type, for example, an integer, then enter the name of the variable, and if you want to initialize one, you can add value after the ‘=’ sign.

您首先要为类型命名,例如整数,然后输入变量的名称,如果要初始化变量,可以在'='符号后添加值。

int age = 19;
System.out.println(age);

Final Variables

最终变量

If you want to make your variables unchangeable, you can add the FINAL keyword in front. To make sure it cannot be overwritten.

如果要使变量不可更改,可以在前面添加FINAL关键字。 为了确保它不会被覆盖。

final int age = 19;
System.out.println(age);

数组 (Arrays)

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

数组用于将多个值存储在单个变量中,而不是为每个值声明单独的变量。

Declaring an Array

声明一个数组

String[] cars;

Initializing the Array

初始化数组

String[] cars = {"Tesla", "Audi", "Ford", "Mazda"};

Have access to the elements

可以访问元素

You will be able to access the elements in an array by index:

您将能够通过索引访问数组中的元素:

String[] cars = {"Tesla", "Audi", "Ford", "Mazda"};
System.out.println(cars[0]);Output: Tesla

基本运算符 (Basic Operators)

  • + (Addition)

    +(加法)
  • - (Subtraction)

    -(减法)
  • * (Multiplication)

    *(乘法)
  • / (Division)

    /(部门)
  • % (Modules)

    %(模组)
  • ++ (Increment)

    ++(增量)

数学 (Math)

Java has a math class that you can use to execute mathematic functions like this min() and max() functions or methods.

Java有一个数学类,您可以用来执行数学函数,例如min()和max()函数或方法。


public class MyClass {
public static void main(String[] args) {
System.out.println(Math.max(5, 10));
}
}Output: 10

条件语句 (Conditional Statements)

Java has the following conditional statement:

Java具有以下条件语句:

  • If

    如果
  • Else

    其他
  • If Else

    如果别的
  • Switch

    开关

If Statement

如果声明

To execute a block of code if a specific condition is true.

如果特定条件为真,则执行代码块。

if (condition) {// block of code to be executed if the condition is true
}

Else Statement

其他声明

To execute a block of code if a specific condition is false.

如果特定条件为假,则执行代码块。

if (condition) {// block of code to be executed if the condition is true
} else {// block of code to be executed if the condition is false
}

If Else Statement

否则声明

To specify a new condition if the first condition is false.

如果第一个条件为假,则指定新条件。

if (condition1) {// block of code to be executed if condition1 is true
} else if (condition2) {// block of code to be executed if the condition1 is false and condition2 is true
} else {// block of code to be executed if the condition1 is false and condition2 is false
}

Switch

开关

The switch statement is used to select one of many code blocks to be executed.

switch语句用于选择要执行的许多代码块之一。

switch(expression) {
case x:// code block
break;
case y:// code block
break;
default:// code block
}

循环 (Loops)

Loops can execute a block of code as long as a specified condition is reached.

只要达到指定条件,循环就可以执行代码块。

While Loop

While循环

The while loop loops through as long as a specific condition is true.

只要满足特定条件,while循环就会循环通过。

while (condition) {  // code block to be executed
}

For Loop

对于循环

When you know exactly how many times you want to loop through a block of code, you can use the for loop.

当您确切知道要遍历一段代码的次数时,可以使用for循环。

for (statement 1; statement 2; statement 3) {// code block to be executed
}

Statement 1 is executed before the execution of the code block.

语句1在执行代码块之前执行。

Statement 2 defines the condition for executing the code block.

语句2定义了执行代码块的条件。

Statement 3 is executed after the code block has been executed.

语句3在代码块执行后执行。

For Each Loop

对于每个循环

There is also a “for-each” loop that is used exclusively to loop through elements in an array:

还有一个“ for-each”循环,专用于循环遍历数组中的元素:

for (type variableName : arrayName) {// code block to be executed
}

结论 (Conclusion)

After this article, I hope you can write a simple Java code now and have a good idea of what Java is about.

读完本文之后,希望您现在可以编写一个简单的Java代码,并对Java的含义有一个很好的了解。

翻译自: https://levelup.gitconnected.com/the-ultimate-java-guide-for-beginners-in-2020-319568728169

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值