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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/620090.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

AI地名故事:笔岗村

笔岗村&#xff0c;实际上是由笔村和宏岗村两个古老的村落合并而成的。南宋度宗元年&#xff0c;也就是公元1265年&#xff0c;笔村开始建立。随着时间的推移&#xff0c;到了宋代后期&#xff0c;宏岗村也相继建立。这两个村落各自承载着丰富的历史和文化&#xff0c;最终在历…

浅析安全用电监控系统在工厂的研究与应用论述

摘 要&#xff1a;随着社会时代的发展&#xff0c;人们的安全意识越来越强烈&#xff0c;在人们生活和工作中离不开各种用电设备&#xff0c;用电设备的安全使用是保障人们生命安全的重要内容。工厂因自身厂内工作环境的特殊性&#xff0c;用电设备的种类多且复杂&#xff0c;如…

云仓酒庄携手中视中州国际传媒 开启央视广告战略合作新征程

近日&#xff0c;云仓酒庄与中视中州&#xff08;央视代理机构&#xff09;隆重举行2024-2025年度央视广告战略签约仪式&#xff0c;云仓酒庄副总裁周玄代表云仓酒庄签约。此次合作标志着云仓酒庄在品牌传播和市场营销方面迈出了坚实的一步&#xff0c;将借助央视及多家卫视的强…

星戈瑞SH-PEG3-OH一种多功能生物相容性PEG小分子

SH-PEG3-OH是一种含有硫基&#xff08;-SH&#xff09;、三个乙二醇单元和羟基&#xff08;-OH&#xff09;的小分子化合物。其分子结构中的硫基赋予了其独特的化学反应性&#xff0c;能够与其他含有不饱和键的化合物发生点击化学反应&#xff0c;如迈克尔加成反应等。同时&…

iOS 面试题总结(可能是最全的!!!)

如有错误 请及时在评论中指出 文章将不定期更新 1. objc_msgForward是干什么的&#xff0c;如果直接调用会发生什么&#xff1f; 作用&#xff1a;这个函数是IMP类型&#xff08;方法实现的内存地址也就是函数指针&#xff09;&#xff0c;用于消息转发&#xff0c;当向一个对…

iframe的替代方案有吗?做页面嵌套界面套娃

UIOTOS可以了解下&#xff0c;uiotos.net&#xff0c;通过连线来代替脚本逻辑开发&#xff0c;复杂的交互界面&#xff0c;通过页面嵌套轻松解决&#xff0c;是个很新颖的思路&#xff0c;前端零代码&#xff01; 蓝图连线尤其是独创的页面嵌套和属性继承技术&#xff0c;好家…

【Pychart】jupyter中pyecharts无法显示问题无法使用/No module named pyecharts

无法显示或No module&#xff0c;一般就是更换python版本后&#xff0c;没有在新的python里安装jupyter&#xff1b;另外原因就是引用方式问题&#xff0c;就是import方式不对&#xff1b;都解决后&#xff0c;有报错没有add&#xff0c;或者str问题。 最后的解决方案竟然是bin…

LVDS 源同步接口

传统数据传输通常采用系统同步传输方式&#xff0c;多个器件基于同一时钟源进行系统同步&#xff0c;器件之间的数据传输时序关系以系统时钟为参考&#xff0c;如图1所示。系统同步传输方式使各器件处于同步工作模式&#xff0c;但器件之间传输数据的传输时延难以确定&#xff…

【代码实践】starRocks 窗口函数(udf)实践

背景说明 实现天粒度的同比计算重点说明 要求数据是连续的因为天粒度的同比&#xff0c;需要365天&#xff0c;但为了方便测试&#xff0c;当前的判断逻辑是计算5天的前&#xff0c;而不是365天前的 参考文档 https://docs.starrocks.io/zh/docs/sql-reference/sql-functio…

流量卡避坑指南

流量卡避坑指南 在选择流量卡时&#xff0c;有几点需要注意以避免踩坑&#xff1a; 合同期和优惠期。 务必看清楚流量卡的合同期和优惠期。 有些卡可能首月免费&#xff0c;但月底办理可能不划算。 真正的长期套餐应该是优惠期20年以上的。 宣传与实际。 对于所谓的“永久9元…

