Section I:Introduction

想学习的私信,免费学习路线

原文

Section I:Introduction

1.1 Your First Java Program

The classic first program when introducing any new language is Hello World, or a program that prints to the console. In Java, Hello World can be written as such:Hello World

public class Helloworld{
	public static void main(String[] args){
		System.out.println("Hello,World");
	}
}
/*
1、All code in Java must be part of a class.
2、We delimit the beginning and end of segments of code
  using { and }.
3、All statements in Java must end in a semi-colon
4、For code run we need public static void main(String[] args)

 */

As compared to other languages like Python, this may seem needlessly verbose. However, there are several reasons for the verbosity of Java, which will be covered in the next few chapters. For now, notice some key syntactical features of the code snippet above:

  • The class declaration : in Java, all code lives within classes. public class HelloWorld
  • The function: all the code that runs must be inside of a method declared as . Future chapters will cover the exact meaning of this declaration.**main**``public static void main(String[] args)
  • Curly braces enclose sections of code (functions, classes, and other types of code that will be covered in future chapters).{}
  • All statements must end with a semi-colon.

1.2 Java Workflow

Taking a program from a .java file into an executable has two main steps in Java: compilation and interpretation.

在这里插入图片描述

To run the code in Hello.java, we would first compile the code into a .class file using the command javac HelloWorld.java. Then, to run the code, we would use the command java HelloWorld.

In your terminal, the result would look like the following

$ javac HelloWorld.java
$ java HelloWorld
Hello World!
D:\cs61b>dir /b
Helloworld.java

D:\cs61b>javac Helloworld.java

D:\cs61b>dir /b
Helloworld.class
Helloworld.java

D:\cs61b>java Helloworld
Hello,World

D:\cs61b>del Helloworld.class

D:\cs61b>dir /b
Helloworld.java

D:\cs61b>java Helloworld
错误: 找不到或无法加载主类 Helloworld

D:\cs61b>javac Helloworld.java

D:\cs61b>type Helloworld.java
public class Helloworld{
        public static void main(String[] args){
                System.out.println("Hello,World");
        }
}

D:\cs61b>type Helloworld.class
漱壕4
      
<init>()VCodeLineNumberTablemain([Ljava/lang/String;)V
SourceFileHelloworld.javaHello,World
D:\cs61b>

在这里插入图片描述

Class Files

There are several reasons for the usage of .class files, which we will only cover briefly here. First of all, .class files are guaranteed to have been type-checked, making the distributed code safer. They are also more efficient to execute, and protect the actual source code in cases of intellectual property. We will not go into the details of .class files in this textbook beyond knowing that they are created after compilation.

1.3 Basic Java Features

Variables and Loops

The program below will print out the integers from 0 through 9.

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
    }
}

When we run this program, we see:

$ javac HelloNumbers.java
$ java HelloNumbers
$ 0 1 2 3 4 5 6 7 8 9 

Some interesting features of this program that might jump out at you:

  • Our variable x must be declared before it is used, and it must be given a type!
  • Our loop definition is contained inside of curly braces, and the boolean expression that is tested is contained inside of parentheses.
  • Our print statement is just instead of . This means we should not include a newline (a return).System.out.print``System.out.println
  • Our print statement adds a number to a space. This makes sure the numbers don’t run into each other. Try removing the space to see what happens.
  • When we run it, our prompt ends up on the same line as the numbers (which you can fix in the following exercise if you’d like).

Of these features the most important one is the fact that variables have a declared type. We’ll come back to this in a bit, but first, an exercise.

Exercise 1.1.2. Modify so that it prints out the cumulative sum of the integers from 0 to 9. For example, your output should start with 0 1 3 6 10… and should end with 45.HelloNumbers

Also, if you’ve got an aesthetic itch, modify the program so that it prints out a new line at the end.

The program below will print out the integers from 0 through 9

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
    }
}

When we run this program, we see:

$ javac HelloNumbers.java
$ java HelloNumbers
$ 0 1 2 3 4 5 6 7 8 9 

