JAVA由Sun Microsystems Inc的 James Gosling 于1995 年开发,后来被 Oracle Corporation 收购。它是一种简单的编程语言。Java 使编写、编译和调试编程变得容易。它有助于创建可重用的代码和模块化程序。Java是一种基于类、面向对象的编程语言,旨在尽可能减少实现依赖项。一种为开发人员编写的通用编程语言,只要编写一次就可以在任何地方运行,编译后的 Java 代码可以在所有支持 Java 的平台上运行。Java 应用程序被编译成可以在任何 Java 虚拟机上运行的字节码。Java 的语法类似于 c/c++。
Java的主要特性
1. Platform Independent: 编译器将源代码转换为字节码,然后JVM执行编译器生成的字节码。这个字节码可以在任何平台上运行,无论是 Windows、Linux 还是 macOS,这意味着如果我们在 Windows 上编译程序,那么我们可以在 Linux 上运行它,反之亦然。每个操作系统都有不同的 JVM,但所有操作系统在执行字节码后产生的输出是相同的。这就是我们称java为平台无关语言的原因。
2. 面向对象编程语言:用对象集合来组织程序是面向对象编程的一种方式,每个对象代表一个类的实例。
面向对象编程的四个主要概念是:
- 抽象
- 封装
- 遗产
- 多态性
3. 简单: Java 是一种简单的语言,因为它没有指针、运算符重载、多重继承和显式内存分配等复杂功能。
4. 健壮: Java 语言是健壮的,这意味着可靠。它的开发方式是尽可能早地检查错误,这就是为什么 java 编译器能够检测到其他编程语言不易检测到的错误。java 使其健壮的主要特性是垃圾收集、异常处理和内存分配。
5. 安全: 在java中,我们没有指针,所以我们不能访问越界数组,即如果我们尝试这样做会显示ArrayIndexOutOfBound Exception 。这就是为什么在 Java 中无法利用堆栈损坏或缓冲区溢出等安全漏洞。此外,java 程序在独立于 os(操作系统)环境的环境中运行,这使得 java 程序更加安全。
6. 分布式: 我们可以使用java编程语言创建分布式应用程序。远程方法调用和企业 Java Bean 用于在 Java 中创建分布式应用程序。Java 程序可以很容易地分布在一个或多个通过 Internet 连接相互连接的系统上。
7 、 多线程: Java支持多线程。它是一种 Java 功能,允许同时执行程序的两个或多个部分,以最大限度地利用 CPU。
8. 可移植性: 众所周知,在一台机器上编写的java代码可以在另一台机器上运行。java的平台无关特性,它的平台无关字节码可以被带到任何平台上执行,这使得java具有可移植性。
9. 高性能:Java 架构的定义方式是它减少了运行时的开销,有时 java 使用即时 (JIT) 编译器,编译器按需编译代码基础知识,只编译那些方法称为使应用程序执行得更快。
10. 动态灵活性:Java 完全面向对象使我们可以灵活地向现有类添加类、新方法,甚至通过子类创建新类。Java 甚至支持用其他语言编写的函数,例如 C、C++,它们被称为本地方法。
11. 沙盒执行:Java 程序在单独的空间中运行,允许用户在字节码验证器的帮助下执行他们的应用程序而不会影响底层系统。字节码验证器还提供额外的安全性,因为它的作用是检查代码是否存在任何违反访问的情况。
12. Write Once Run Anywhere:如上所述,java应用程序生成一个’.class’文件,它对应于我们的应用程序(程序),但包含二进制格式的代码。由于字节码不依赖于任何机器架构,因此它提供了与架构无关的易用性。这是全球范围内有进取心的 IT 行业使用 java 的主要原因。
13. 编译和解释的力量:大多数语言的设计目的要么是编译语言,要么是解释语言。但是随着 Java 编译器将源代码编译为字节码,而 JVM 执行此字节码以机器依赖于操作系统的可执行代码,java 集成了强大的功能。
Question 1
a) Suppose you are writing a Java program for an art gallery.
i) Write a class called ArtWork which has two attributes: name (which is a String), and creation_year (which is an integer) indicating when the artwork was created. Add accessor (getter) and mutator (setter) methods for each attribute. Use these appropriately in a constructor for ArtWork; the constructor takes two variables as input, which correspond to the name and creation year of an artwork, and sets the name and creation_year attributes accordingly.
ii) Add a method called age to the ArtWork class in part i), which returns the age of an artwork object by subtracting its creation_year value from 2019. Briefly describe a problem with this method’s specification, and a solution for the problem.
b) This question uses some of the code you wrote for class ArtWork in part a). Write the code for a tester class called ArtTester with a main method. In this class, an ArtWork object called “The Queen” from the year 1817 is constructed. The main method should also print out the age of the artwork.
c) Answer the following questions about the Java programming language:
i) What is the difference between x++ and ++x, in terms of their code behaviour?
ii) Why would someone use a break statement inside a switch?
iii) Briefly describe the reason for using the extends keyword.
Question 2
a) Answer the following questions about arrays and ArrayLists:
i) Indicate TWO differences between arrays and ArrayLists.
ii) Write Java code to place the following set of Strings into both an array called someArray and an ArrayList called someAList: {“twenty”, “ten”, “one”}.
Note: Your code must include a declaration of all the relevant variables.
b) This question is about access modifiers:
i) private and public are two of the access modifiers that can be used in Java. Explain the meaning of access modification in Java, by comparing the private and public access modifiers, including any advantages or disadvantages.
ii) How can you access an instance variable (attribute) if it is private?
iii) Often we may store a fixed variable (i.e. a constant value) marked as public final static. For example, a constant such as the absolute zero value of temperature (i.e. -273.14 Kelvin) would be written as shown in Figure 1. Will making this public cause any problem?
c) Assume the class Flower has already been implemented with the correct constructors and methods (e.g. public String getName() and public String toString()). What does the code fragment (in Figure 2) output to the screen?
Flower[] flowers = new Flower[3];
flowers[1] = new Flower("Rose");
System.out.println(flowers[1]);
System.out.println(flowers[2]);
System.out.println(flowers[3].getName());
d) Given an array of int values, write a method that computes the maximum value. Hint: You should pay attention to the method header, return type, and the calculation itself.
e) This section of the question is about Javadocs.
i) Write the command used to generate Javadoc comments, assuming that they are being generated for the file Example.java.
ii) Give ONE example of a Javadoc tag, along with its description. iii) Explain what a Javadoc is, and what is its purpose.