C#图像处理实例1:opencvsharp获取轮廓凸包

在OpenCvSharp中&#xff0c;你可以使用Cv2.ApproxPolyDP函数来获取轮廓的凸包。这个函数使用Douglas-Peucker算法来近似轮廓。 以下是一个简单的例子&#xff0c;展示如何使用OpenCvSharp获取轮廓的凸包&#xff1a; Mat src Cv2.ImRead("保存图像\2.jpg", ImreadM…

实验名称:TCP 连接管理

目录 实验目的&#xff1a; 实验原理&#xff1a; 实验步骤&#xff1a; 1) 启动WireShark&#xff0c;设置抓包状态 2) 访问指定服务器 &#xff0c;通过Wireshark抓取通信数据报文 3) 分析TCP连接建立的三次握手和连接释放的四次握手过程 原始数据记录&#xff1a; 实…

https介绍,加密解密(举例+必要性,对称/非对称加密介绍),数字摘要/指纹(介绍,应用(session id,网盘的秒传功能))

目录 https 引入 介绍 加密解密层 介绍 没有绝对的安全 使用ssl的弊端 加密解密 概念 加密 解密 秘钥 举例 现实中 网络中 加密的必要性 常见加密方式 对称加密 特点 非对称加密 特点 数字摘要/指纹 介绍 应用 session id 百度网盘的秒传功能 https …

【数据结构课程学习】:队列学习

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;数据结构课程学习 &#x1f337;追光的人&#xff0c;终会万丈光芒 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 目录 &#x1f697; 1.队列的基本概念&#xff1a…

xCode升级后: Library ‘iconv2.4.0’ not found

报错信息&#xff1a; targets 选中 xxxNotification: Build Phases ——> Link Binary With Libraries 中&#xff0c;移除 libiconv.2.4.0.tbd libiconv.2.4.0.dylib 这两个库&#xff08;只有一个的移除一个就好&#xff09;。 然后重新添加 libiconv.tbd 修改完…

开发者集结号:大湾区 Open Source Day 邀您共探技术前沿

开源技术正以其开放、协作的特性&#xff0c;引领着软件开发的新潮流&#xff0c;是推动社会进步的重要力量。作为开发者&#xff0c;您是否渴望深入了解开源项目的前沿动态&#xff1f;由ALC深圳与2024中国互联网发展创新与投资大赛联合举办、FISCO金链盟深度参与的大湾区 Ope…

第五十二周:文献阅读+STHTNN

目录 摘要 Abstract 文献阅读&#xff1a;用于区域空气质量预测的时空分层传输神经网络 现有问题 提出方法 创新点 方法论 周期特征提取组件(PFEC) 场景动态图模块(SDGM) 时空特征提取组件&#xff08;STEC) 传输注意力模块(TransATT) STHTNN模型 研究实验 数据集…

基于微信小程序的预约挂号系统(源码)

博主介绍&#xff1a;✌程序员徐师兄、10年大厂程序员经历。全网粉丝12W、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447…

AI算法-高数5.1-线性代数-向量定义、表示和向量间的关系

看线性代数这篇文章&#xff08;AI算法-高数5-线性代数1-基本概念、向量-CSDN博客&#xff09;理解有些吃力的朋友们&#xff0c;可以先学下宋浩老师的这些课程。 宋浩老师&#xff1a; 3.1 n维向量及其运算_哔哩哔哩_bilibili 3.2 向量间的线性关系&#xff08;一&#xff…

程序员如何避免35岁危机?

所谓的“35岁危机”通常是指在职业生涯中遇到的一个挑战阶段&#xff0c;这个概念在不同行业和个人中有不同的体现。对于程序员来说&#xff0c;这可能与技术更新迅速、工作强度大、职业发展路径的不确定性等因素有关。 以下是一些建议&#xff0c;帮助程序员避免或缓解这一危机…