Some interesting features of this program that might jump out at you:

  • Our variable x must be declared before it is used, and it must be given a type!
  • Our loop definition is contained inside of curly braces, and the boolean expression that is tested is contained inside of parentheses.
  • Our print statement is just instead of . This means we should not include a newline (a return).System.out.print``System.out.println
  • Our print statement adds a number to a space. This makes sure the numbers don’t run into each other. Try removing the space to see what happens.
  • When we run it, our prompt ends up on the same line as the numbers (which you can fix in the following exercise if you’d like).

Of these features the most important one is the fact that variables have a declared type. We’ll come back to this in a bit, but first, an exercise.

Exercise 1.1.2. Modify so that it prints out the cumulative sum of the integers from 0 to 9. For example, your output should start with 0 1 3 6 10… and should end with 45.HelloNumbers

Also, if you’ve got an aesthetic itch, modify the program so that it prints out a new line at the end.

Static Typing

Java is a statically typed language, which means that all variables, parameters, and methods must have a declared type. After declaration, the type can never change. Expressions also have an implicit type; for example, the expression has type . 3 + 5``int

Because all types are declared statically, the compiler checks that types are compatible before the program even runs. This means that expressions with an incompatible type will fail to compile instead of crashing the program at runtime.

The advantages of static typing include:

  • catching type errors earlier in the coding process, reducing the debugging burden on the programmer.
  • avoiding type errors for end users.
  • making it easier to read and reason about code.
  • avoiding expensive runtime type checks, making code more efficient.

However, static typing also has several disadvantages; namely:

  • more verbose code.
  • less generalizable code.

One of the most important features of Java is that all variables and expressions have a so-called . Java variables can contain values of that type, and only that type. Furthermore, the type of a variable can never change.static type

One of the key features of the Java compiler is that it performs a static type check. For example, suppose we have the program below:

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
        x = "horse";
    }
}

/*
1、Befor Java variables can be used,they must be declared.
2、Java variables must have a specific type.
3、Java variable type can never change.
4、Types are verified before the code even runs!!
 */

Compiling this program, we see:

$ javac HelloNumbers.java 
HelloNumbers.java:9: error: incompatible types: String cannot be converted to int
        x = "horse";
                ^
1 error

The compiler rejects this program out of hand before it even runs. This is a big deal, because it means that there’s no chance that somebody running this program out in the world will ever run into a type error!

This is in contrast to dynamically typed languages like Python, where users can run into type errors during execution!

In addition to providing additional error checking, static types also let the programmer know exactly what sort of object he or she is working with. We’ll see just how important this is in the coming weeks. This is one of my personal favorite Java features.

To summarize, static typing has the following advantages:

  • The compiler ensures that all types are compatible, making it easier for the programmer to debug their code.
  • Since the code is guaranteed to be free of type errors, users of your compiled programs will never run into type errors. For example, Android apps are written in Java, and are typically distributed only as .class files, i.e. in a compiled format. As a result, such applications should never crash due to a type error since they have already been checked by the compiler.
  • Every variable, parameter, and function has a declared type, making it easier for a programmer to understand and reason about code.

However, static typing also has several disadvantages, which will be discussed further in later chapters. To name a few:

  • More verbose code.
  • Less generalizable code.
Extra Thought Exercise

In Java, we can say . But in Python, we can’t say , like we saw above. Why is that so?System.out.println(5 + " ");``print(5 + "horse")

Consider these two Java statements:

String h = 5 + "horse";

and

int h = 5 + "horse";

The first one of these will succeed; the second will give a compiler error. Since Java is strongly typed, if you tell it is a string, it can concatenate the elements and give you a string. But when is an , it can’t concatenate a number and a string and give you a number.h``h``int

Python doesn’t constrain the type, and it can’t make an assumption for what type you want. Is supposed to be a number? A string? Python doesn’t know. So it errors.x = 5 + "horse"

In this case, , Java interprets the arguments as a string concatentation, and prints out “5horse” as your result. Or, more usefully, will print a space after your “5”.System.out.println(5 + "horse");``System.out.println(5 + " ");

What does print? 510, or 15? How about ?System.out.println(5 + "10");``System.out.println(5 + 10);

Defining Functions in Java

In languages like Python, functions can be declared anywhere, even outside of functions. For example, the code below declares a function that returns the larger of two arguments, and then uses this function to compute and print the larger of the numbers 8 and 10:

def larger(x, y):
    if x > y:
        return x
    return y

print(larger(8, 10))

