Java interview questions

Source: Internet
Author: User

1. What are the differences between the basic data types in Java and whether the string type is the basic data type.

Six numeric types (four integers, two floating point types), one character type, and one boolean type. Pay attention to the value range of each type. Prevent programs from overflowing the range of this type.

String is a class, not a basic data type. There are also integer classes.

String. indexof ("test") and other methods.

At the same time, the string instance is a constant and cannot be changed after creation.

Basic data types include byte, Int, Char, long, float, double, Boolean, and short.
Java. Lang. string class is of the final type. Therefore, it cannot be inherited or modified.

Java provides two different types: The reference type and the original type (or the built-in type ). Int Is the original data type of Java, and integer is the encapsulation class provided by Java for int. Java provides encapsulation classes for each original type.
Booleanboolean character bytebyte primitive short intinteger Longlong floatfloat doubledouble
The behavior of the reference type and the original type is completely different, and they have different semantics. The reference type and the original type have different features and usage, they include: size and speed problems, which type of data structure is stored, the default value specified when the reference type and original type are used as instance data of a class. The default value of the instance variables referenced by the object is null, and the default value of the original type of instance variables is related to their types.

Stringbuffer can make up for the unchangeable shortcomings of the string class. And thread synchronization is secure.

2. Write a method to reverse the string, for example, input ABC and output CBA.

Public static void main (string [] ARGs ){
System. Out. Print (reversewords ("ABC "));
}

Public static string reversewords (string Str)
{
String result = "";
Char [] charstr = Str. tochararray ();
For (INT I = 1; I <= Str. Length (); I ++)
{
Result = Result + Str. copyvalueof (charstr, str. Length ()-I, 1 );
}
Return result;
}

Test passed

There are many methods, which should be more efficient. It also uses substring to connect one by one.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.