Java 基础知识:入门(文件语法和变量)

2022/11/5 1:23:52

本文主要是介绍Java 基础知识:入门(文件语法和变量),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

使用 Java 的基础知识

语法

使用Java时首先要知道的是语法,因为Java对如何设置文件有相当严格的要求。
对于 Java,Java 应用程序中的第一个文件是,其中包含以下内容:
Main.java

public class Main {
  public static void main(String[] args) {
    // whatever contents the user puts here
  }
}

示例中的第一行是类,每个 java 文件都需要该类。类有一些要求,主要是名称必须以大写字母开头,并且类必须与文件共享其名称(因此 Main 是 Main 的类.java,但如果类称为 Example,则需要将文件命名为 Example.java)。

第二行包含 main 方法,该方法与类明显不同。与类名不同(类名可能因文件名而异),每个文件都必须具有名称为 main 的方法。main 方法的目的是包含将在文件中执行的所有代码。

基本变量和语句

现在已经讨论了Java文件结构的基础知识,是时候讨论Java使用的基本变量和一些有用的语句了。

注释和控制台日志

//This is a single line comment
/* This is
a multiline comment */
System.out.println("Hello World"); //will print content to current line in console and anything printed after starts on a new line
System.out.print("Hello World"); //will print on current line and next print will continue on same line in console

变量声明

要在 Java 中声明变量,用户必须首先声明变量的类型,后跟名称和赋值,如下所示:

String exampleStr = "Hello there"; //variable declaration of type string
int integerVal = 50; //int variables store non decimal numbers
float floatingPointVal = 50.55; //floating point variables store numbers with a decimal
char character = "D"; //character variables store a single character
boolean bool = true; //boolean variables store boolean values (true/false)

关闭

要使用Java编写代码,用户必须确保每个文件都包含一个共享文件名的类,该文件名中包含main方法。用户还必须确保所有变量都使用其给定类型和名称(也称为标识符)声明。



这篇关于Java 基础知识:入门(文件语法和变量)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程