Since all Java code is part of a class, we must define functions so that they belong to some class. Functions that are part of a class are commonly called “methods”. We will use the terms interchangably throughout the course. The equivalent Java program to the code above is as follows:

public class LargerDemo {
    public static int larger(int x, int y) {
        if (x > y) {
            return x;
        }
        return y;
    }

    public static void main(String[] args) {
        System.out.println(larger(8, 10));
    }
}

The new piece of syntax here is that we declared our method using the keywords , which is a very rough analog of Python’s keyword. We will see alternate ways to declare methods in the next chapter.public static``def

The Java code given here certainly seems much more verbose! You might think that this sort of programming language will slow you down, and indeed it will, in the short term. Think of all of this stuff as safety equipment that we don’t yet understand. When we’re building small programs, it all seems superfluous. However, when we get to building large programs, we’ll grow to appreciate all of the added complexity.

As an analogy, programming in Python can be a bit like Dan Osman free-soloing Lover’s Leap. It can be very fast, but dangerous. Java, by contrast is more like using ropes, helmets, etc. as in this video.

Code Style, Comments, Javadoc

Code can be beautiful in many ways. It can be concise. It can be clever. It can be efficient. One of the least appreciated aspects of code by novices is code style. When you program as a novice, you are often single mindedly intent on getting it to work, without regard to ever looking at it again or having to maintain it over a long period of time.

In this course, we’ll work hard to try to keep our code readable. Some of the most important features of good coding style are:

  • Consistent style (spacing, variable naming, brace style, etc)
  • Size (lines that are not too wide, source files that are not too long)
  • Descriptive naming (variables, functions, classes), e.g. variables or functions with names like or instead of or .year``getUserName``x``f
  • Avoidance of repetitive code: You should almost never have two significant blocks of code that are nearly identical except for a few changes.
  • Comments where appropriate. Line comments in Java use the delimiter. Block (a.k.a. multi-line comments) comments use and .//``/*``*/

The golden rule is this: Write your code so that it is easy for a stranger to understand.

Here is the course’s official style guide. It’s worth taking a look!

Often, we are willing to incur slight performance penalties, just so that our code is simpler to grok. We will highlight examples in later chapters.

Comments

We encourage you to write code that is self-documenting, i.e. by picking variable names and function names that make it easy to know exactly what’s going on. However, this is not always enough. For example, if you are implementing a complex algorithm, you may need to add comments to describe your code. Your use of comments should be judicious. Through experience and exposure to others’ code, you will get a feeling for when comments are most appropriate.

One special note is that all of your methods and almost all of your classes should be described in a comment using the so-called Javadoc format. In a Javadoc comment, the block comment starts with an extra asterisk, e.g. , and the comment often (but not always) contains descriptive tags. We won’t discuss these tags in this textbook, but see the link above for a description of how they work./**

As an example without tags:

public class LargerDemo {
    /** Returns the larger of x and y. */           
    public static int larger(int x, int y) {
        if (x > y) {
            return x;
        }
        return y;
    }

    public static void main(String[] args) {
        System.out.println(larger(8, 10));
    }
}

The widely used javadoc tool can be used to generate HTML descriptions of your code. We’ll see examples in a later chapter.

1.4 Exercises

  1. True/False: All variables, parameters, and methods must have a declared type in Java, and the type can never change.
  2. Suppose we have a function smaller(a, b) that takes in two int arguments a and b and returns the smaller of the two. What would the expression String x = smaller(10, 20) + 3; output?
  3. Choose all statements that are true in Java:
    • All code must be part of a class.
    • The end and beginning of code segments are delimited using curly brackets {}.
    • All statements in Java end with a semi-colon ;.
    • Any code we want to run must be inside of a function public static void main(String[] args).

翻译版

第一节:引言

1.1 您的第一个 Java 程序

引入任何新语言时,经典的第一个程序是 Hello World,即打印到控制台的程序。在 Java 中,Hello World 可以这样写:Hello World

public class Helloworld{
	public static void main(String[] args){
		System.out.println("Hello,World");
	}
}
/*
1、All code in Java must be part of a class.
2、We delimit the beginning and end of segments of code
  using { and }.
3、All statements in Java must end in a semi-colon
4、For code run we need public static void main(String[] args)

 */

