Java 中的字符与字符串
本篇文章介绍了 Java 中字符和字符串的区别。
在 Java 中,char 是一种原始数据类型,用于保存单个字符。它表示 UTF-16 字符集中的单个字符。相比之下,String 是一个包含字符序列的类,可以将其视为字符数组。
你可以认为像 Delft
这样的字符串由 D
、e
、l
、f
、t
字符组成。所以,基本上,一组字符形成一个字符串。让我们通过一些例子来理解。
在 Java 中创建字符
在 Java 中,要创建字符,我们使用单引号将字符括起来。字符可以是任何 UTF-16。在这里,我们使用了多种字符来理解字符集。请参阅下面的示例。
public class SimpleTesting{
public static void main(String[] args){
char ch1 = 'a';
System.out.println(ch1);
char ch2 = 'A';
System.out.println(ch2);
char ch3 = '1';
System.out.println(ch3);
char ch4 = '@';
System.out.println(ch4);
}
}
输出:
a
A
1
@
在 Java 中创建字符串
在这里,我们使用双引号创建一个字符串。字符串可以包含 UTF-16 集的任何字符,例如数字、特殊字符、空格等。请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args){
String str1 = "Hello,";
System.out.println(str1);
String str2 = "This is";
System.out.println(str2);
String str3 = "4 Line String";
System.out.println(str3);
String str4 = "Example!";
System.out.println(str4);
}
}
输出:
Hello,
This is
4 Line String
Example!
Java 中的字符到字符串的转换
Char 和 String 都使用字符来创建,有时当我们需要从字符中获取字符串时,我们可以使用字符类的 toString()
方法。此方法返回原始字符值的 String 对象。请参阅下面的示例。
public class SimpleTesting{
public static void main(String[] args){
char ch = 'A';
System.out.println(ch);
String str = Character.toString(ch);
System.out.println(str);
}
}
输出:
A
A
Java 中字符串到字符的转换
类似地,我们可以使用 String 类的 charAt()
方法将字符串转换为 char。此方法返回指定索引的字符。我们可以通过指定索引值从字符串中获取任何字符。
public class SimpleTesting{
public static void main(String[] args){
String str = "Hello";
System.out.println(str);
char ch = str.charAt(0);
System.out.println(ch);
}
}
输出:
Hello
H
Java 中字符对象的字符基元
如果你正在使用原始字符并希望将它们转换为对象,请使用 Character 类。你只需将字符分配给 Character 引用,Java 将自动完成其余的工作。
当原始值分配给引用时,它会自动转换为对象。这个过程在 Java 中称为装箱,其逆过程称为拆箱。
public class SimpleTesting{
public static void main(String[] args){
char ch = 'A';
System.out.println("primitive char "+ch);
Character chr = ch;
System.out.println("char object "+chr);
char ch2 = chr;
System.out.println("primitive char "+ch2);
}
}
输出:
primitive char A
char object A
primitive char A
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 的列中展平层次索引
发布时间:2024/04/24 浏览次数:1782 分类:Python
-
在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。
计算 Pandas DataFrame 中的方差
发布时间:2024/04/23 浏览次数:212 分类:Python
-
本教程演示了如何计算 Python Pandas DataFrame 中的方差。
Pandas 中的 Groupby 索引列
发布时间:2024/04/23 浏览次数:89 分类:Python
-
本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。