首页 > Java > java教程 > 正文

Java 字符串面试题

WBOY
发布: 2024-08-30 16:29:16
原创
910 人浏览过

在核心Java中,String是理解基础java编程入门的关键类之一。 string 类定义良好并保存在 java.lang 包中。它是一个不可变的类,因此开发者可以直接使用它,而无需每次都创建实例。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

如果您正在寻找与 Java String 相关的工作,您需要准备 2023 年 Java String 面试题。根据不同的工作概况,每次面试确实有所不同。在这里,我们准备了重要的 Java String 面试问题和答案,这将帮助您在面试中取得成功。

在本文中,我们将介绍 10 个最重要且最常见的 Java String 面试问题。这些问题分为以下两部分:

第 1 部分 – Java 字符串面试问题(基础)

第一部分涵盖基本的 Java String 面试问题和答案

Q1。详细解释 Java 编程语言中的 String 类,开发人员如何使用该类。我们应该将 Java 编程语言中的 String 视为一种数据类型吗?如果有请详细说明一下?

答案:

字符串是Java中的关键类之一,它基本上保存在java.lang包中。它根本不像Java int 或long 等其他变量那样被定义为原始数据类型。它主要以单一表示形式保存大量字符串。 String对象在java编程中非常流行,几乎使用了从头到尾的所有编码,它是Java核心包中定义的一个不可变的最终类,JVM通常会创建一个String池来存储所有创建的String对象。

Q2。在 Java 编程语言中,有多种方法可用于创建 String 对象;详细解释可用于创建相同内容的方法,并给出一些带有代码片段的相关示例?

答案:

开发人员可以通过多种方式在 Java 编程语言中创建 String 对象。方法如下:

  • 使用New运算符:我们可以像创建其他对象一样,使用简单的new运算符创建一个字符串对象,但它不会存储在JVM的字符串池中。
  • 使用“运算符:仅使用“运算符即可创建字符串对象,并在其中提供相应的值。同样的用途,JVM 会将该值存储在字符串池中并相应地返回。
  • 使用其他:可以从字节数组、字符数组、StringBuilder 或 StringBuffer 创建字符串对象。
String str = new String("abcd"); à not store in JVM String pool
String str1 = "abcd"; à store in JVM String pool
登录后复制

让我们进入下一个 Java String 面试题

Q3。 Java 语言中经常使用 one 关键字,称为“Palindrome”。请解释一下,当Java编程中的一个字符串对象引用可以作为“回文”调用时,是否可以编写一个java方法来检查它?如果是,请给出一些带有代码片段的示例。

答案:

当特定 String 类的值在反转时可以相同时,一个 String 对象可以称为“回文”。举个例子,我们可以说“cbc”,这里这个字符串值可以被认为是回文,因为这个值的反转总是会提供相同的结果。

通常有两种流行的方法可用于在 String 中验证相同的方法,根据面试官的期望,我们可以依赖相同的方法:

  • StringBuilder strBuilder = new StringBuilder(“cbc”);

strBuilder.reverse();

  • 字符串 str1 = “abc”;

Int lenStr1 = str1.length();
For(int j=0; j If(str1.charAt(j) != str1.charAt(lenStr1 – j-1)){
返回 false;
}
}

返回真;

第四季度。假设您想从 Java 编程语言中定义的 String 对象之一中删除一些 asci 字符或提供或给定字符;如何在 Java 编程中使用单一方法来完成此任务。请用代码片段解释一下?

答案:

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), "……");
}
登录后复制

Q5. One of the very common requirements in a programming language is to make some property in upper case or lower case. Is there any specific methodology defined for String object in a Java programming language to present that property value in upper case or lower case? Please explain with a code snippet?

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());
登录后复制

Part 2 – Java String Interview Questions (Advanced)

Let us now have a look at the advanced Java String Interview Questions.

Q6. Is there any method in String call subsequence? If yes, please explain the same in details with a proper definition?

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.

Q7. One another very common requirement for the developer in Java programming is to compare two objects as per development requirements. Is it possible to compare two String object in the Java programming language? If yes, please explain how it can be done?

Answer:

Comparing between two String values can be done by below approaches:

  • Using a comparable interface: Java String normally implements a comparable interface, so compare To method can be utilized easily to compare two string values.
  • By using equals or equalsIgnoreCase method: String value can be compared easily by using direct method equal or eualsIgnoreCase.

Let us move to the next Java String Interview Questions

Q8. Is it possible to convert one of the String objects into a char data type or one char data type covert to a String object? If yes, please explain how it can be achievable?

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);
}
登录后复制

Q9. One of the very popular methodologies in the Java programming language called the switch case. Is it possible to use switch case methodology in case of using String object for java programming? If yes, please explain how it can be possible?

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.

Q10: Suppose planning to perform every kind of permutation of String class in java programming. Please give some details programming of how we can able to do this by using String object properly?

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板