与 Python 等其他语言相比,这似乎不必要地冗长。但是,Java 的冗长有几个原因,这将在接下来的几章中介绍。现在,请注意上面代码片段的一些关键语法特征:

  • 类声明:在 Java 中,所有代码都存在于类中。public class HelloWorld
  • 函数:所有运行的代码都必须位于声明为 的方法中。今后的章节将介绍该宣言的确切含义。**main**``public static void main(String[] args)
  • 大括号将代码部分(函数、类和其他类型的代码)括起来,这些代码将在后面的章节中介绍。{}
  • 所有语句必须以分号结尾。

1.2 Java工作流

在 Java 中,将程序从文件转换为可执行文件有两个主要步骤:编译解释.java

在这里插入图片描述

要运行 中的代码,我们首先使用命令将代码编译成文件。然后,要运行代码,我们将使用命令 。Hello.java``.class``javac HelloWorld.java``java HelloWorld

在终端中,结果如下所示:

$ javac HelloWorld.java
$ java HelloWorld
Hello World!
D:\cs61b>dir /b
Helloworld.java

D:\cs61b>javac Helloworld.java

D:\cs61b>dir /b
Helloworld.class
Helloworld.java

D:\cs61b>java Helloworld
Hello,World

D:\cs61b>del Helloworld.class

D:\cs61b>dir /b
Helloworld.java

D:\cs61b>java Helloworld
错误: 找不到或无法加载主类 Helloworld

D:\cs61b>javac Helloworld.java

D:\cs61b>type Helloworld.java
public class Helloworld{
        public static void main(String[] args){
                System.out.println("Hello,World");
        }
}

D:\cs61b>type Helloworld.class
漱壕4
      
<init>()VCodeLineNumberTablemain([Ljava/lang/String;)V
SourceFileHelloworld.javaHello,World
D:\cs61b>

在这里插入图片描述

类文件

使用文件有几个原因,我们在这里只简要介绍一下。首先,保证文件经过类型检查,使分布式代码更安全。它们的执行效率也更高,并在知识产权的情况下保护实际的源代码。我们不会在本教科书中详细介绍文件,只知道它们是在编译后创建的。.class``.class``.class

1.3 Java 基本特性

变量和循环

下面的程序将打印出从 0 到 9 的整数。

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
    }
}

当我们运行这个程序时,我们看到:

$ javac HelloNumbers.java
$ java HelloNumbers
$ 0 1 2 3 4 5 6 7 8 9 

该程序的一些有趣的功能可能会让您大吃一惊:

  • 我们的变量 x 在使用之前必须声明,并且必须给它一个类型!
  • 我们的循环定义包含在大括号内,测试的布尔表达式包含在括号内。
  • 我们的 print 语句只是代替 .这意味着我们不应该包含换行符(返回)。System.out.print``System.out.println
  • 我们的 print 语句在空格中添加一个数字。这样可以确保数字不会相互碰撞。尝试删除空间以查看会发生什么。
  • 当我们运行它时,我们的提示最终会与数字在同一行上(如果你愿意,可以在下面的练习中修复)。

在这些特性中,最重要的一个是变量具有声明的类型。我们稍后会回到这个问题,但首先,一个练习。

**练习 1.1.2.**修改,以便打印出从 0 到 9 的整数的累积总和。例如,输出应以 0 1 3 6 10…并应以 45 结尾。HelloNumbers

此外,如果您有审美上的痒,请修改程序,使其在最后打印出一行新行。

下面的程序将打印出从 0 到 9 的整数。

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
    }
}

当我们运行这个程序时,我们看到:

$ javac HelloNumbers.java
$ java HelloNumbers
$ 0 1 2 3 4 5 6 7 8 9 

该程序的一些有趣的功能可能会让您大吃一惊:

  • 我们的变量 x 在使用之前必须声明,并且必须给它一个类型!
  • 我们的循环定义包含在大括号内,测试的布尔表达式包含在括号内。
  • 我们的 print 语句只是代替 .这意味着我们不应该包含换行符(返回)。System.out.print``System.out.println
  • 我们的 print 语句在空格中添加一个数字。这样可以确保数字不会相互碰撞。尝试删除空间以查看会发生什么。
  • 当我们运行它时,我们的提示最终会与数字在同一行上(如果你愿意,可以在下面的练习中修复)。

