静态方法属于类,它们将与类一起加载到内存中。您可以无需创建对象即可调用它们。(使用类名作为引用)。示例public class Sample{ static int num = 50; public static void demo(){ System.out.println("静态方法的内容"); } public static void main(String args[]){ Sample.demo(); } }输出静态方法的内容“this”关键字用作实例的引用。由于静态方法不具有(属于)任何实例,因此您不能在... 阅读更多
异常是在程序执行期间发生的错误(运行时错误)。当发生异常时,程序会突然终止,并且生成异常的那一行之后的代码永远不会执行。示例import java.util.Scanner; public class ExceptionExample { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("输入第一个数字:"); int a = sc.nextInt(); System.out.println("输入第二个数字:"); int b = sc.nextInt(); int c = a/b; System.out.println("结果是:"+c); ... 阅读更多
字符串用于在 Java 中存储字符序列,它们被视为对象。java.lang 包的 String 类表示一个字符串。您可以通过使用 new 关键字(像任何其他对象一样)或通过为文字赋值(像任何其他基本数据类型一样)来创建字符串。String stringObject = new String("Hello how are you"); String stringLiteral = "Welcome to Tutorialspoint";连接字符串您可以通过以下方式在 Java 中连接字符串 -使用“+”运算符:Java 提供了一个连接运算符,使用它,您可以直接添加两个字符串文字示例import java.util.Scanner; public class StringExample { public ... 阅读更多
字符串用于在 Java 中存储字符序列,它们被视为对象。java.lang 包的 String 类表示一个字符串。您可以通过使用 new 关键字(像任何其他对象一样)或通过为文字赋值(像任何其他基本数据类型一样)来创建字符串。示例public class StringDemo { public static void main(String args[]) { String stringObject = new String("Hello how are you"); System.out.println(stringObject); String stringLiteral = "Welcome to Tutorialspoint"; System.out.println(stringLiteral); } }输出Hello how are you Welcome to Tutorialspoint字符串的存储... 阅读更多