在核心Java中,String是理解基础java编程入门的关键类之一。 string 类定义良好并保存在 java.lang 包中。它是一个不可变的类,因此开发者可以直接使用它,而无需每次都创建实例。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
如果您正在寻找与 Java String 相关的工作,您需要准备 2023 年 Java String 面试题。根据不同的工作概况,每次面试确实有所不同。在这里,我们准备了重要的 Java String 面试问题和答案,这将帮助您在面试中取得成功。
在本文中,我们将介绍 10 个最重要且最常见的 Java String 面试问题。这些问题分为以下两部分:
第一部分涵盖基本的 Java String 面试问题和答案
答案:
字符串是Java中的关键类之一,它基本上保存在java.lang包中。它根本不像Java int 或long 等其他变量那样被定义为原始数据类型。它主要以单一表示形式保存大量字符串。 String对象在java编程中非常流行,几乎使用了从头到尾的所有编码,它是Java核心包中定义的一个不可变的最终类,JVM通常会创建一个String池来存储所有创建的String对象。
答案:
开发人员可以通过多种方式在 Java 编程语言中创建 String 对象。方法如下:
String str = new String("abcd"); à not store in JVM String pool String str1 = "abcd"; à store in JVM String pool
让我们进入下一个 Java String 面试题
答案:
当特定 String 类的值在反转时可以相同时,一个 String 对象可以称为“回文”。举个例子,我们可以说“cbc”,这里这个字符串值可以被认为是回文,因为这个值的反转总是会提供相同的结果。
通常有两种流行的方法可用于在 String 中验证相同的方法,根据面试官的期望,我们可以依赖相同的方法:
strBuilder.reverse();
Int lenStr1 = str1.length();
For(int j=0; j
If(str1.charAt(j) != str1.charAt(lenStr1 – j-1)){
返回 false;
}
}
返回真;
答案:
These are the basic Java String Interview Questions asked in an interview. One of the key methods we normally used to replace the entire string is the replace All method. It will help the developer for replacing the entire content of the String with another String content as per the requirement of the project. But one of the key characteristics of a replacement. All method is it always accepting String as it’s one of the specific arguments, so it will very much require for using one of the Character classes for creating the string and use the same properly for replacing it with another expected String or an empty string. Please find below the code snippet with an example.
public String replaceString(String str1, char c1){ Return str1.replaceAll(Character.toString(c1), "……"); }
Answer:
It will be one of the very common functionalities required for the developer in any case for the String object variable value. A developer can easily be used to UpperCase and LowerCase methods on the specific String class to getfor getting the entire string value in total upper case or total lower case. This method has one more optional argument, which is a locale object. So someone can able to use some specific locale rules for making the string value in upper case or lower case.
String s = "abc"; String s1 = "ABC" System.out.println("Upper case of S: "+s.toUpperCase()+", lower case of S1: "+s1.toLowerCase());
Let us now have a look at the advanced Java String Interview Questions.
Answer:
CharSequence interface is a very much useful interface in java introduce to Java 1.4. The string class normally implements that specific interface. So the implementation of the subsequence method inside the String class can be possible due to the above reason. Actually, it invokes one of the frequent methods of String called the substring method internally.
Answer:
Comparing between two String values can be done by below approaches:
Let us move to the next Java String Interview Questions
Answer:
charAt method can be used to get an individual character by providing an index, and the CharAraay method converts one string value to a character array.
String str = "abc"; Char c = str.charAt(0); String str1 = new String("This is a test String"); char[] carray= str1.toCharArray(); for(char c1: carray){ System.out.print(c1); }
Answers:
This is the most asked Java String Interview Question in an interview. Switch case is one of the common functionality for avoiding writing a lot of if-else logic in the entire code. It always helps in maintaining code quality properly. Earlier switch case reference only is able to do for the integer or long values only. But from the Java 7 version onwards, java allows using switch case on String object as well. So as of now, the String value can be used for switch case utility.
Answer:
We can use this by using the below code:
Set<String> perm = new HashSet<String>(); char initial = str.charAt(0); String rem = str.substring(1); Set<String> words = permutationFinder(rem); for (String strNew : words) { for (int i = 0;i<=strNew.length();i++){ perm.add(charInsert(strNew, initial, i)); } } return perm;
以上是Java 字符串面试题的详细内容。更多信息请关注PHP中文网其他相关文章!