在这些特性中,最重要的一个是变量具有声明的类型。我们稍后会回到这个问题,但首先,一个练习。

**练习 1.1.2.**修改,以便打印出从 0 到 9 的整数的累积总和。例如,输出应以 0 1 3 6 10…并应以 45 结尾。HelloNumbers

此外,如果您有审美上的痒,请修改程序,使其在最后打印出一行新行。

静态打字

Java 是一种静态类型语言,这意味着所有变量、参数和方法都必须具有声明的类型。声明后,类型永远无法更改。表达式也具有隐式类型;例如,表达式的类型为 。3 + 5``int

由于所有类型都是静态声明的,因此编译器在程序运行之前会检查类型是否兼容。这意味着具有不兼容类型的表达式将无法编译,而不是在运行时使程序崩溃。

静态类型的优点包括:

  • 在编码过程的早期捕获类型错误,从而减轻程序员的调试负担。
  • 避免最终用户的类型错误。
  • 使代码更容易阅读和推理。
  • 避免了昂贵的运行时类型检查,使代码更加高效。

但是,静态类型也有几个缺点;即:

  • 更冗长的代码。
  • 可概括性较差的代码。

Java 最重要的特性之一是所有变量和表达式都有一个所谓的 .Java 变量可以包含该类型的值,并且只能包含该类型的值。此外,变量的类型永远不会改变。static type

Java 编译器的主要特性之一是它执行静态类型检查。例如,假设我们有以下程序:

public class HelloNumbers {
    public static void main(String[] args) {
        int x = 0;
        while (x < 10) {
            System.out.print(x + " ");
            x = x + 1;
        }
        x = "horse";
    }
}

编译这个程序,我们看到:

$ javac HelloNumbers.java 
HelloNumbers.java:9: error: incompatible types: String cannot be converted to int
        x = "horse";
                ^
1 error

编译器甚至在程序运行之前就将其拒之门外。这是一件大事,因为这意味着在世界上运行这个程序的人不可能遇到类型错误!

这与 Python 等动态类型语言形成鲜明对比,在 Python 中,用户在执行过程中可能会遇到类型错误!

除了提供额外的错误检查外,静态类型还让程序员确切地知道他或她正在处理什么样的对象。我们将在未来几周内看到这有多重要。这是我个人最喜欢的 Java 特性之一。

总而言之,静态类型具有以下优点:

  • 编译器确保所有类型都兼容,使程序员更容易调试其代码。
  • 由于保证代码没有类型错误,因此编译程序的用户永远不会遇到类型错误。例如,Android 应用程序是用 Java 编写的,通常仅以 .class 文件的形式分发,即以编译格式分发。因此,此类应用程序永远不会因类型错误而崩溃,因为它们已经过编译器检查。
  • 每个变量、参数和函数都有一个声明的类型,使程序员更容易理解和推理代码。

但是,静态类型也有一些缺点,将在后面的章节中进一步讨论。仅举几例:

  • 更冗长的代码。
  • 可概括性较差的代码。
额外的思维练习

在 Java 中,我们可以说 .但是在 Python 中,我们不能说,就像我们上面看到的那样。为什么会这样?System.out.println(5 + " ");``print(5 + "horse")

请考虑以下两个 Java 语句:

String h = 5 + "horse";

int h = 5 + "horse";

其中第一个将成功;第二个将给出编译器错误。由于 Java 是强类型的,如果你告诉它是一个字符串,它可以连接元素并给你一个字符串。但是当是 时,它不能将一个数字和一个字符串连接起来并给你一个数字。h``h``int

Python 不限制类型,也无法假设您想要什么类型。应该是一个数字吗?一根绳子?Python 不知道。所以它错了。x = 5 + "horse"

在本例中,Java 将参数解释为字符串连接,并打印出“5horse”作为结果。或者,更有用的是,将在“5”之后打印一个空格。System.out.println(5 + "horse");``System.out.println(5 + " ");

打印什么?510,还是 15?怎么样 ?System.out.println(5 + "10");``System.out.println(5 + 10);

在 Java 中定义函数

