(JAVA) Miscellaneous Topics

Installing JDK

  • Go to this website to download the Java Development Kit (JDK), and we then install it.
  • To check if the JDK is installed correctly, open Terminal on MacOS and runjavac --version, which should show javac <some version number>
  • javac stands for Java Compiler.

Compliation in Java

  • All code in Java goes in SomeName.java files, instead of having two different kinds of files like .h and .cpp files in C++. There are not separate header and implementation files, because declaration and implementation cannot be separated in Java.
  • We compile all .java files individually as in we write javac SomeFile.java on the command line (i.e. you typically write this file by yourself, and this is your “source code”), and the java compiler turns this file into a SomeFile.class file (i.e. Java turns your source code into something that a machine can deal with). This file is a compiled version of SomeFile.java. Java doesn’t compile to machine code, so it doesn’t produce any .exe files or directly executable files. Instead, java compiles the source code into an intermediate form called Java bytecode, which runs on what’s called the Java Virtual Machine (JVM). What’s being run on the JVM is the .class file.

Many classes in one .java file

  • You may get files like SomeFile$SomeOtherName.class as output when you compile SomeFile.java because you actually get one class file for each class definition inside the .java file. An example will be when you have inner classes in your class definition.
  • In Java, generally, there is at most one main(String[] args) method per .java file. Indeed, often times, in a large java project (i.e. a package that you compose), you may only need one Test class for testing your project, and in this whole project, the only class that has a main() method will be this testing class.

Editors & IDEs for the Java Language

  • XCode does not by default support Java.
  • You can use Visual Studio Code for Java (Win, Mac, Linux). It is not an IDE! It is a text editor, but it does have a Java extension which is still fairly powerful.
  • Eclipse is an IDE for Java.

Class

  • Each .java file can contain only one public class. This class is visible to every other class.
  • The file name of the .java file should be exactly the same as the name of the publi class it contains

Access Modifiers

  • Class-Level Access Modifiers
  • Member-Level Access Modifiers

Instance and Static members

Final members

Packaging

What is Packaging

  • Packaging means you put a bunch of classes together into a package. You do this by including the command pacakge pacakge_name; at the beginning of the .java file.

Why Packaging

  • Firstly, it allows you to use package-private classes (without first packaging the .java file where the package-private class is defined, you cannot use package-private class because there is no related pacakge)
  • Secondly, by pacakging classes in a package, you can explicitly use those classes (but certainly only the public fields and methods) in your own user-defined class. For example, the java.lang package provided classes that are fundamental to the design of the Java programming language, and Math class (i.e. java.lang.Math) is one of the important and frequently used classes.

Inheritance

Caveat on Conversions

  • Suppose class B inherits class A. By the rule of inheritance, B inherits every protected and public method & members (not including any of A's constructors, as A and B essentially have different names; nor does B inherit package-private or private members in A).
  • Moreover, due to polymorphism allowed by the inheritance structure, we can use a reference of type A to refer to a B object. For example, A obj = new B();.
  • Now assume obj has been created: Keep in mind that B may also have some members that are exclusive to itself (i.e. those are not in A). Say B has such a method called public void bs_method(). In this case, if you want to call bs_method() from obj, you will need to firstly perform an explicit narrowing conversion (narrowing conversion must be explicit, or you get a compile error): ((B) obj).bs_method();. You will get a compile error if you do this: obj.bs_method();. This is because you are calling the method bs_method() from a reference of type A, so the compiler will look for such a method in class A's body, and it certainly won’t find it, so you get an compile error.

GUI with the Swing Package

GUI stands for Graphical User Interface. javax.swing is a package that we can use to design interactive applications in Java.

Top Level Containers

  • There are three classes in swing that serves as the top level containers in GUI: JFrame, JDialog, and JApplet. We focus on JFrame.
  • Top level containers are literally like the windows we typically see in computer applications i.e. the outermost frame we see when we open up an application program, and as they are the outermost frame, various GUI Components can be added to them. In fact, GUI Components must be part of a containment hierarchy (i.e. the structure of which GUI object contains which) in order to appear on the screen.
  • However, one top level container cannot fit into another one as they are already the top level components of an application.
  • Each top-level container has a content pane (of the class Container, accessed through JFrame_object_name.getContentPane(). GUI components must be added to this content pane, but we have many ways to achieve this, and some may be better than the others (see JPanel). Notice that Container actually belongs to another package java.awt.
  • A menu bar on (the top end of) a JFrame is optional. A menu bar is not a GUI component.

JFrame

  • JFrame is a heavyweight container, so its physical appearance is heavily dependent on the underlying operation system (i.e. a JFrame window on a Windows computer appears to be different than that on an IOS computer).

Adding GUI Components to JFrame

Instance Methods

  • setVisible(boolean)
  • add(Component)
  • setSize(int width, int height)
  • pack(): resize the Jframe to fit a preferred size and layout of its sub-components.
  • setResizable(boolean)
  • setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE): program does not exist when the JFrame is closed. An alternative argument that is commonly used is JFrame.EXIT_ON_CLOSE, which exits the java application if the user closes it.

JPanel

  • JPanel is a container, but a lower-level one.

  • It is also a lightweight container, which means it is managed not by the operation system but by the Java program.

  • Comparing with adding GUI components to the content pane or the frame directly, it is better to have GUI components added to JPanels, an then add these JPanels to a JFrame. Why?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值