<tfoot id='ZExEn'></tfoot>

      • <bdo id='ZExEn'></bdo><ul id='ZExEn'></ul>
      <i id='ZExEn'><tr id='ZExEn'><dt id='ZExEn'><q id='ZExEn'><span id='ZExEn'><b id='ZExEn'><form id='ZExEn'><ins id='ZExEn'></ins><ul id='ZExEn'></ul><sub id='ZExEn'></sub></form><legend id='ZExEn'></legend><bdo id='ZExEn'><pre id='ZExEn'><center id='ZExEn'></center></pre></bdo></b><th id='ZExEn'></th></span></q></dt></tr></i><div id='ZExEn'><tfoot id='ZExEn'></tfoot><dl id='ZExEn'><fieldset id='ZExEn'></fieldset></dl></div>

      <small id='ZExEn'></small><noframes id='ZExEn'>

        <legend id='ZExEn'><style id='ZExEn'><dir id='ZExEn'><q id='ZExEn'></q></dir></style></legend>

        Java中的字符与字符串?

        时间:2023-07-27

            1. <small id='xq448'></small><noframes id='xq448'>

              <tfoot id='xq448'></tfoot>
                <legend id='xq448'><style id='xq448'><dir id='xq448'><q id='xq448'></q></dir></style></legend>

                  <tbody id='xq448'></tbody>

              • <i id='xq448'><tr id='xq448'><dt id='xq448'><q id='xq448'><span id='xq448'><b id='xq448'><form id='xq448'><ins id='xq448'></ins><ul id='xq448'></ul><sub id='xq448'></sub></form><legend id='xq448'></legend><bdo id='xq448'><pre id='xq448'><center id='xq448'></center></pre></bdo></b><th id='xq448'></th></span></q></dt></tr></i><div id='xq448'><tfoot id='xq448'></tfoot><dl id='xq448'><fieldset id='xq448'></fieldset></dl></div>
                • <bdo id='xq448'></bdo><ul id='xq448'></ul>
                  本文介绍了Java中的字符与字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  今年我正在学习 Java 作为 AP 计算机科学课程的一部分,当我阅读Char"和String"时,我不明白为什么有人会费心使用Char"而只能存储一个字符,而不仅仅是使用字符串",并且能够存储更多.简而言之,如果char"只能存储一个字符,那它有什么意义呢?

                  I am learning Java this year as part of the AP Computer Science curriculum, and while I was reading about "Char" and "String" I could not understand why one would bother to use "Char" and only be able to store one character rather than just use "String" and be able to store much more than that. In short what's the point of "char" if it can only store a single character?

                  推荐答案

                  人们提到内存问题是有道理的,但我认为 99% 的情况下这不是一个非常重要的原因.一个重要的原因是,如果您犯了错误,Java 编译器会告诉您,这样您就不必自己解决了.

                  People are mentioning memory concerns, which are valid, but I don't think that's a very important reason 99% of the time. An important reason is that the Java compiler will tell you if you make a mistake so you don't have to figure it out on your own.

                  例如,如果您只想为变量使用 1 个字符,则可以使用 char 来存储值,现在没有人可以在其中放入任何其他内容而不会出错.如果您改用 String,则字符串中可能有两个字符,即使您希望这永远不可能.事实上,字符串中可能有 0 个字符,这同样糟糕.此外,所有使用 String 的代码都必须说获取字符串的第一个字符",而它可以简单地说给我这个字符".

                  For example, if you only want 1 character for a variable, you can use a char to store the value and now nobody can put anything else in there without it being an error. If you used a String instead, there could be two characters in the String even though you intended that to never be possible. In fact, there could be 0 characters in the String which would be just as bad. Also, all your code that uses the String will have to say "get the first character of the String" where it could simply say, "give me the character".

                  一个类比(不幸的是,这对你来说可能还没有意义)是,为什么我会说一个 Person 有一个 Name 而我可以说 PersonNames 列表?"同样的原因也适用.如果您只希望 Person 有一个 Name,那么给他一个 Name 列表会增加大量维护开销.

                  An analogy (which may not make sense to you yet, unfortunately) would be, "Why would I say a Person has a Name when I could say a Person has a List of Names?" The same reasons apply. If you only want a Person to have one Name, then giving him a list of Names adds a lot maintenance overhead.

                  这篇关于Java中的字符与字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将一个字符与多个字符进行比较 下一篇:将文本文件读入Java中的char数组

                  相关文章

                  最新文章

                      <bdo id='2Qhbj'></bdo><ul id='2Qhbj'></ul>
                  1. <i id='2Qhbj'><tr id='2Qhbj'><dt id='2Qhbj'><q id='2Qhbj'><span id='2Qhbj'><b id='2Qhbj'><form id='2Qhbj'><ins id='2Qhbj'></ins><ul id='2Qhbj'></ul><sub id='2Qhbj'></sub></form><legend id='2Qhbj'></legend><bdo id='2Qhbj'><pre id='2Qhbj'><center id='2Qhbj'></center></pre></bdo></b><th id='2Qhbj'></th></span></q></dt></tr></i><div id='2Qhbj'><tfoot id='2Qhbj'></tfoot><dl id='2Qhbj'><fieldset id='2Qhbj'></fieldset></dl></div>

                    <small id='2Qhbj'></small><noframes id='2Qhbj'>

                    <tfoot id='2Qhbj'></tfoot>

                      <legend id='2Qhbj'><style id='2Qhbj'><dir id='2Qhbj'><q id='2Qhbj'></q></dir></style></legend>