在像 Python 这样的语言中,函数可以在任何地方声明,甚至在函数之外。例如,下面的代码声明一个函数,该函数返回两个参数中较大的一个,然后使用此函数计算和打印数字 8 和 10 中的较大一个:

def larger(x, y):
    if x > y:
        return x
    return y

print(larger(8, 10))

由于所有 Java 代码都是类的一部分,因此我们必须定义函数,以便它们属于某个类。作为类一部分的函数通常称为“方法”。我们将在整个课程中互换使用这些术语。与上述代码等效的 Java 程序如下:

public class LargerDemo {
    public static int larger(int x, int y) {
        if (x > y) {
            return x;
        }
        return y;
    }

    public static void main(String[] args) {
        System.out.println(larger(8, 10));
    }
}

这里的新语法是,我们使用 关键字 声明了我们的方法,这是 Python 关键字的一个非常粗略的模拟。我们将在下一章中看到声明方法的替代方法。public static``def

这里给出的 Java 代码似乎更冗长!你可能会认为这种编程语言会减慢你的速度,事实上,在短期内会。把所有这些东西都看作是我们还不了解的安全设备。当我们构建小程序时,这一切似乎都是多余的。但是,当我们开始构建大型程序时,我们将逐渐意识到所有增加的复杂性。

打个比方,用 Python 编程可能有点像 Dan Osman 自由独奏的 Lover’s Leap。它可能非常快,但很危险。相比之下,Java 更像是使用绳索、头盔等,就像这个视频一样。

代码样式, 注释, Javadoc

代码在很多方面都是美观的。它可以简明扼要。它可以很聪明。它可以是有效的。新手最不欣赏代码的方面之一是代码风格。当你作为一个新手进行编程时,你通常一心一意地想让它工作,而不考虑再看它或必须长时间维护它。

在本课程中,我们将努力保持代码的可读性。良好的编码风格的一些最重要的特征是:

  • 一致的样式(间距、变量命名、大括号样式等)
  • 大小(行不太宽,源文件不太长)
  • 描述性命名(变量、函数、类),例如名称为 or 而不是 or 的变量或函数。year``getUserName``x``f
  • 避免重复代码:除了一些更改之外,您几乎不应该有两个几乎相同的重要代码块。
  • 在适当的情况下发表评论。Java 中的行注释使用分隔符。阻止(也称为多行注释)注释使用 和 .//``/*``*/

黄金法则是这样的:编写代码,以便陌生人易于理解。

这是该课程的官方风格指南。值得一看!

通常,我们愿意承担轻微的性能损失,只是为了让我们的代码更容易理解。我们将在后面的章节中重点介绍示例。

评论

我们鼓励您编写自文档化的代码,即通过选择变量名称和函数名称,以便轻松了解到底发生了什么。然而,这并不总是足够的。例如,如果要实现复杂的算法,则可能需要添加注释来描述代码。您对评论的使用应该是明智的。通过经验和接触他人的代码,您将感觉到何时评论最合适。

需要特别注意的是,所有方法和几乎所有类都应该使用所谓的 Javadoc 格式在注释中描述。在 Javadoc 注释中,块注释以额外的星号开头,例如 ,并且注释通常(但并非总是)包含描述性标记。我们不会在本教科书中讨论这些标签,但请参阅上面的链接以了解它们的工作原理。/**

举个不带标签的例子:

public class LargerDemo {
    /** Returns the larger of x and y. */           
    public static int larger(int x, int y) {
        if (x > y) {
            return x;
        }
        return y;
    }

    public static void main(String[] args) {
        System.out.println(larger(8, 10));
    }
}

广泛使用的 javadoc 工具可用于生成代码的 HTML 描述。我们将在后面的章节中看到示例。

1.4 练习

  1. True/False:所有变量、参数和方法都必须在 Java 中具有声明的类型,并且该类型永远不能更改。
  2. 假设我们有一个函数,它接受两个参数并返回两个参数中较小的一个。表达式将输出什么?smaller(a, b)``int``a``b``String x = smaller(10, 20) + 3;
  3. 选择在 Java 中为 true 的所有语句:
    • 所有代码都必须是类的一部分。
    • 代码段的结尾和开头使用大括号分隔。{}
    • Java 中的所有语句都以分号结尾。;
    • 我们想要运行的任何代码都必须位于函数内部。public static void main(String[] args)
  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值