Java中的字符串初始化
字符串初始化是编程的基本部分之一。字符串初始化意味着在java程序中使用变量之前为其赋值。字符串初始化可以通过两种方式完成:
- 对象初始化
- 直接初始化
字符串是java中的一个对象。字符串是指某些字符序列。 它也称为字符数组。字符串始终用引号表示;即使引号也不是字符串的一部分。字符串初始化也分两步进行,第一步是声明,第二步是初始化。字符串声明是指声明一个字符串而不显式分配任何值。在大多数用例中,字符串的唯一声明不需要初始化。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试Java 中字符串初始化的类型
在java中,字符串的初始化有多种方式。通过使用构造函数或使用文字。使用 new 关键字和使用 Literal 初始化 String 是有区别的。每次创建新的 java 对象时都使用 new 关键字初始化 String。已定义但未初始化的变量不能在程序中使用,因为它没有保存任何值。
1.对象初始化
使用 new 关键字初始化字符串。使用 new 关键字初始化 String 会在内存堆中创建一个新对象。通过对象初始化的字符串是可变的,这意味着字符串的值可以在初始化后重新分配。
String strMsg = new String("Be specific towards your goal!");
当使用构造函数初始化对象时,java编译器在堆内存中创建一个新对象。堆内存是 JVM 保留的内存。
2.直接初始化
使用文字初始化字符串。使用 Literal 初始化字符串会在内存池区域中创建一个对象。
String strMsg = "Be specific towards your goal!";
当使用文字初始化字符串时,分配给它的值已经在另一个字符串中初始化。在这种情况下,通过 Literal 创建的 String 不会创建新对象。它只是传递了对先前创建的对象的引用。
Java 中字符串初始化的示例
以下解释的是java中字符串初始化的不同示例。
示例#1
在下面创建的字符串中,两个字符串都是使用 Literal 创建的。
代码:
String ObjStringFirst="Welcome"; String ObjStringSecond="Welcome";
在上面给出的字符串中,我们使用字符串文字创建对象。在这种情况下,如果该对象已经存在并且具有相同的值,它将返回相同的对象;否则,它将创建一个新对象。
示例#2
在下面给出的示例中,我们可以看到字符串是如何初始化的。
代码:
public class StringExample { public static void main(String[] args) { String nameFirst = "Alister"; String nameSecond = "Campbell"; String nameThird = new String("Adam"); String nameFourth = new String("Martin"); //String nameFifth; String strNullFirst = null; String strNullSecond = null; String strNullThird = null; System.out.println("String initialized :"); //printing variable which are initialized System.out.println("First : " + nameFirst); System.out.println("Second : " + nameSecond); System.out.println("Third : " + nameThird); //printing variable which are declared but not initialized //System.out.println("String declared but not initialized :"); //System.out.println("Fifth : " + nameFifth); } }
输出:
示例 #3
在此示例中,给出了 String 变量的声明。它包含一个在编译程序时声明但未初始化的变量。
代码:
public class StringExample { public static void main(String[] args) { String nameFirst; //printing variable which are declared but not initialized System.out.println("String declared but not initialized :"); System.out.println("Fifth : " + nameFirst); } }
输出:
上述程序的输出如下所示。上面给出的程序将在编译时生成错误。字符串已声明但未初始化。
在 String 声明之后,如果稍后为声明的 String 变量赋值,则编译不会抛出任何错误(因为 String 初始化分为两部分)。第一个是声明,第二个是赋值。
示例#4
在此示例中,我们使用空值进行字符串初始化。所有通过空值初始化的字符串变量将具有不同的对象,这三个变量都将具有不同的对象,因为通过 Literal 初始化的字符串将在字符串池中创建对象,而通过 new 关键字初始化的字符串将在内存堆区域中创建对象。
代码:
public class StringExample { public static void main(String[] args) { String strVal1 = ""; String strVal2 = new String(); String strVal3 = new String(""); //printing variable which are initialized with empty values System.out.println("String Values :"); System.out.println("First : " + strVal1); System.out.println("Second : " + strVal2); System.out.println("Third : " + strVal3); } }
输出:
在上面给出的程序中,即使 Strings 的对象不同,但分配给 String 的值是相同的。下面给出的输出显示了如何将空值分配给每个字符串变量。
示例#5
在下面给出的程序中,给出了上面初始化字符串的比较。这里使用三种比较字符串的方式。
1. == operator: In java == operator compare the references, not the values.
2. equals() method: It compares the variable’s value, not the references; hence, if the value matches both the String variable, it returns true other false.
3. compareTo() method: compareTo() method check the values & if values are identical then it return 0 otherwise it return + & – based on the below given comparison.
- It returns 0 if str1 == str2
- It returns positive value if str1 > str2
- It returns negative value if str1 < str2
Code:
public class StringExample { public static void main(String[] args) { String strVal1 = ""; String strVal2 = new String(); String strVal3 = new String(""); //printing variable which are initialized with empty values System.out.println("String Values :"); System.out.println("First : " + strVal1); System.out.println("Second : " + strVal2); System.out.println("Third : " + strVal3); System.out.println(" \nComparing strings using == operator :"); System.out.println("Comparing first two strings :"); System.out.println(strVal1 == strVal2); System.out.println("Comparing last two strings :"); System.out.println(strVal2 == strVal3); System.out.println("Comparing first & last strings :"); System.out.println(strVal1 == strVal3); System.out.println(" \nComparing strings using equals method :"); System.out.println("Comparing first two strings :"); System.out.println(strVal1.equals(strVal2)); System.out.println("Comparing last two strings :"); System.out.println(strVal2.equals(strVal3)); System.out.println("Comparing first & last strings :"); System.out.println(strVal1.equals(strVal3)); System.out.println(" \nComparing strings using compareTo method :"); System.out.println("Comparing first two strings :"); System.out.println(strVal1.compareTo(strVal2)); System.out.println("Comparing last two strings :"); System.out.println(strVal2.compareTo(strVal3)); System.out.println("Comparing first & last strings :"); System.out.println(strVal1.compareTo(strVal3)); } }
Output:
The output of the comparing string program is given below. It contains the output after comparing the Strings in three different ways; thus, outcomes of the comparison are also different according to the method applied.
Conclusion
The above-given article explains the initialization of String & different types of ways to initialize the String in java. Also given examples in the article describe the internal creation of objects when initiating String using Literal. It also describes the difference between declaration & initialization.
以上是Java中的字符串初始化的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
