1

With an Integer object I can do this:

Integer one = new Integer(3);
int oneval = one.intValue();

Is there something similar for the String object?

4
  • no, the only you can do si to get char[] Commented Aug 2, 2013 at 21:43
  • 1
    What are you trying to do? Integer is the wrapper for an int, there is no primitive String. Commented Aug 2, 2013 at 21:44
  • docs.oracle.com/javase/tutorial/java/nutsandbolts/… — note there is no "string primitive," though it mentions String as similar. Commented Aug 2, 2013 at 21:55
  • All Strings are reference types. They are always objects. The Java Language goes to great lengths to provide syntactic sugar so that, at times, they can -seem- like primitives ... but they are always reference types. There is no such thing as a string primitive. Commented Aug 2, 2013 at 22:10

2 Answers 2

12

There is no primitive string type in Java. The closest you can get is to call toCharArray to get the char[] array of characters in the string (and it's a copy of the characters).

Returns:

a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.

Sign up to request clarification or add additional context in comments.

1 Comment

Possibly worth mentioning that you also don't need one (a primitive string type), since String is immutable. (Of course, so is Integer...)
3

Strings are not primitives in Java. Every String is an object.

What you can get from a String is a character array, or an encoded byte array. Although these aren't primitives, they're at least arrays of primitives.

  String s = "Twas brillig";
  char[] sChars = s.toCharArray();
  byte[] sBytes = s.getBytes();  // Default charset
  byte[] sBytes = s.getBytes( Charset.forName("UTF-8") );  // Specific charset

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.